-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SearchStrategyRegistry and defaultSearchStrategy to support exist…
…ing search behavior, and integrate it with CallClient.
- Loading branch information
Showing
6 changed files
with
255 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/ui/public/courier/search_strategy/default_search_strategy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export const defaultSearchStrategy = { | ||
id: 'default', | ||
|
||
search: ({ body, es }) => { | ||
return es.msearch({ body }); | ||
}, | ||
|
||
isValidForSearchRequest: searchRequest => { | ||
// Basic index patterns don't have `type` defined. | ||
const indexPattern = searchRequest.source.getField('index'); | ||
return indexPattern.type == null; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { | ||
assignSearchRequestsToSearchStrategies, | ||
addSearchStrategy, | ||
} from './search_strategy_registry'; | ||
|
||
export { | ||
defaultSearchStrategy, | ||
} from './default_search_strategy'; |
69 changes: 69 additions & 0 deletions
69
src/ui/public/courier/search_strategy/search_strategy_registry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
const searchStrategies = []; | ||
|
||
const addSearchStrategy = searchStrategy => { | ||
searchStrategies.push(searchStrategy); | ||
}; | ||
|
||
/** | ||
* Build a structure like this: | ||
* | ||
* [{ | ||
* searchStrategy: <rollupSearchStrategy>, | ||
* searchRequests: []<SearchRequest>, | ||
* }, { | ||
* searchStrategy: <defaultSearchStrategy>, | ||
* searchRequests: []<SearchRequest>, | ||
* }] | ||
* | ||
* We use an array of objects to preserve the order of the search requests, which use to | ||
* deterministically associate each response with the originating request. | ||
*/ | ||
const assignSearchRequestsToSearchStrategies = searchRequests => { | ||
const searchStrategiesWithRequests = []; | ||
const searchStrategyById = {}; | ||
|
||
searchRequests.forEach(searchRequest => { | ||
const matchingSearchStrategy = searchStrategies.find(searchStrategy => searchStrategy.isValidForSearchRequest(searchRequest)); | ||
const { id } = matchingSearchStrategy; | ||
let searchStrategyWithRequest = searchStrategyById[id]; | ||
|
||
// Create the data structure if we don't already have it. | ||
if (!searchStrategyWithRequest) { | ||
searchStrategyWithRequest = { | ||
searchStrategy: matchingSearchStrategy, | ||
searchRequests: [], | ||
}; | ||
|
||
searchStrategyById[id] = searchStrategyWithRequest; | ||
searchStrategiesWithRequests.push(searchStrategyWithRequest); | ||
} | ||
|
||
searchStrategyWithRequest.searchRequests.push(searchRequest); | ||
}); | ||
|
||
return searchStrategiesWithRequests; | ||
}; | ||
|
||
export { | ||
assignSearchRequestsToSearchStrategies, | ||
addSearchStrategy, | ||
}; |
83 changes: 83 additions & 0 deletions
83
src/ui/public/courier/search_strategy/search_strategy_registry.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
assignSearchRequestsToSearchStrategies, | ||
addSearchStrategy, | ||
} from './search_strategy_registry'; | ||
|
||
describe('SearchStrategyRegistry', () => { | ||
describe('assignSearchRequestsToSearchStrategies', () => { | ||
test('associates search requests with valid search strategies', () => { | ||
const searchStrategyA = { | ||
id: 'a', | ||
isValidForSearchRequest: searchRequest => { | ||
return searchRequest.type === 'a'; | ||
}, | ||
}; | ||
|
||
addSearchStrategy(searchStrategyA); | ||
|
||
const searchStrategyB = { | ||
id: 'b', | ||
isValidForSearchRequest: searchRequest => { | ||
return searchRequest.type === 'b'; | ||
}, | ||
}; | ||
|
||
addSearchStrategy(searchStrategyB); | ||
|
||
const searchRequests = [{ | ||
id: 0, | ||
type: 'b', | ||
}, { | ||
id: 1, | ||
type: 'a', | ||
}, { | ||
id: 2, | ||
type: 'a', | ||
}, { | ||
id: 3, | ||
type: 'b', | ||
}]; | ||
|
||
const searchStrategiesWithSearchRequests = assignSearchRequestsToSearchStrategies(searchRequests); | ||
|
||
expect(searchStrategiesWithSearchRequests).toEqual([{ | ||
searchStrategy: searchStrategyB, | ||
searchRequests: [{ | ||
id: 0, | ||
type: 'b', | ||
}, { | ||
id: 3, | ||
type: 'b', | ||
}], | ||
}, { | ||
searchStrategy: searchStrategyA, | ||
searchRequests: [{ | ||
id: 1, | ||
type: 'a', | ||
}, { | ||
id: 2, | ||
type: 'a', | ||
}], | ||
}]); | ||
}); | ||
}); | ||
}); |