Search in sources :

Example 1 with FunctionScoreQueryBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder in project elasticsearch by elastic.

the class InnerHitBuilderTests method testInlineLeafInnerHitsNestedQueryViaFunctionScoreQuery.

public void testInlineLeafInnerHitsNestedQueryViaFunctionScoreQuery() {
    InnerHitBuilder leafInnerHits = randomInnerHits();
    NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None).innerHit(leafInnerHits, false);
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(nestedQueryBuilder);
    Map<String, InnerHitBuilder> innerHitBuilders = new HashMap<>();
    ((AbstractQueryBuilder<?>) functionScoreQueryBuilder).extractInnerHitBuilders(innerHitBuilders);
    assertThat(innerHitBuilders.get(leafInnerHits.getName()), notNullValue());
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) HashMap(java.util.HashMap)

Example 2 with FunctionScoreQueryBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testCombineModes.

public void testCombineModes() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num").field("type", "double").endObject().endObject().endObject().endObject()));
    client().prepareIndex().setType("type1").setId("1").setIndex("test").setRefreshPolicy(IMMEDIATE).setSource(jsonBuilder().startObject().field("test", "value value").field("num", 1.0).endObject()).get();
    FunctionScoreQueryBuilder baseQuery = functionScoreQuery(constantScoreQuery(termQuery("test", "value")), ScoreFunctionBuilders.weightFactorFunction(2));
    // decay score should return 0.5 for this function and baseQuery should return 2.0f as it's score
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.MULTIPLY))));
    SearchResponse sr = response.actionGet();
    SearchHits sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(1.0, 1.e-5));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.REPLACE))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(0.5, 1.e-5));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.SUM))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(2.0 + 0.5, 1.e-5));
    logger.info("--> Hit[0] {} Explanation:\n {}", sr.getHits().getAt(0).getId(), sr.getHits().getAt(0).getExplanation());
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.AVG))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo((2.0 + 0.5) / 2, 1.e-5));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.MIN))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(0.5, 1.e-5));
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("num", 0.0, 1.0, null, 0.5)).boostMode(CombineFunction.MAX))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(2.0, 1.e-5));
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with FunctionScoreQueryBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder in project elasticsearch by elastic.

the class DecayFunctionScoreIT method testParseGeoPoint.

public void testParseGeoPoint() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("loc").field("type", "geo_point").endObject().endObject().endObject().endObject()));
    client().prepareIndex().setType("type1").setId("1").setIndex("test").setSource(jsonBuilder().startObject().field("test", "value").startObject("loc").field("lat", 20).field("lon", 11).endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    FunctionScoreQueryBuilder baseQuery = functionScoreQuery(constantScoreQuery(termQuery("test", "value")), ScoreFunctionBuilders.weightFactorFunction(randomIntBetween(1, 10)));
    GeoPoint point = new GeoPoint(20, 11);
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", point, "1000km")).boostMode(CombineFunction.REPLACE))));
    SearchResponse sr = response.actionGet();
    SearchHits sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(1.0, 1.e-5));
    // this is equivalent to new GeoPoint(20, 11); just flipped so scores must be same
    float[] coords = { 11, 20 };
    response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, gaussDecayFunction("loc", coords, "1000km")).boostMode(CombineFunction.REPLACE))));
    sr = response.actionGet();
    sh = sr.getHits();
    assertThat(sh.getTotalHits(), equalTo((long) (1)));
    assertThat(sh.getAt(0).getId(), equalTo("1"));
    assertThat((double) sh.getAt(0).getScore(), closeTo(1.0f, 1.e-5));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with FunctionScoreQueryBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder in project elasticsearch by elastic.

the class HighlighterSearchIT method testFiltersFunctionScoreQueryHighlight.

public void testFiltersFunctionScoreQueryHighlight() throws Exception {
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "brown").field("enable", "yes").endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    FunctionScoreQueryBuilder.FilterFunctionBuilder filterBuilder = new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("enable", "yes"), new RandomScoreFunctionBuilder());
    for (String type : UNIFIED_AND_NULL) {
        SearchResponse searchResponse = client().prepareSearch().setQuery(new FunctionScoreQueryBuilder(QueryBuilders.prefixQuery("text", "bro"), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { filterBuilder })).highlighter(new HighlightBuilder().field(new Field("text")).highlighterType(type)).get();
        assertHitCount(searchResponse, 1);
        HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("text");
        assertThat(field.getFragments().length, equalTo(1));
        assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with FunctionScoreQueryBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder in project fess by codelibs.

the class BsDataConfigToRoleCQ method functionScore.

// ===================================================================================
//                                                                       Query Control
//                                                                       =============
public void functionScore(OperatorCall<DataConfigToRoleCQ> queryLambda, ScoreFunctionCall<ScoreFunctionCreator<DataConfigToRoleCQ>> functionsLambda, final ConditionOptionCall<FunctionScoreQueryBuilder> opLambda) {
    DataConfigToRoleCQ cq = new DataConfigToRoleCQ();
    queryLambda.callback(cq);
    final Collection<FilterFunctionBuilder> list = new ArrayList<>();
    if (functionsLambda != null) {
        functionsLambda.callback((cqLambda, scoreFunctionBuilder) -> {
            DataConfigToRoleCQ cf = new DataConfigToRoleCQ();
            cqLambda.callback(cf);
            list.add(new FilterFunctionBuilder(cf.getQuery(), scoreFunctionBuilder));
        });
    }
    final FunctionScoreQueryBuilder builder = regFunctionScoreQ(cq.getQuery(), list);
    if (opLambda != null) {
        opLambda.callback(builder);
    }
}
Also used : FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) DataConfigToRoleCQ(org.codelibs.fess.es.config.cbean.cq.DataConfigToRoleCQ) ArrayList(java.util.ArrayList)

Aggregations

FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)17 ArrayList (java.util.ArrayList)9 FilterFunctionBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)8 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)2 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)2 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)2 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)2 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)2 RandomScoreFunctionBuilder (org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder)2 SearchHits (org.elasticsearch.search.SearchHits)2 Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)2 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)2 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)2 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Search (io.searchbox.core.Search)1 SearchResult (io.searchbox.core.SearchResult)1 HashMap (java.util.HashMap)1