Search in sources :

Example 36 with Settings

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

the class GetTermVectorsIT method testArtificialDocWithPreference.

public void testArtificialDocWithPreference() throws ExecutionException, InterruptedException, IOException {
    // setup indices
    Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", "field1", "type=text,term_vector=with_positions_offsets"));
    ensureGreen();
    // index document
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "random permutation"));
    // Get search shards
    ClusterSearchShardsResponse searchShardsResponse = client().admin().cluster().prepareSearchShards("test").get();
    List<Integer> shardIds = Arrays.stream(searchShardsResponse.getGroups()).map(s -> s.getShardId().id()).collect(Collectors.toList());
    // request termvectors of artificial document from each shard
    int sumTotalTermFreq = 0;
    int sumDocFreq = 0;
    for (Integer shardId : shardIds) {
        TermVectorsResponse tvResponse = client().prepareTermVectors().setIndex("test").setType("type1").setPreference("_shards:" + shardId).setDoc(jsonBuilder().startObject().field("field1", "random permutation").endObject()).setFieldStatistics(true).setTermStatistics(true).get();
        Fields fields = tvResponse.getFields();
        Terms terms = fields.terms("field1");
        assertNotNull(terms);
        TermsEnum termsEnum = terms.iterator();
        while (termsEnum.next() != null) {
            sumTotalTermFreq += termsEnum.totalTermFreq();
            sumDocFreq += termsEnum.docFreq();
        }
    }
    assertEquals("expected to find term statistics in exactly one shard!", 2, sumTotalTermFreq);
    assertEquals("expected to find term statistics in exactly one shard!", 2, sumDocFreq);
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) Versions(org.elasticsearch.common.lucene.uid.Versions) Arrays(java.util.Arrays) FieldType(org.apache.lucene.document.FieldType) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Fields(org.apache.lucene.index.Fields) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) PayloadHelper(org.apache.lucene.analysis.payloads.PayloadHelper) ActionFuture(org.elasticsearch.action.ActionFuture) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Settings(org.elasticsearch.common.settings.Settings) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) TermsEnum(org.apache.lucene.index.TermsEnum) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) ElasticsearchAssertions.assertThrows(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) DirectoryReader(org.apache.lucene.index.DirectoryReader) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) ElasticsearchAssertions.assertAcked(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked) Fields(org.apache.lucene.index.Fields) Terms(org.apache.lucene.index.Terms) Settings(org.elasticsearch.common.settings.Settings) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 37 with Settings

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

the class GetTermVectorsIT method testPerFieldAnalyzer.

public void testPerFieldAnalyzer() throws IOException {
    int numFields = 25;
    // setup mapping and document source
    Set<String> withTermVectors = new HashSet<>();
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties");
    XContentBuilder source = jsonBuilder().startObject();
    for (int i = 0; i < numFields; i++) {
        String fieldName = "field" + i;
        if (randomBoolean()) {
            withTermVectors.add(fieldName);
        }
        mapping.startObject(fieldName).field("type", "text").field("term_vector", withTermVectors.contains(fieldName) ? "yes" : "no").endObject();
        source.field(fieldName, "some text here");
    }
    source.endObject();
    mapping.endObject().endObject().endObject();
    // setup indices with mapping
    Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(settings).addMapping("type1", mapping));
    ensureGreen();
    // index a single document with prepared source
    client().prepareIndex("test", "type1", "0").setSource(source).get();
    refresh();
    // create random per_field_analyzer and selected fields
    Map<String, String> perFieldAnalyzer = new HashMap<>();
    Set<String> selectedFields = new HashSet<>();
    for (int i = 0; i < numFields; i++) {
        if (randomBoolean()) {
            perFieldAnalyzer.put("field" + i, "keyword");
        }
        if (randomBoolean()) {
            perFieldAnalyzer.put("non_existing" + i, "keyword");
        }
        if (randomBoolean()) {
            selectedFields.add("field" + i);
        }
        if (randomBoolean()) {
            selectedFields.add("non_existing" + i);
        }
    }
    // selected fields not specified
    TermVectorsResponse response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setPerFieldAnalyzer(perFieldAnalyzer).get();
    // should return all fields that have terms vectors, some with overridden analyzer
    checkAnalyzedFields(response.getFields(), withTermVectors, perFieldAnalyzer);
    // selected fields specified including some not in the mapping
    response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setSelectedFields(selectedFields.toArray(Strings.EMPTY_ARRAY)).setPerFieldAnalyzer(perFieldAnalyzer).get();
    // should return only the specified valid fields, with some with overridden analyzer
    checkAnalyzedFields(response.getFields(), selectedFields, perFieldAnalyzer);
}
Also used : HashMap(java.util.HashMap) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) Alias(org.elasticsearch.action.admin.indices.alias.Alias) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Example 38 with Settings

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

