test: Stop using the `mapping.single_type` setting in percolator tests.

Closes #24958
This commit is contained in:
Martijn van Groningen 2017-05-30 16:31:54 +02:00
parent 36a180ec20
commit 6945d7b046
No known key found for this signature in database
GPG Key ID: AB236F4FCF2AF12A
2 changed files with 73 additions and 70 deletions

View File

@ -79,6 +79,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
@ -101,7 +102,6 @@ import static org.hamcrest.Matchers.instanceOf;
public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
private String typeName;
private String fieldName; private String fieldName;
private IndexService indexService; private IndexService indexService;
private MapperService mapperService; private MapperService mapperService;
@ -114,10 +114,10 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
@Before @Before
public void init() throws Exception { public void init() throws Exception {
indexService = createIndex("test", Settings.builder().put("mapping.single_type", false).build()); indexService = createIndex("test");
mapperService = indexService.mapperService(); 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("_field_names").field("enabled", false).endObject() // makes testing easier
.startObject("properties") .startObject("properties")
.startObject("field").field("type", "text").endObject() .startObject("field").field("type", "text").endObject()
@ -128,28 +128,27 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
.startObject("number_field").field("type", "long").endObject() .startObject("number_field").field("type", "long").endObject()
.startObject("date_field").field("type", "date").endObject() .startObject("date_field").field("type", "date").endObject()
.endObject().endObject().endObject().string(); .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 { private void addQueryFieldMappings() throws Exception {
typeName = randomAlphaOfLength(4);
fieldName = randomAlphaOfLength(4); 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() .startObject("properties").startObject(fieldName).field("type", "percolator").endObject().endObject()
.endObject().endObject().string(); .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); fieldType = (PercolatorFieldMapper.FieldType) mapperService.fullName(fieldName);
} }
public void testExtractTerms() throws Exception { public void testExtractTerms() throws Exception {
addQueryMapping(); addQueryFieldMappings();
BooleanQuery.Builder bq = new BooleanQuery.Builder(); BooleanQuery.Builder bq = new BooleanQuery.Builder();
TermQuery termQuery1 = new TermQuery(new Term("field", "term1")); TermQuery termQuery1 = new TermQuery(new Term("field", "term1"));
bq.add(termQuery1, BooleanClause.Occur.SHOULD); bq.add(termQuery1, BooleanClause.Occur.SHOULD);
TermQuery termQuery2 = new TermQuery(new Term("field", "term2")); TermQuery termQuery2 = new TermQuery(new Term("field", "term2"));
bq.add(termQuery2, BooleanClause.Occur.SHOULD); bq.add(termQuery2, BooleanClause.Occur.SHOULD);
DocumentMapper documentMapper = mapperService.documentMapper(typeName); DocumentMapper documentMapper = mapperService.documentMapper("doc");
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY,
mapperService.documentMapperParser(), documentMapper, null, null); mapperService.documentMapperParser(), documentMapper, null, null);
@ -159,16 +158,16 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE)); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
List<IndexableField> fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name()))); List<IndexableField> 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.size(), equalTo(2));
assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1")); assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1"));
assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2")); assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2"));
} }
public void testExtractTermsAndRanges_failed() throws Exception { public void testExtractTermsAndRanges_failed() throws Exception {
addQueryMapping(); addQueryFieldMappings();
TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true); 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); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY,
mapperService.documentMapperParser(), documentMapper, null, null); mapperService.documentMapperParser(), documentMapper, null, null);
@ -181,9 +180,9 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testExtractTermsAndRanges_partial() throws Exception { public void testExtractTermsAndRanges_partial() throws Exception {
addQueryMapping(); addQueryFieldMappings();
PhraseQuery phraseQuery = new PhraseQuery("field", "term"); PhraseQuery phraseQuery = new PhraseQuery("field", "term");
DocumentMapper documentMapper = mapperService.documentMapper(typeName); DocumentMapper documentMapper = mapperService.documentMapper("doc");
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY,
mapperService.documentMapperParser(), documentMapper, null, null); mapperService.documentMapperParser(), documentMapper, null, null);
@ -197,7 +196,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testCreateCandidateQuery() throws Exception { public void testCreateCandidateQuery() throws Exception {
addQueryMapping(); addQueryFieldMappings();
MemoryIndex memoryIndex = new MemoryIndex(false); MemoryIndex memoryIndex = new MemoryIndex(false);
memoryIndex.addField("field1", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer()); 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 { public void testPercolatorFieldMapper() throws Exception {
addQueryMapping(); addQueryFieldMappings();
QueryBuilder queryBuilder = termQuery("field", "value"); 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() .jsonBuilder()
.startObject() .startObject()
.field(fieldName, queryBuilder) .field(fieldName, queryBuilder)
@ -262,7 +261,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
// add an query for which we don't extract terms from // add an query for which we don't extract terms from
queryBuilder = rangeQuery("field").from("a").to("z"); 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() .jsonBuilder()
.startObject() .startObject()
.field(fieldName, queryBuilder) .field(fieldName, queryBuilder)
@ -278,7 +277,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testStoringQueries() throws Exception { public void testStoringQueries() throws Exception {
addQueryMapping(); addQueryFieldMappings();
QueryBuilder[] queries = new QueryBuilder[]{ QueryBuilder[] queries = new QueryBuilder[]{
termQuery("field", "value"), matchAllQuery(), matchQuery("field", "value"), matchPhraseQuery("field", "value"), termQuery("field", "value"), matchAllQuery(), matchQuery("field", "value"), matchPhraseQuery("field", "value"),
prefixQuery("field", "v"), wildcardQuery("field", "v*"), rangeQuery("number_field").gte(0).lte(9), 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) // (it can't use shard data for rewriting purposes, because percolator queries run on MemoryIndex)
for (QueryBuilder query : queries) { 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() XContentFactory.jsonBuilder().startObject()
.field(fieldName, query) .field(fieldName, query)
.endObject().bytes(), .endObject().bytes(),
@ -299,10 +298,10 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testQueryWithRewrite() throws Exception { public void testQueryWithRewrite() throws Exception {
addQueryMapping(); addQueryFieldMappings();
client().prepareIndex("remote", "type", "1").setSource("field", "value").get(); client().prepareIndex("remote", "doc", "1").setSource("field", "value").get();
QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "type", "1", "field")); QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "doc", "1", "field"));
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() .jsonBuilder()
.startObject() .startObject()
.field(fieldName, queryBuilder) .field(fieldName, queryBuilder)
@ -315,9 +314,9 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
public void testPercolatorFieldMapperUnMappedField() throws Exception { public void testPercolatorFieldMapperUnMappedField() throws Exception {
addQueryMapping(); addQueryFieldMappings();
MapperParsingException exception = expectThrows(MapperParsingException.class, () -> { 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() .jsonBuilder()
.startObject() .startObject()
.field(fieldName, termQuery("unmapped_field", "value")) .field(fieldName, termQuery("unmapped_field", "value"))
@ -330,8 +329,8 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
public void testPercolatorFieldMapper_noQuery() throws Exception { public void testPercolatorFieldMapper_noQuery() throws Exception {
addQueryMapping(); addQueryFieldMappings();
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() .jsonBuilder()
.startObject() .startObject()
.endObject() .endObject()
@ -340,7 +339,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0)); assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0));
try { try {
mapperService.documentMapper(typeName).parse(SourceToParse.source("test", typeName, "1", XContentFactory mapperService.documentMapper("doc").parse(SourceToParse.source("test", "doc", "1", XContentFactory
.jsonBuilder() .jsonBuilder()
.startObject() .startObject()
.nullField(fieldName) .nullField(fieldName)
@ -352,21 +351,21 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testAllowNoAdditionalSettings() throws Exception { public void testAllowNoAdditionalSettings() throws Exception {
addQueryMapping(); addQueryFieldMappings();
IndexService indexService = createIndex("test1", Settings.EMPTY); IndexService indexService = createIndex("test1", Settings.EMPTY);
MapperService mapperService = indexService.mapperService(); 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() .startObject("properties").startObject(fieldName).field("type", "percolator").field("index", "no").endObject().endObject()
.endObject().endObject().string(); .endObject().endObject().string();
MapperParsingException e = expectThrows(MapperParsingException.class, () -> 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]")); 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. // multiple percolator fields are allowed in the mapping, but only one field can be used at index time.
public void testMultiplePercolatorFields() throws Exception { public void testMultiplePercolatorFields() throws Exception {
String typeName = "another_type"; String typeName = "doc";
String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName)
.startObject("_field_names").field("enabled", false).endObject() // makes testing easier .startObject("_field_names").field("enabled", false).endObject() // makes testing easier
.startObject("properties") .startObject("properties")
@ -383,7 +382,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
.field("query_field2", queryBuilder) .field("query_field2", queryBuilder)
.endObject().bytes(), .endObject().bytes(),
XContentType.JSON)); 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(); BytesRef queryBuilderAsBytes = doc.rootDoc().getField("query_field1.query_builder_field").binaryValue();
assertQueryBuilder(queryBuilderAsBytes, queryBuilder); 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 // percolator field can be nested under an object field, but only one query can be specified per document
public void testNestedPercolatorField() throws Exception { public void testNestedPercolatorField() throws Exception {
String typeName = "another_type"; String typeName = "doc";
String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName) String percolatorMapper = XContentFactory.jsonBuilder().startObject().startObject(typeName)
.startObject("_field_names").field("enabled", false).endObject() // makes testing easier .startObject("_field_names").field("enabled", false).endObject() // makes testing easier
.startObject("properties") .startObject("properties")
@ -413,7 +412,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
.field("query_field", queryBuilder) .field("query_field", queryBuilder)
.endObject().endObject().bytes(), .endObject().endObject().bytes(),
XContentType.JSON)); 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(); BytesRef queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
assertQueryBuilder(queryBuilderAsBytes, queryBuilder); assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
@ -424,7 +423,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
.endArray() .endArray()
.endObject().bytes(), .endObject().bytes(),
XContentType.JSON)); 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(); queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
assertQueryBuilder(queryBuilderAsBytes, queryBuilder); assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
@ -494,7 +493,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
} }
public void testImplicitlySetDefaultScriptLang() throws Exception { public void testImplicitlySetDefaultScriptLang() throws Exception {
addQueryMapping(); addQueryFieldMappings();
XContentBuilder query = jsonBuilder(); XContentBuilder query = jsonBuilder();
query.startObject(); query.startObject();
query.startObject("script"); query.startObject("script");
@ -508,7 +507,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
query.endObject(); query.endObject();
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() XContentFactory.jsonBuilder().startObject()
.rawField(fieldName, new BytesArray(query.string()), query.contentType()) .rawField(fieldName, new BytesArray(query.string()), query.contentType())
.endObject().bytes(), .endObject().bytes(),
@ -536,7 +535,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
query.endObject(); query.endObject();
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() XContentFactory.jsonBuilder().startObject()
.rawField(fieldName, new BytesArray(query.string()), query.contentType()) .rawField(fieldName, new BytesArray(query.string()), query.contentType())
.endObject().bytes(), .endObject().bytes(),

View File

@ -484,35 +484,39 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
public void testManyPercolatorFields() throws Exception { public void testManyPercolatorFields() throws Exception {
String queryFieldName = randomAlphaOfLength(8); String queryFieldName = randomAlphaOfLength(8);
createIndex("test", client().admin().indices().prepareCreate("test") createIndex("test1", client().admin().indices().prepareCreate("test1")
.setSettings("index.mapping.single_type", false) .addMapping("type", queryFieldName, "type=percolator", "field", "type=keyword")
.addMapping("doc_type", "field", "type=keyword") );
.addMapping("query_type1", queryFieldName, "type=percolator") createIndex("test2", client().admin().indices().prepareCreate("test2")
.addMapping("query_type2", queryFieldName, "type=percolator", "second_query_field", "type=percolator") .addMapping("type", queryFieldName, "type=percolator", "second_query_field", "type=percolator", "field", "type=keyword")
.addMapping("query_type3", jsonBuilder().startObject().startObject("query_type3").startObject("properties") );
.startObject("object_field") createIndex("test3", client().admin().indices().prepareCreate("test3")
.field("type", "object") .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("properties") .startObject("field")
.startObject(queryFieldName) .field("type", "keyword")
.field("type", "percolator") .endObject()
.endObject() .startObject("object_field")
.endObject() .field("type", "object")
.endObject() .startObject("properties")
.endObject() .startObject(queryFieldName)
.endObject().endObject()) .field("type", "percolator")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject().endObject())
); );
} }
public void testWithMultiplePercolatorFields() throws Exception { public void testWithMultiplePercolatorFields() throws Exception {
String queryFieldName = randomAlphaOfLength(8); String queryFieldName = randomAlphaOfLength(8);
createIndex("test1", client().admin().indices().prepareCreate("test1") createIndex("test1", client().admin().indices().prepareCreate("test1")
.setSettings("index.mapping.single_type", false) .addMapping("type", queryFieldName, "type=percolator", "field", "type=keyword"));
.addMapping("doc_type", "field", "type=keyword")
.addMapping("query_type", queryFieldName, "type=percolator"));
createIndex("test2", client().admin().indices().prepareCreate("test2") createIndex("test2", client().admin().indices().prepareCreate("test2")
.setSettings("index.mapping.single_type", false) .addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties")
.addMapping("doc_type", "field", "type=keyword") .startObject("field")
.addMapping("query_type", jsonBuilder().startObject().startObject("query_type").startObject("properties") .field("type", "keyword")
.endObject()
.startObject("object_field") .startObject("object_field")
.field("type", "object") .field("type", "object")
.startObject("properties") .startObject("properties")
@ -526,10 +530,10 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
); );
// Acceptable: // Acceptable:
client().prepareIndex("test1", "query_type", "1") client().prepareIndex("test1", "type", "1")
.setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject()) .setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject())
.get(); .get();
client().prepareIndex("test2", "query_type", "1") client().prepareIndex("test2", "type", "1")
.setSource(jsonBuilder().startObject().startObject("object_field") .setSource(jsonBuilder().startObject().startObject("object_field")
.field(queryFieldName, matchQuery("field", "value")) .field(queryFieldName, matchQuery("field", "value"))
.endObject().endObject()) .endObject().endObject())
@ -538,26 +542,26 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
BytesReference source = jsonBuilder().startObject().field("field", "value").endObject().bytes(); BytesReference source = jsonBuilder().startObject().field("field", "value").endObject().bytes();
SearchResponse response = client().prepareSearch() SearchResponse response = client().prepareSearch()
.setQuery(new PercolateQueryBuilder(queryFieldName, "doc_type", source, XContentType.JSON)) .setQuery(new PercolateQueryBuilder(queryFieldName, "type", source, XContentType.JSON))
.setIndices("test1") .setIndices("test1")
.get(); .get();
assertHitCount(response, 1); assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("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")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test1"));
response = client().prepareSearch() 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") .setIndices("test2")
.get(); .get();
assertHitCount(response, 1); assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("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")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test2"));
// Unacceptable: // Unacceptable:
MapperParsingException e = expectThrows(MapperParsingException.class, () -> { MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
client().prepareIndex("test2", "query_type", "1") client().prepareIndex("test2", "type", "1")
.setSource(jsonBuilder().startObject().startArray("object_field") .setSource(jsonBuilder().startObject().startArray("object_field")
.startObject().field(queryFieldName, matchQuery("field", "value")).endObject() .startObject().field(queryFieldName, matchQuery("field", "value")).endObject()
.startObject().field(queryFieldName, matchQuery("field", "value")).endObject() .startObject().field(queryFieldName, matchQuery("field", "value")).endObject()