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