Search in sources :

Example 1 with FilterFunctionBuilder

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

the class DecayFunctionScoreIT method testDateWithoutOrigin.

public void testDateWithoutOrigin() throws Exception {
    DateTime dt = new DateTime(DateTimeZone.UTC);
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num1").field("type", "date").endObject().endObject().endObject().endObject()));
    DateTime docDate = dt.minusDays(1);
    String docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    docDate = dt.minusDays(2);
    docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    docDate = dt.minusDays(3);
    docDateString = docDate.getYear() + "-" + String.format(Locale.ROOT, "%02d", docDate.getMonthOfYear()) + "-" + String.format(Locale.ROOT, "%02d", docDate.getDayOfMonth());
    client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("test", "value").field("num1", docDateString).endObject())).actionGet();
    refresh();
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(QueryBuilders.matchAllQuery(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(linearDecayFunction("num1", null, "7000d")), new FilterFunctionBuilder(gaussDecayFunction("num1", null, "1d")), new FilterFunctionBuilder(exponentialDecayFunction("num1", null, "7000d")) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY))));
    SearchResponse sr = response.actionGet();
    assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(3));
    double[] scores = new double[4];
    for (int i = 0; i < sh.getHits().length; i++) {
        scores[Integer.parseInt(sh.getAt(i).getId()) - 1] = sh.getAt(i).getScore();
    }
    assertThat(scores[1], lessThan(scores[0]));
    assertThat(scores[2], lessThan(scores[1]));
}
Also used : FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) DateTime(org.joda.time.DateTime) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with FilterFunctionBuilder

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

the class DecayFunctionScoreIT method testValueMissingLin.

public void testValueMissingLin() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("test").field("type", "text").endObject().startObject("num1").field("type", "date").endObject().startObject("num2").field("type", "double").endObject().endObject().endObject().endObject()));
    client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-27").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("test", "value").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-30").field("num2", "1.0").endObject())).actionGet();
    client().index(indexRequest("test").type("type1").id("4").source(jsonBuilder().startObject().field("test", "value").field("num1", "2013-05-30").endObject())).actionGet();
    refresh();
    ActionFuture<SearchResponse> response = client().search(searchRequest().searchType(SearchType.QUERY_THEN_FETCH).source(searchSource().query(functionScoreQuery(baseQuery, new FilterFunctionBuilder[] { new FilterFunctionBuilder(linearDecayFunction("num1", "2013-05-28", "+3d")), new FilterFunctionBuilder(linearDecayFunction("num2", "0.0", "1")) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.MULTIPLY))));
    SearchResponse sr = response.actionGet();
    assertNoFailures(sr);
    SearchHits sh = sr.getHits();
    assertThat(sh.getHits().length, equalTo(4));
    double[] scores = new double[4];
    for (int i = 0; i < sh.getHits().length; i++) {
        scores[Integer.parseInt(sh.getAt(i).getId()) - 1] = sh.getAt(i).getScore();
    }
    assertThat(scores[0], lessThan(scores[1]));
    assertThat(scores[2], lessThan(scores[3]));
}
Also used : FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchAssertions.assertOrderedSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits) ElasticsearchAssertions.assertSearchHits(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with FilterFunctionBuilder

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

the class FunctionScoreIT method testMinScoreFunctionScoreBasic.

public void testMinScoreFunctionScoreBasic() throws IOException {
    index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).endObject());
    refresh();
    float score = randomFloat();
    float minScore = randomFloat();
    index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).field("random_score", // Pass the random score as a document field so that it can be extracted in the script
    score).endObject());
    refresh();
    ensureYellow();
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['random_score']", Collections.emptyMap());
    SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)))).actionGet();
    if (score < minScore) {
        assertThat(searchResponse.getHits().getTotalHits(), is(0L));
    } else {
        assertThat(searchResponse.getHits().getTotalHits(), is(1L));
    }
    searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)))).actionGet();
    if (score < minScore) {
        assertThat(searchResponse.getHits().getTotalHits(), is(0L));
    } else {
        assertThat(searchResponse.getHits().getTotalHits(), is(1L));
    }
}
Also used : Script(org.elasticsearch.script.Script) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 4 with FilterFunctionBuilder

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

the class FunctionScoreIT method testMinScoreFunctionScoreManyDocsAndRandomMinScore.

public void testMinScoreFunctionScoreManyDocsAndRandomMinScore() throws IOException, ExecutionException, InterruptedException {
    List<IndexRequestBuilder> docs = new ArrayList<>();
    int numDocs = randomIntBetween(1, 100);
    int scoreOffset = randomIntBetween(-2 * numDocs, 2 * numDocs);
    int minScore = randomIntBetween(-2 * numDocs, 2 * numDocs);
    for (int i = 0; i < numDocs; i++) {
        docs.add(client().prepareIndex(INDEX, TYPE, Integer.toString(i)).setSource("num", i + scoreOffset));
    }
    indexRandom(true, docs);
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "return (doc['num'].value)", Collections.emptyMap());
    int numMatchingDocs = numDocs + scoreOffset - minScore;
    if (numMatchingDocs < 0) {
        numMatchingDocs = 0;
    }
    if (numMatchingDocs > numDocs) {
        numMatchingDocs = numDocs;
    }
    SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
    searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 5 with FilterFunctionBuilder

use of org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder 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

FilterFunctionBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)15 ArrayList (java.util.ArrayList)10 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)3 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)3 SearchHits (org.elasticsearch.search.SearchHits)3 ElasticsearchAssertions.assertOrderedSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits)3 ElasticsearchAssertions.assertSearchHits (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)2 Script (org.elasticsearch.script.Script)2 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)2 DataConfigToLabelCQ (org.codelibs.fess.es.config.cbean.cq.DataConfigToLabelCQ)1 DataConfigToRoleCQ (org.codelibs.fess.es.config.cbean.cq.DataConfigToRoleCQ)1 FileConfigToLabelCQ (org.codelibs.fess.es.config.cbean.cq.FileConfigToLabelCQ)1 FileConfigToRoleCQ (org.codelibs.fess.es.config.cbean.cq.FileConfigToRoleCQ)1 LabelToRoleCQ (org.codelibs.fess.es.config.cbean.cq.LabelToRoleCQ)1 WebConfigToLabelCQ (org.codelibs.fess.es.config.cbean.cq.WebConfigToLabelCQ)1 WebConfigToRoleCQ (org.codelibs.fess.es.config.cbean.cq.WebConfigToRoleCQ)1 SearchFieldLogCQ (org.codelibs.fess.es.log.cbean.cq.SearchFieldLogCQ)1