Search in sources :

Example 11 with Builder

use of org.elasticsearch.common.settings.Settings.Builder in project elasticsearch by elastic.

the class QueryRescorerIT method testMoreDocs.

public void testMoreDocs() throws Exception {
    Builder builder = Settings.builder();
    builder.put("index.analysis.analyzer.synonym.tokenizer", "whitespace");
    builder.putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase");
    builder.put("index.analysis.filter.synonym.type", "synonym");
    builder.putArray("index.analysis.filter.synonym.synonyms", "ave => ave, avenue", "street => str, street");
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").field("search_analyzer", "synonym").endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test", "type1", "1").setSource("field1", "massachusetts avenue boston massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "2").setSource("field1", "lexington avenue boston massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "3").setSource("field1", "boston avenue lexington massachusetts").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    client().prepareIndex("test", "type1", "4").setSource("field1", "boston road lexington massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "5").setSource("field1", "lexington street lexington massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "6").setSource("field1", "massachusetts avenue lexington massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "7").setSource("field1", "bosten street san franciso california").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    client().prepareIndex("test", "type1", "8").setSource("field1", "hollywood boulevard los angeles california").execute().actionGet();
    client().prepareIndex("test", "type1", "9").setSource("field1", "1st street boston massachussetts").execute().actionGet();
    client().prepareIndex("test", "type1", "10").setSource("field1", "1st street boston massachusetts").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    client().prepareIndex("test", "type1", "11").setSource("field1", "2st street boston massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "12").setSource("field1", "3st street boston massachusetts").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    // Make sure non-zero from works:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(2).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), greaterThan(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
}
Also used : RescoreBuilder(org.elasticsearch.search.rescore.RescoreBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 12 with Builder

use of org.elasticsearch.common.settings.Settings.Builder in project elasticsearch by elastic.

the class StopTokenFilterTests method testCorrectPositionIncrementSetting.

public void testCorrectPositionIncrementSetting() throws IOException {
    Builder builder = Settings.builder().put("index.analysis.filter.my_stop.type", "stop");
    if (random().nextBoolean()) {
        builder.put("index.analysis.filter.my_stop.version", Version.LATEST);
    } else {
    // don't specify
    }
    builder.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString());
    ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(builder.build());
    TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_stop");
    assertThat(tokenFilter, instanceOf(StopTokenFilterFactory.class));
    Tokenizer tokenizer = new WhitespaceTokenizer();
    tokenizer.setReader(new StringReader("foo bar"));
    TokenStream create = tokenFilter.create(tokenizer);
    assertThat(create, instanceOf(StopFilter.class));
}
Also used : WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) TokenStream(org.apache.lucene.analysis.TokenStream) ESTestCase(org.elasticsearch.test.ESTestCase) StopFilter(org.apache.lucene.analysis.StopFilter) SuggestStopFilter(org.apache.lucene.search.suggest.analyzing.SuggestStopFilter) Builder(org.elasticsearch.common.settings.Settings.Builder) StringReader(java.io.StringReader) WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) Tokenizer(org.apache.lucene.analysis.Tokenizer)

Example 13 with Builder

use of org.elasticsearch.common.settings.Settings.Builder in project elasticsearch by elastic.

the class QueryRescorerIT method testSmallRescoreWindow.

// Tests a rescore window smaller than number of hits:
public void testSmallRescoreWindow() throws Exception {
    Builder builder = Settings.builder();
    builder.put("index.analysis.analyzer.synonym.tokenizer", "whitespace");
    builder.putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase");
    builder.put("index.analysis.filter.synonym.type", "synonym");
    builder.putArray("index.analysis.filter.synonym.synonyms", "ave => ave, avenue", "street => str, street");
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").field("search_analyzer", "synonym").endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test", "type1", "3").setSource("field1", "massachusetts").execute().actionGet();
    client().prepareIndex("test", "type1", "6").setSource("field1", "massachusetts avenue lexington massachusetts").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    client().prepareIndex("test", "type1", "1").setSource("field1", "lexington massachusetts avenue").execute().actionGet();
    client().prepareIndex("test", "type1", "2").setSource("field1", "lexington avenue boston massachusetts road").execute().actionGet();
    client().admin().indices().prepareRefresh("test").execute().actionGet();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).execute().actionGet();
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("1"));
    assertFourthHit(searchResponse, hasId("2"));
    // Now, rescore only top 2 hits w/ proximity:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 2).execute().actionGet();
    // Only top 2 hits were re-ordered:
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("6"));
    assertSecondHit(searchResponse, hasId("3"));
    assertThirdHit(searchResponse, hasId("1"));
    assertFourthHit(searchResponse, hasId("2"));
    // Now, rescore only top 3 hits w/ proximity:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 3).execute().actionGet();
    // Only top 3 hits were re-ordered:
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("6"));
    assertSecondHit(searchResponse, hasId("1"));
    assertThirdHit(searchResponse, hasId("3"));
    assertFourthHit(searchResponse, hasId("2"));
}
Also used : RescoreBuilder(org.elasticsearch.search.rescore.RescoreBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 14 with Builder

use of org.elasticsearch.common.settings.Settings.Builder in project elasticsearch by elastic.

the class QueryRescorerIT method testFromSize.

// #11277
public void testFromSize() throws Exception {
    Builder settings = Settings.builder();
    settings.put(SETTING_NUMBER_OF_SHARDS, 1);
    settings.put(SETTING_NUMBER_OF_REPLICAS, 0);
    assertAcked(prepareCreate("test").setSettings(settings));
    for (int i = 0; i < 5; i++) {
        client().prepareIndex("test", "type", "" + i).setSource("text", "hello world").get();
    }
    refresh();
    SearchRequestBuilder request = client().prepareSearch();
    request.setQuery(QueryBuilders.termQuery("text", "hello"));
    request.setFrom(1);
    request.setSize(4);
    request.addRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchAllQuery()), 50);
    assertEquals(4, request.get().getHits().getHits().length);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) RescoreBuilder(org.elasticsearch.search.rescore.RescoreBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder)

Example 15 with Builder

use of org.elasticsearch.common.settings.Settings.Builder in project elasticsearch by elastic.

the class QueryRescorerIT method indexRandomNumbers.

private int indexRandomNumbers(String analyzer, int shards, boolean dummyDocs) throws Exception {
    Builder builder = Settings.builder().put(indexSettings());
    if (shards > 0) {
        builder.put(SETTING_NUMBER_OF_SHARDS, shards);
    }
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("analyzer", analyzer).field("type", "text").endObject().endObject().endObject().endObject()).setSettings(builder));
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i));
    }
    indexRandom(true, dummyDocs, docs);
    ensureGreen();
    return numDocs;
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RescoreBuilder(org.elasticsearch.search.rescore.RescoreBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) QueryRescorerBuilder(org.elasticsearch.search.rescore.QueryRescorerBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder)

Aggregations

Builder (org.elasticsearch.common.settings.Settings.Builder)21 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)13 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)8 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 XContentFactory.jsonBuilder (org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)8 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)8 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Settings (org.elasticsearch.common.settings.Settings)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 QueryRescorerBuilder (org.elasticsearch.search.rescore.QueryRescorerBuilder)4 RescoreBuilder (org.elasticsearch.search.rescore.RescoreBuilder)4 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)3 TransportClient (org.elasticsearch.client.transport.TransportClient)3 DiskThresholdSettings (org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings)3 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)3 RecoverySettings (org.elasticsearch.indices.recovery.RecoverySettings)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 TransportAddress (org.elasticsearch.common.transport.TransportAddress)2