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);
}
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);
}
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());
}
}
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));
}
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());
}
}
Aggregations