Search in sources :

Example 1 with QueryRescorerBuilder

use of org.elasticsearch.search.rescore.QueryRescorerBuilder in project elasticsearch by elastic.

the class QueryRescorerIT method testExplain.

public void testExplain() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox").execute().actionGet();
    client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree").execute().actionGet();
    client().prepareIndex("test", "type1", "3").setSource("field1", "quick huge brown", "field2", "the quick lazy huge brown fox jumps over the tree").execute().actionGet();
    refresh();
    {
        SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)).setRescorer(queryRescorer(matchPhraseQuery("field1", "the quick brown").slop(2).boost(4.0f)).setQueryWeight(0.5f).setRescoreQueryWeight(0.4f), 5).setExplain(true).execute().actionGet();
        assertHitCount(searchResponse, 3);
        assertFirstHit(searchResponse, hasId("1"));
        assertSecondHit(searchResponse, hasId("2"));
        assertThirdHit(searchResponse, hasId("3"));
        for (int i = 0; i < 3; i++) {
            assertThat(searchResponse.getHits().getAt(i).getExplanation(), notNullValue());
            assertThat(searchResponse.getHits().getAt(i).getExplanation().isMatch(), equalTo(true));
            assertThat(searchResponse.getHits().getAt(i).getExplanation().getDetails().length, equalTo(2));
            assertThat(searchResponse.getHits().getAt(i).getExplanation().getDetails()[0].isMatch(), equalTo(true));
            if (i == 2) {
                assertThat(searchResponse.getHits().getAt(i).getExplanation().getDetails()[1].getValue(), equalTo(0.5f));
            } else {
                assertThat(searchResponse.getHits().getAt(i).getExplanation().getDescription(), equalTo("sum of:"));
                assertThat(searchResponse.getHits().getAt(i).getExplanation().getDetails()[0].getDetails()[1].getValue(), equalTo(0.5f));
                assertThat(searchResponse.getHits().getAt(i).getExplanation().getDetails()[1].getDetails()[1].getValue(), equalTo(0.4f));
            }
        }
    }
    String[] scoreModes = new String[] { "max", "min", "avg", "total", "multiply", "" };
    String[] descriptionModes = new String[] { "max of:", "min of:", "avg of:", "sum of:", "product of:", "sum of:" };
    for (int innerMode = 0; innerMode < scoreModes.length; innerMode++) {
        QueryRescorerBuilder innerRescoreQuery = queryRescorer(QueryBuilders.matchQuery("field1", "the quick brown").boost(4.0f)).setQueryWeight(0.5f).setRescoreQueryWeight(0.4f);
        if (!"".equals(scoreModes[innerMode])) {
            innerRescoreQuery.setScoreMode(QueryRescoreMode.fromString(scoreModes[innerMode]));
        }
        SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)).setRescorer(innerRescoreQuery, 5).setExplain(true).execute().actionGet();
        assertHitCount(searchResponse, 3);
        assertFirstHit(searchResponse, hasId("1"));
        assertSecondHit(searchResponse, hasId("2"));
        assertThirdHit(searchResponse, hasId("3"));
        for (int j = 0; j < 3; j++) {
            assertThat(searchResponse.getHits().getAt(j).getExplanation().getDescription(), equalTo(descriptionModes[innerMode]));
        }
        for (int outerMode = 0; outerMode < scoreModes.length; outerMode++) {
            QueryRescorerBuilder outerRescoreQuery = queryRescorer(QueryBuilders.matchQuery("field1", "the quick brown").boost(4.0f)).setQueryWeight(0.5f).setRescoreQueryWeight(0.4f);
            if (!"".equals(scoreModes[outerMode])) {
                outerRescoreQuery.setScoreMode(QueryRescoreMode.fromString(scoreModes[outerMode]));
            }
            searchResponse = client().prepareSearch().setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(Operator.OR)).addRescorer(innerRescoreQuery, 5).addRescorer(outerRescoreQuery.windowSize(10)).setExplain(true).get();
            assertHitCount(searchResponse, 3);
            assertFirstHit(searchResponse, hasId("1"));
            assertSecondHit(searchResponse, hasId("2"));
            assertThirdHit(searchResponse, hasId("3"));
            for (int j = 0; j < 3; j++) {
                Explanation explanation = searchResponse.getHits().getAt(j).getExplanation();
                assertThat(explanation.getDescription(), equalTo(descriptionModes[outerMode]));
                assertThat(explanation.getDetails()[0].getDetails()[0].getDescription(), equalTo(descriptionModes[innerMode]));
            }
        }
    }
}
Also used : QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Explanation(org.apache.lucene.search.Explanation) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with QueryRescorerBuilder

