Search in sources :

Example 1 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class MultiMatchQueryIT method testFuzzyFieldLevelBoosting.

/**
     * Test for edge case where field level boosting is applied to field that doesn't exist on documents on
     * one shard. There was an issue reported in https://github.com/elastic/elasticsearch/issues/18710 where a
     * `multi_match` query using the fuzziness parameter with a boost on one of two fields returns the
     * same document score if both documents are placed on different shard. This test recreates that scenario
     * and checks that the returned scores are different.
     */
public void testFuzzyFieldLevelBoosting() throws InterruptedException, ExecutionException {
    String idx = "test18710";
    CreateIndexRequestBuilder builder = prepareCreate(idx).setSettings(Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 3).put(SETTING_NUMBER_OF_REPLICAS, 0));
    assertAcked(builder.addMapping("type", "title", "type=text", "body", "type=text"));
    ensureGreen();
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex(idx, "type", "1").setSource("title", "foo", "body", "bar"));
    builders.add(client().prepareIndex(idx, "type", "2").setSource("title", "bar", "body", "foo"));
    indexRandom(true, false, builders);
    SearchResponse searchResponse = client().prepareSearch(idx).setExplain(true).setQuery(multiMatchQuery("foo").field("title", 100).field("body").fuzziness(0)).get();
    SearchHit[] hits = searchResponse.getHits().getHits();
    assertNotEquals("both documents should be on different shards", hits[0].getShard().getShardId(), hits[1].getShard().getShardId());
    assertEquals("1", hits[0].getId());
    assertEquals("2", hits[1].getId());
    assertThat(hits[0].getScore(), greaterThan(hits[1].getScore()));
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SearchAfterIT method createIndexMappingsFromObjectType.

private void createIndexMappingsFromObjectType(String indexName, String typeName, List<Object> types) {
    CreateIndexRequestBuilder indexRequestBuilder = client().admin().indices().prepareCreate(indexName);
    List<String> mappings = new ArrayList<>();
    int numFields = types.size();
    for (int i = 0; i < numFields; i++) {
        Class type = types.get(i).getClass();
        if (type == Integer.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=integer");
        } else if (type == Long.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=long");
        } else if (type == Float.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=float");
        } else if (type == Double.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=double");
        } else if (type == Byte.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=byte");
        } else if (type == Short.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=short");
        } else if (type == Boolean.class) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=boolean");
        } else if (types.get(i) instanceof String) {
            mappings.add("field" + Integer.toString(i));
            mappings.add("type=keyword");
        } else {
            fail("Can't match type [" + type + "]");
        }
    }
    indexRequestBuilder.addMapping(typeName, mappings.toArray()).get();
    ensureGreen();
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList)

Example 3 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testQueryStringWithSynonyms.

public void testQueryStringWithSynonyms() throws IOException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.index.type", "custom").put("index.analysis.analyzer.index.tokenizer", "standard").put("index.analysis.analyzer.index.filter", "lowercase").put("index.analysis.analyzer.search.type", "custom").put("index.analysis.analyzer.search.tokenizer", "standard").putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym").put("index.analysis.filter.synonym.type", "synonym").putArray("index.analysis.filter.synonym.synonyms", "fast, quick"));
    assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search"));
    client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick").defaultField("text").defaultOperator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick brown").defaultField("text").defaultOperator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    searchResponse = client().prepareSearch().setQuery(queryStringQuery("fast").defaultField("text").defaultOperator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get();
    refresh();
    searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick").defaultField("text").defaultOperator(Operator.AND)).get();
    assertHitCount(searchResponse, 2);
    searchResponse = client().prepareSearch("test").setQuery(queryStringQuery("quick brown").defaultField("text").defaultOperator(Operator.AND)).get();
    assertHitCount(searchResponse, 2);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testNGramCopyField.

// see #5120
public void testNGramCopyField() {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.my_ngram_analyzer.type", "custom").put("index.analysis.analyzer.my_ngram_analyzer.tokenizer", "my_ngram_tokenizer").put("index.analysis.tokenizer.my_ngram_tokenizer.type", "nGram").put("index.analysis.tokenizer.my_ngram_tokenizer.min_gram", "1").put("index.analysis.tokenizer.my_ngram_tokenizer.max_gram", "10").putArray("index.analysis.tokenizer.my_ngram_tokenizer.token_chars", new String[0]));
    assertAcked(builder.addMapping("test", "origin", "type=text,copy_to=meta", "meta", "type=text,analyzer=my_ngram_analyzer"));
    // we only have ngrams as the index analyzer so searches will get standard analyzer
    client().prepareIndex("test", "test", "1").setSource("origin", "C.A1234.5678").setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "1234")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "1234.56")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(termQuery("meta", "A1234")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(termQuery("meta", "a1234")).get();
    // it's upper case
    assertHitCount(searchResponse, 0L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "A1234").analyzer("my_ngram_analyzer")).get();
    assertHitCount(searchResponse, 1L);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("meta", "a1234").analyzer("my_ngram_analyzer")).get();
    assertHitCount(searchResponse, 1L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SimpleQueryStringIT method testEmptySimpleQueryStringWithAnalysis.

public void testEmptySimpleQueryStringWithAnalysis() throws Exception {
    // https://github.com/elastic/elasticsearch/issues/18202
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("body").field("type", "text").field("analyzer", "stop").endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("test1").addMapping("type1", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("body", "Some Text"));
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("the*").field("body")).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 0L);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)30 SearchResponse (org.elasticsearch.action.search.SearchResponse)11 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)11 ArrayList (java.util.ArrayList)8 PhraseSuggestionBuilder (org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Before (org.junit.Before)3 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 DirectCandidateGeneratorBuilder (org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder)2 StupidBackoff (org.elasticsearch.search.suggest.phrase.StupidBackoff)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1