diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java index 5a150349ed2..110b4bf30a0 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java @@ -79,6 +79,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -101,7 +102,6 @@ import static org.hamcrest.Matchers.instanceOf; public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { - private String typeName; private String fieldName; private IndexService indexService; private MapperService mapperService; @@ -114,10 +114,10 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { @Before public void init() throws Exception { - indexService = createIndex("test", Settings.builder().put("mapping.single_type", false).build()); + indexService = createIndex("test"); mapperService = indexService.mapperService(); - String mapper = XContentFactory.jsonBuilder().startObject().startObject("type") + String mapper = XContentFactory.jsonBuilder().startObject().startObject("doc") .startObject("_field_names").field("enabled", false).endObject() // makes testing easier .startObject("properties") .startObject("field").field("type", "text").endObject() @@ -128,28 +128,27 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { .startObject("number_field").field("type", "long").endObject() .startObject("date_field").field("type", "date").endObject() .endObject().endObject().endObject().string(); - mapperService.merge("type", new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE, true); + mapperService.merge("doc", new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE, false); } - private void addQueryMapping() throws Exception { - typeName = randomAlphaOfLength(4); + private void addQueryFieldMappings() throws Exception { fieldName = randomAlphaOfLength(4); - String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) + String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject("doc") .startObject("properties").startObject(fieldName).field("type", "percolator").endObject().endObject() .endObject().endObject().string(); - mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE, true); + mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE, false); fieldType = (PercolatorFieldMapper.FieldType) mapperService.fullName(fieldName); } public void testExtractTerms() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); BooleanQuery.Builder bq = new BooleanQuery.Builder(); TermQuery termQuery1 = new TermQuery(new Term("field", "term1")); bq.add(termQuery1, BooleanClause.Occur.SHOULD); TermQuery termQuery2 = new TermQuery(new Term("field", "term2")); bq.add(termQuery2, BooleanClause.Occur.SHOULD); - DocumentMapper documentMapper = mapperService.documentMapper(typeName); + DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null); @@ -159,16 +158,16 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE)); List fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name()))); - Collections.sort(fields, (field1, field2) -> field1.binaryValue().compareTo(field2.binaryValue())); + fields.sort(Comparator.comparing(IndexableField::binaryValue)); assertThat(fields.size(), equalTo(2)); assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1")); assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2")); } public void testExtractTermsAndRanges_failed() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true); - DocumentMapper documentMapper = mapperService.documentMapper(typeName); + DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null); @@ -181,9 +180,9 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testExtractTermsAndRanges_partial() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); PhraseQuery phraseQuery = new PhraseQuery("field", "term"); - DocumentMapper documentMapper = mapperService.documentMapper(typeName); + DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null); @@ -197,7 +196,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testCreateCandidateQuery() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); MemoryIndex memoryIndex = new MemoryIndex(false); memoryIndex.addField("field1", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer()); @@ -242,9 +241,9 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testPercolatorFieldMapper() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); QueryBuilder queryBuilder = termQuery("field", "value"); - ParsedDocument doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + ParsedDocument doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .field(fieldName, queryBuilder) @@ -262,7 +261,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { // add an query for which we don't extract terms from queryBuilder = rangeQuery("field").from("a").to("z"); - doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .field(fieldName, queryBuilder) @@ -278,7 +277,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testStoringQueries() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); QueryBuilder[] queries = new QueryBuilder[]{ termQuery("field", "value"), matchAllQuery(), matchQuery("field", "value"), matchPhraseQuery("field", "value"), prefixQuery("field", "v"), wildcardQuery("field", "v*"), rangeQuery("number_field").gte(0).lte(9), @@ -288,7 +287,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { // (it can't use shard data for rewriting purposes, because percolator queries run on MemoryIndex) for (QueryBuilder query : queries) { - ParsedDocument doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", + ParsedDocument doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory.jsonBuilder().startObject() .field(fieldName, query) .endObject().bytes(), @@ -299,10 +298,10 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testQueryWithRewrite() throws Exception { - addQueryMapping(); - client().prepareIndex("remote", "type", "1").setSource("field", "value").get(); - QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "type", "1", "field")); - ParsedDocument doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + addQueryFieldMappings(); + client().prepareIndex("remote", "doc", "1").setSource("field", "value").get(); + QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "doc", "1", "field")); + ParsedDocument doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .field(fieldName, queryBuilder) @@ -315,9 +314,9 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { public void testPercolatorFieldMapperUnMappedField() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); MapperParsingException exception = expectThrows(MapperParsingException.class, () -> { - mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .field(fieldName, termQuery("unmapped_field", "value")) @@ -330,8 +329,8 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { public void testPercolatorFieldMapper_noQuery() throws Exception { - addQueryMapping(); - ParsedDocument doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + addQueryFieldMappings(); + ParsedDocument doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .endObject() @@ -340,7 +339,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0)); try { - mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory + mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory .jsonBuilder() .startObject() .nullField(fieldName) @@ -352,21 +351,21 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testAllowNoAdditionalSettings() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); IndexService indexService = createIndex("test1", Settings.EMPTY); MapperService mapperService = indexService.mapperService(); - String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) + String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject("doc") .startObject("properties").startObject(fieldName).field("type", "percolator").field("index", "no").endObject().endObject() .endObject().endObject().string(); MapperParsingException e = expectThrows(MapperParsingException.class, () -> - mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE, true)); + mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE, true)); assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters: [index : no]")); } // multiple percolator fields are allowed in the mapping, but only one field can be used at index time. public void testMultiplePercolatorFields() throws Exception { - String typeName = "another_type"; + String typeName = "doc"; String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) .startObject("_field_names").field("enabled", false).endObject() // makes testing easier .startObject("properties") @@ -383,7 +382,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { .field("query_field2", queryBuilder) .endObject().bytes(), XContentType.JSON)); - assertThat(doc.rootDoc().getFields().size(), equalTo(14)); // also includes all other meta fields + assertThat(doc.rootDoc().getFields().size(), equalTo(12)); // also includes all other meta fields BytesRef queryBuilderAsBytes = doc.rootDoc().getField("query_field1.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); @@ -393,7 +392,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { // percolator field can be nested under an object field, but only one query can be specified per document public void testNestedPercolatorField() throws Exception { - String typeName = "another_type"; + String typeName = "doc"; String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) .startObject("_field_names").field("enabled", false).endObject() // makes testing easier .startObject("properties") @@ -413,7 +412,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { .field("query_field", queryBuilder) .endObject().endObject().bytes(), XContentType.JSON)); - assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields + assertThat(doc.rootDoc().getFields().size(), equalTo(9)); // also includes all other meta fields BytesRef queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); @@ -424,7 +423,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { .endArray() .endObject().bytes(), XContentType.JSON)); - assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields + assertThat(doc.rootDoc().getFields().size(), equalTo(9)); // also includes all other meta fields queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); @@ -494,7 +493,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { } public void testImplicitlySetDefaultScriptLang() throws Exception { - addQueryMapping(); + addQueryFieldMappings(); XContentBuilder query = jsonBuilder(); query.startObject(); query.startObject("script"); @@ -508,7 +507,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { query.endObject(); query.endObject(); - ParsedDocument doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", + ParsedDocument doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory.jsonBuilder().startObject() .rawField(fieldName, new BytesArray(query.string()), query.contentType()) .endObject().bytes(), @@ -536,7 +535,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { query.endObject(); query.endObject(); - doc = mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", + doc = mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory.jsonBuilder().startObject() .rawField(fieldName, new BytesArray(query.string()), query.contentType()) .endObject().bytes(), diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index 21775d2a53a..4f099adb7b7 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -484,35 +484,39 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase { public void testManyPercolatorFields() throws Exception { String queryFieldName = randomAlphaOfLength(8); - createIndex("test", client().admin().indices().prepareCreate("test") - .setSettings("index.mapping.single_type", false) - .addMapping("doc_type", "field", "type=keyword") - .addMapping("query_type1", queryFieldName, "type=percolator") - .addMapping("query_type2", queryFieldName, "type=percolator", "second_query_field", "type=percolator") - .addMapping("query_type3", jsonBuilder().startObject().startObject("query_type3").startObject("properties") - .startObject("object_field") - .field("type", "object") - .startObject("properties") - .startObject(queryFieldName) - .field("type", "percolator") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject().endObject()) + createIndex("test1", client().admin().indices().prepareCreate("test1") + .addMapping("type", queryFieldName, "type=percolator", "field", "type=keyword") + ); + createIndex("test2", client().admin().indices().prepareCreate("test2") + .addMapping("type", queryFieldName, "type=percolator", "second_query_field", "type=percolator", "field", "type=keyword") + ); + createIndex("test3", client().admin().indices().prepareCreate("test3") + .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties") + .startObject("field") + .field("type", "keyword") + .endObject() + .startObject("object_field") + .field("type", "object") + .startObject("properties") + .startObject(queryFieldName) + .field("type", "percolator") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject().endObject()) ); } public void testWithMultiplePercolatorFields() throws Exception { String queryFieldName = randomAlphaOfLength(8); createIndex("test1", client().admin().indices().prepareCreate("test1") - .setSettings("index.mapping.single_type", false) - .addMapping("doc_type", "field", "type=keyword") - .addMapping("query_type", queryFieldName, "type=percolator")); + .addMapping("type", queryFieldName, "type=percolator", "field", "type=keyword")); createIndex("test2", client().admin().indices().prepareCreate("test2") - .setSettings("index.mapping.single_type", false) - .addMapping("doc_type", "field", "type=keyword") - .addMapping("query_type", jsonBuilder().startObject().startObject("query_type").startObject("properties") + .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties") + .startObject("field") + .field("type", "keyword") + .endObject() .startObject("object_field") .field("type", "object") .startObject("properties") @@ -526,10 +530,10 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase { ); // Acceptable: - client().prepareIndex("test1", "query_type", "1") + client().prepareIndex("test1", "type", "1") .setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject()) .get(); - client().prepareIndex("test2", "query_type", "1") + client().prepareIndex("test2", "type", "1") .setSource(jsonBuilder().startObject().startObject("object_field") .field(queryFieldName, matchQuery("field", "value")) .endObject().endObject()) @@ -538,26 +542,26 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase { BytesReference source = jsonBuilder().startObject().field("field", "value").endObject().bytes(); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder(queryFieldName, "doc_type", source, XContentType.JSON)) + .setQuery(new PercolateQueryBuilder(queryFieldName, "type", source, XContentType.JSON)) .setIndices("test1") .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); - assertThat(response.getHits().getAt(0).getType(), equalTo("query_type")); + assertThat(response.getHits().getAt(0).getType(), equalTo("type")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test1")); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("object_field." + queryFieldName, "doc_type", source, XContentType.JSON)) + .setQuery(new PercolateQueryBuilder("object_field." + queryFieldName, "type", source, XContentType.JSON)) .setIndices("test2") .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); - assertThat(response.getHits().getAt(0).getType(), equalTo("query_type")); + assertThat(response.getHits().getAt(0).getType(), equalTo("type")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test2")); // Unacceptable: MapperParsingException e = expectThrows(MapperParsingException.class, () -> { - client().prepareIndex("test2", "query_type", "1") + client().prepareIndex("test2", "type", "1") .setSource(jsonBuilder().startObject().startArray("object_field") .startObject().field(queryFieldName, matchQuery("field", "value")).endObject() .startObject().field(queryFieldName, matchQuery("field", "value")).endObject()