use of org.elasticsearch.search.rescore.QueryRescorerBuilder in project elasticsearch by elastic.

the class SimpleSearchIT method assertRescoreWindowFails.

private void assertRescoreWindowFails(int windowSize) {
    SearchRequestBuilder search = client().prepareSearch("idx").addRescorer(new QueryRescorerBuilder(matchAllQuery()).windowSize(windowSize));
    SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> search.get());
    assertThat(e.toString(), containsString("Rescore window [" + windowSize + "] is too large. It must " + "be less than [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.get(Settings.EMPTY)));
    assertThat(e.toString(), containsString("This limit can be set by changing the [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.getKey() + "] index level setting."));
}
Also used : QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException)

Example 3 with QueryRescorerBuilder

use of org.elasticsearch.search.rescore.QueryRescorerBuilder in project elasticsearch by elastic.

the class QueryRescorerIT method testScoring.

public void testScoring() throws Exception {
    int numDocs = indexRandomNumbers("keyword");
    String[] scoreModes = new String[] { "max", "min", "avg", "total", "multiply", "" };
    float primaryWeight = 1.1f;
    float secondaryWeight = 1.6f;
    for (String scoreMode : scoreModes) {
        for (int i = 0; i < numDocs - 4; i++) {
            String[] intToEnglish = new String[] { English.intToEnglish(i), English.intToEnglish(i + 1), English.intToEnglish(i + 2), English.intToEnglish(i + 3) };
            QueryBuilder query = boolQuery().disableCoord(true).should(functionScoreQuery(termQuery("field1", intToEnglish[0]), weightFactorFunction(2.0f)).boostMode(REPLACE)).should(functionScoreQuery(termQuery("field1", intToEnglish[1]), weightFactorFunction(3.0f)).boostMode(REPLACE)).should(functionScoreQuery(termQuery("field1", intToEnglish[2]), weightFactorFunction(5.0f)).boostMode(REPLACE)).should(functionScoreQuery(termQuery("field1", intToEnglish[3]), weightFactorFunction(0.2f)).boostMode(REPLACE));
            QueryRescorerBuilder rescoreQuery = queryRescorer(boolQuery().disableCoord(true).should(functionScoreQuery(termQuery("field1", intToEnglish[0]), weightFactorFunction(5.0f)).boostMode(REPLACE)).should(functionScoreQuery(termQuery("field1", intToEnglish[1]), weightFactorFunction(7.0f)).boostMode(REPLACE)).should(functionScoreQuery(termQuery("field1", intToEnglish[3]), weightFactorFunction(0.0f)).boostMode(REPLACE)));
            rescoreQuery.setQueryWeight(primaryWeight).setRescoreQueryWeight(secondaryWeight);
            if (!"".equals(scoreMode)) {
                rescoreQuery.setScoreMode(QueryRescoreMode.fromString(scoreMode));
            }
            SearchResponse rescored = client().prepareSearch().setPreference(// ensure we hit the same shards for tie-breaking
            "test").setFrom(0).setSize(10).setQuery(query).setRescorer(rescoreQuery, 50).get();
            assertHitCount(rescored, 4);
            assertThat(rescored.getHits().getMaxScore(), equalTo(rescored.getHits().getHits()[0].getScore()));
            if ("total".equals(scoreMode) || "".equals(scoreMode)) {
                assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
                assertSecondHit(rescored, hasId(String.valueOf(i)));
                assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
                assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(3.0f * primaryWeight + 7.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(2.0f * primaryWeight + 5.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight + 0.0f * secondaryWeight));
            } else if ("max".equals(scoreMode)) {
                assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
                assertSecondHit(rescored, hasId(String.valueOf(i)));
                assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
                assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(7.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(5.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight));
            } else if ("min".equals(scoreMode)) {
                assertFirstHit(rescored, hasId(String.valueOf(i + 2)));
                assertSecondHit(rescored, hasId(String.valueOf(i + 1)));
                assertThirdHit(rescored, hasId(String.valueOf(i)));
                assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(5.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(3.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(2.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.0f * secondaryWeight));
            } else if ("avg".equals(scoreMode)) {
                assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
                assertSecondHit(rescored, hasId(String.valueOf(i + 2)));
                assertThirdHit(rescored, hasId(String.valueOf(i)));
                assertThat(rescored.getHits().getHits()[0].getScore(), equalTo((3.0f * primaryWeight + 7.0f * secondaryWeight) / 2.0f));
                assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(5.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[2].getScore(), equalTo((2.0f * primaryWeight + 5.0f * secondaryWeight) / 2.0f));
                assertThat(rescored.getHits().getHits()[3].getScore(), equalTo((0.2f * primaryWeight) / 2.0f));
            } else if ("multiply".equals(scoreMode)) {
                assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
                assertSecondHit(rescored, hasId(String.valueOf(i)));
                assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
                assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(3.0f * primaryWeight * 7.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(2.0f * primaryWeight * 5.0f * secondaryWeight));
                assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
                assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight * 0.0f * secondaryWeight));
            }
        }
    }
}
Also used : QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with QueryRescorerBuilder

