org.elasticsearch.search.rescore.QueryRescorerBuilder Java Examples

The following examples show how to use org.elasticsearch.search.rescore.QueryRescorerBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScriptLanguageDemo.java    From elasticsearch-full with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {


    SearchResponse searchResponse = client.prepareSearch()
            .setIndices("test")
            .setTypes("test")
            .setQuery(QueryBuilders.matchQuery("name","天津公安"))
            .addRescorer(new QueryRescorerBuilder(QueryBuilders.matchPhraseQuery("name","天津公安")))
            .addRescorer(new QueryRescorerBuilder(
                    QueryBuilders.functionScoreQuery(
                            ScoreFunctionBuilders.scriptFunction("doc['time'].value / 10000")
                    )
            ).windowSize(100).setScoreMode(QueryRescoreMode.Multiply))
            .setFrom(0)
            .setSize(100)
            .execute()
            .actionGet();

}
 
Example #2
Source File: StoredLtrQueryIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testScriptFeatureUseCase() throws Exception {
    addElement(new StoredFeature("feature1", Collections.singletonList("query"), "mustache",
            QueryBuilders.matchQuery("field1", "{{query}}").toString()));
    addElement(new StoredFeature("feature6", Arrays.asList("query", "extra_multiplier_ltr"), ScriptFeature.TEMPLATE_LANGUAGE,
            "{\"lang\": \"native\", \"source\": \"feature_extractor\", \"params\": { \"dependent_feature\": \"feature1\"," +
                    " \"extra_script_params\" : {\"extra_multiplier_ltr\": \"extra_multiplier\"}}}"));
    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());

    builder.request().setFeatureSet("my_set");
    builder.request().setFeatureNameQuery("feature1");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    builder.execute().get();
    builder.request().setFeatureNameQuery("feature6");
    long version = builder.get().getResponse().getVersion();

    CreateModelFromSetRequestBuilder createModelFromSetRequestBuilder = new CreateModelFromSetRequestBuilder(client());
    createModelFromSetRequestBuilder.withVersion(IndexFeatureStore.DEFAULT_STORE, "my_set", version,
            "my_model", new StoredLtrModel.LtrModelDefinition("model/linear", SIMPLE_SCRIPT_MODEL, true));
    createModelFromSetRequestBuilder.get();
    buildIndex();
    Map<String, Object> params = new HashMap<>();
    params.put("query", "hello");
    params.put("dependent_feature", new HashMap<>());
    params.put("extra_multiplier_ltr", 100.0d);
    SearchRequestBuilder sb = client().prepareSearch("test_index")
            .setQuery(QueryBuilders.matchQuery("field1", "world"))
            .setRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
                    .modelName("my_model").params(params).toString()))
                    .setScoreMode(QueryRescoreMode.Total)
                    .setQueryWeight(0)
                    .setRescoreQueryWeight(1));

    SearchResponse sr = sb.get();
    assertEquals(1, sr.getHits().getTotalHits().value);
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.greaterThanOrEqualTo(29.0f));
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.lessThanOrEqualTo(30.0f));
}
 
Example #3
Source File: LtrQueryRescorer.java    From fess with Apache License 2.0 5 votes vote down vote up
@Override
public RescorerBuilder<?> evaluate(final Map<String, Object> params) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String modelName = fessConfig.getLtrModelName();
    if (StringUtil.isBlank(modelName)) {
        return null;
    }
    return new QueryRescorerBuilder(new StoredLtrQueryBuilder().modelName(modelName).params(params)).windowSize(fessConfig
            .getLtrWindowSize());
}
 
Example #4
Source File: LoggingIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
public void testFailures() throws Exception {
    prepareModels();
    buildIndex();
    QueryBuilder query = QueryBuilders.matchQuery("field1", "found")
            .boost(random().nextInt(3))
            .queryName("not_sltr");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)));

    assertExcWithMessage(() -> client().prepareSearch("test_index")
            .setTypes("test")
            .setSource(sourceBuilder).get(), IllegalArgumentException.class, "No query named [test] found");

    SearchSourceBuilder sourceBuilder2 = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "not_sltr", false)));

    assertExcWithMessage(() -> client().prepareSearch("test_index")
            .setTypes("test")
            .setSource(sourceBuilder2).get(), IllegalArgumentException.class, "Query named [not_sltr] must be a " +
            "[sltr] query [TermQuery] found");

    SearchSourceBuilder sourceBuilder3 = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addRescoreLogging("first_log", 0, false)));
    assertExcWithMessage(() -> client().prepareSearch("test_index")
            .setTypes("test")
            .setSource(sourceBuilder3).get(), IllegalArgumentException.class, "rescore index [0] is out of bounds, " +
            "only [0]");

    SearchSourceBuilder sourceBuilder4 = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(QueryBuilders.matchAllQuery()))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addRescoreLogging("first_log", 0, false)));
    assertExcWithMessage(() -> client().prepareSearch("test_index")
            .setTypes("test")
            .setSource(sourceBuilder4).get(), IllegalArgumentException.class, "Expected a [sltr] query but found " +
            "a [MatchAllDocsQuery] at index [0]");
}
 
Example #5
Source File: LoggingIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
public void testLog() throws Exception {
    prepareModels();
    Map<String, Doc> docs = buildIndex();

    Map<String, Object> params = new HashMap<>();
    params.put("query", "found");
    List<String> idsColl = new ArrayList<>(docs.keySet());
    Collections.shuffle(idsColl, random());
    String[] ids = idsColl.subList(0, TestUtil.nextInt(random(), 5, 15)).toArray(new String[0]);
    StoredLtrQueryBuilder sbuilder = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test")
            .boost(random().nextInt(3));

    StoredLtrQueryBuilder sbuilder_rescore = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test_rescore")
            .boost(random().nextInt(3));

    QueryBuilder query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp);
    sbuilder.featureSetName(null);
    sbuilder.modelName("my_model");
    sbuilder.boost(random().nextInt(3));
    sbuilder_rescore.featureSetName(null);
    sbuilder_rescore.modelName("my_model");
    sbuilder_rescore.boost(random().nextInt(3));

    query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp2 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp2);

    query = QueryBuilders.boolQuery()
            .must(new WrapperQueryBuilder(sbuilder.toString()))
            .must(
                QueryBuilders.nestedQuery(
                    "nesteddocs1",
                    QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("nesteddocs1.field1", "nestedvalue")),
                    ScoreMode.None
                ).innerHit(new InnerHitBuilder())
    );
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));
    SearchResponse resp3 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHits(docs, resp3);
}
 
Example #6
Source File: LoggingIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
public void testLogExtraLogging() throws Exception {
    prepareModelsExtraLogging();
    Map<String, Doc> docs = buildIndex();

    Map<String, Object> params = new HashMap<>();
    params.put("query", "found");
    List<String> idsColl = new ArrayList<>(docs.keySet());
    Collections.shuffle(idsColl, random());
    String[] ids = idsColl.subList(0, TestUtil.nextInt(random(), 5, 15)).toArray(new String[0]);
    StoredLtrQueryBuilder sbuilder = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test")
            .boost(random().nextInt(3));

    StoredLtrQueryBuilder sbuilder_rescore = new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
            .featureSetName("my_set")
            .params(params)
            .queryName("test_rescore")
            .boost(random().nextInt(3));

    QueryBuilder query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp);
    sbuilder.featureSetName(null);
    sbuilder.modelName("my_model");
    sbuilder.boost(random().nextInt(3));
    sbuilder_rescore.featureSetName(null);
    sbuilder_rescore.modelName("my_model");
    sbuilder_rescore.boost(random().nextInt(3));

    query = QueryBuilders.boolQuery().must(new WrapperQueryBuilder(sbuilder.toString()))
            .filter(QueryBuilders.idsQuery("test").addIds(ids));
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));

    SearchResponse resp2 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp2);

    query = QueryBuilders.boolQuery()
            .must(new WrapperQueryBuilder(sbuilder.toString()))
            .must(
                    QueryBuilders.nestedQuery(
                            "nesteddocs1",
                            QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("nesteddocs1.field1", "nestedvalue")),
                            ScoreMode.None
                    ).innerHit(new InnerHitBuilder())
            );
    sourceBuilder = new SearchSourceBuilder().query(query)
            .fetchSource(false)
            .size(10)
            .addRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(sbuilder_rescore.toString())))
            .ext(Collections.singletonList(
                    new LoggingSearchExtBuilder()
                            .addQueryLogging("first_log", "test", false)
                            .addRescoreLogging("second_log", 0, true)));
    SearchResponse resp3 = client().prepareSearch("test_index").setTypes("test").setSource(sourceBuilder).get();
    assertSearchHitsExtraLogging(docs, resp3);
}