the class GetTermVectorsIT method testArtificialNoDoc.

public void testArtificialNoDoc() throws IOException {
    // setup indices
    Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", "field1", "type=text"));
    ensureGreen();
    // request tvs from artificial document
    String text = "the quick brown fox jumps over the lazy dog";
    TermVectorsResponse resp = client().prepareTermVectors().setIndex("test").setType("type1").setDoc(jsonBuilder().startObject().field("field1", text).endObject()).setOffsets(true).setPositions(true).setFieldStatistics(true).setTermStatistics(true).get();
    assertThat(resp.isExists(), equalTo(true));
    checkBrownFoxTermVector(resp.getFields(), "field1", false);
    // Since the index is empty, all of artificial document's "term_statistics" should be 0/absent
    Terms terms = resp.getFields().terms("field1");
    assertEquals("sumDocFreq should be 0 for a non-existing field!", 0, terms.getSumDocFreq());
    assertEquals("sumTotalTermFreq should be 0 for a non-existing field!", 0, terms.getSumTotalTermFreq());
    // we're guaranteed to receive terms for that field
    TermsEnum termsEnum = terms.iterator();
    while (termsEnum.next() != null) {
        String term = termsEnum.term().utf8ToString();
        assertEquals("term [" + term + "] does not exist in the index; ttf should be 0!", 0, termsEnum.totalTermFreq());
    }
}
Also used : Terms(org.apache.lucene.index.Terms) Settings(org.elasticsearch.common.settings.Settings) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 39 with Settings

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

the class AutoCreateIndexTests method testAutoCreationConflictingPatternsFirstWins.

public void testAutoCreationConflictingPatternsFirstWins() {
    Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "+test1,-test1,-test2,+test2").build();
    AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
    ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
    assertThat(autoCreateIndex.shouldAutoCreate("test1", clusterState), equalTo(true));
    expectForbidden(clusterState, autoCreateIndex, "test2", "-test2");
    expectNotMatch(clusterState, autoCreateIndex, "does_not_match" + randomAsciiOfLengthBetween(1, 5));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterName(org.elasticsearch.cluster.ClusterName) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 40 with Settings

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

the class AutoCreateIndexTests method testParseFailed.

public void testParseFailed() {
    try {
        Settings settings = Settings.builder().put("action.auto_create_index", ",,,").build();
        newAutoCreateIndex(settings);
        fail("initialization should have failed");
    } catch (IllegalArgumentException ex) {
        assertEquals("Can't parse [,,,] for setting [action.auto_create_index] must be either [true, false, or a " + "comma separated list of index patterns]", ex.getMessage());
    }
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

Settings (org.elasticsearch.common.settings.Settings)874 IndexSettings (org.elasticsearch.index.IndexSettings)112 Path (java.nio.file.Path)91 IOException (java.io.IOException)83 ClusterState (org.elasticsearch.cluster.ClusterState)76 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)72 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)68 HashMap (java.util.HashMap)66 ArrayList (java.util.ArrayList)65 Version (org.elasticsearch.Version)63 Environment (org.elasticsearch.env.Environment)63 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)61 Test (org.junit.Test)60 Map (java.util.Map)55 Index (org.elasticsearch.index.Index)55 Matchers.containsString (org.hamcrest.Matchers.containsString)54 List (java.util.List)45 ThreadPool (org.elasticsearch.threadpool.ThreadPool)41 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)37 MetaData (org.elasticsearch.cluster.metadata.MetaData)36