use of org.elasticsearch.search.rescore.QueryRescorerBuilder in project elasticsearch by elastic.

the class QueryRescorerIT method testMultipleRescores.

public void testMultipleRescores() throws Exception {
    int numDocs = indexRandomNumbers("keyword", 1, true);
    QueryRescorerBuilder eightIsGreat = RescoreBuilder.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", English.intToEnglish(8)), ScoreFunctionBuilders.weightFactorFunction(1000.0f)).boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
    QueryRescorerBuilder sevenIsBetter = RescoreBuilder.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", English.intToEnglish(7)), ScoreFunctionBuilders.weightFactorFunction(10000.0f)).boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
    // First set the rescore window large enough that both rescores take effect
    SearchRequestBuilder request = client().prepareSearch();
    request.addRescorer(eightIsGreat, numDocs).addRescorer(sevenIsBetter, numDocs);
    SearchResponse response = request.get();
    assertFirstHit(response, hasId("7"));
    assertSecondHit(response, hasId("8"));
    // Now squash the second rescore window so it never gets to see a seven
    response = request.setSize(1).clearRescorers().addRescorer(eightIsGreat, numDocs).addRescorer(sevenIsBetter, 1).get();
    assertFirstHit(response, hasId("8"));
    // We have no idea what the second hit will be because we didn't get a chance to look for seven
    // Now use one rescore to drag the number we're looking for into the window of another
    QueryRescorerBuilder ninetyIsGood = RescoreBuilder.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.queryStringQuery("*ninety*"), ScoreFunctionBuilders.weightFactorFunction(1000.0f)).boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
    QueryRescorerBuilder oneToo = RescoreBuilder.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.queryStringQuery("*one*"), ScoreFunctionBuilders.weightFactorFunction(1000.0f)).boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
    request.clearRescorers().addRescorer(ninetyIsGood, numDocs).addRescorer(oneToo, 10);
    response = request.setSize(2).get();
    assertThat(response.getHits().getMaxScore(), equalTo(response.getHits().getHits()[0].getScore()));
    assertFirstHit(response, hasId("91"));
    assertFirstHit(response, hasScore(2001.0f));
    // Not sure which one it is but it is ninety something
    assertSecondHit(response, hasScore(1001.0f));
}
Also used : QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

QueryRescorerBuilder (org.elasticsearch.search.rescore.QueryRescorerBuilder)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 Explanation (org.apache.lucene.search.Explanation)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1