-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rescore collapsed documents #28521
Merged
Merged
Rescore collapsed documents #28521
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90cb54e
rescore in collapse top doc collector
fred84 a770c25
Merge remote-tracking branch 'upstream/master' into 27243_collapse_wi…
fred84 e235174
Merge remote-tracking branch 'upstream/master' into 27243_collapse_wi…
fred84 e737519
test for rescore + collapse
fred84 0b91413
collapse + rescore test
fred84 40fca97
Merge remote-tracking branch 'upstream/master' into 27243_collapse_wi…
fred84 5663f10
Merge remote-tracking branch 'upstream/master' into 27243_collapse_wi…
fred84 a5d8834
Merge branch '27242_collapse_with_rescore' of https://github.com/fred…
fred84 e060df4
clean diff
fred84 4f5729a
fix checkstyle warnings
fred84 cc8f8ef
fix test
fred84 12e4d06
Merge branch 'master' into 27243_collapse_with_rescore
fred84 c5151b5
Merge branch '27243_collapse_with_rescore' of https://github.com/fred…
fred84 8320622
remove rest test + static score in integration test
fred84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -30,19 +30,24 @@ | |
import org.elasticsearch.common.settings.Settings.Builder; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.index.query.MatchQueryBuilder; | ||
import org.elasticsearch.index.query.Operator; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.SearchHits; | ||
import org.elasticsearch.search.collapse.CollapseBuilder; | ||
import org.elasticsearch.search.rescore.QueryRescoreMode; | ||
import org.elasticsearch.search.rescore.QueryRescorerBuilder; | ||
import org.elasticsearch.search.sort.SortBuilders; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; | ||
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; | ||
|
@@ -67,6 +72,7 @@ | |
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThirdHit; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasScore; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
|
@@ -748,4 +754,55 @@ public void testRescorePhaseWithInvalidSort() throws Exception { | |
assertThat(hit.getScore(), equalTo(101f)); | ||
} | ||
} | ||
|
||
public void testRescoreAfterCollapse() throws Exception { | ||
assertAcked(prepareCreate("test") | ||
.addMapping( | ||
"type1", | ||
jsonBuilder() | ||
.startObject() | ||
.startObject("properties") | ||
.startObject("group") | ||
.field("type", "keyword") | ||
.endObject() | ||
.endObject() | ||
.endObject()) | ||
); | ||
|
||
indexDocument(1, "value", "a"); | ||
indexDocument(2, "one one value", "a"); | ||
indexDocument(3, "one one two value", "b"); | ||
// should be highest on rescore, but filtered out during collapse | ||
indexDocument(4, "one two two value", "b"); | ||
|
||
refresh("test"); | ||
|
||
SearchResponse searchResponse = client().prepareSearch("test") | ||
.setTypes("type1") | ||
.setQuery(new MatchQueryBuilder("name", "one")) | ||
.addRescorer(new QueryRescorerBuilder(new MatchQueryBuilder("name", "two"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use the same for the rescore with another field for instance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
.setCollapse(new CollapseBuilder("group")) | ||
.execute() | ||
.actionGet(); | ||
|
||
assertThat(searchResponse.getHits().totalHits, equalTo(3L)); | ||
assertThat(searchResponse.getHits().getHits().length, equalTo(2)); | ||
|
||
Map<String, Float> collapsedHits = Arrays | ||
.stream(searchResponse.getHits().getHits()) | ||
.collect(Collectors.toMap(SearchHit::getId, SearchHit::getScore)); | ||
|
||
assertThat(collapsedHits.keySet(), containsInAnyOrder("2", "3")); | ||
assertThat(collapsedHits.get("3"), greaterThan(collapsedHits.get("2"))); | ||
} | ||
|
||
private void indexDocument(int id, String name, String group) throws IOException { | ||
XContentBuilder docBuilder =jsonBuilder() | ||
.startObject() | ||
.field("name", name) | ||
.field("group", group) | ||
.endObject(); | ||
|
||
client().prepareIndex("test", "type1", Integer.toString(id)).setSource(docBuilder).execute().actionGet(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The score of this query depends on the number of shards, the default similarity, ... To make sure that we have consistent scoring you can use a
function_score
query like the following:... and add the
my_static_doc_score
at indexing time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed