Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
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;
Expand All @@ -101,7 +102,6 @@

public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {

private String typeName;
private String fieldName;
private IndexService indexService;
private MapperService mapperService;
Expand All @@ -114,10 +114,10 @@ protected Collection<Class<? extends Plugin>> getPlugins() {

@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()
Expand All @@ -128,28 +128,27 @@ public void init() throws Exception {
.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);
Expand All @@ -159,16 +158,16 @@ public void testExtractTerms() throws Exception {
PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
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.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);
Expand All @@ -181,9 +180,9 @@ public void testExtractTermsAndRanges_failed() throws Exception {
}

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);
Expand All @@ -197,7 +196,7 @@ public void testExtractTermsAndRanges_partial() throws Exception {
}

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());
Expand Down Expand Up @@ -242,9 +241,9 @@ private void assertTermIterator(PrefixCodedTerms.TermIterator termIterator, Stri
}

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)
Expand All @@ -262,7 +261,7 @@ public void testPercolatorFieldMapper() throws Exception {

// 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)
Expand All @@ -278,7 +277,7 @@ public void testPercolatorFieldMapper() throws Exception {
}

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),
Expand All @@ -288,7 +287,7 @@ public void testStoringQueries() throws Exception {
// (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(),
Expand All @@ -299,10 +298,10 @@ public void testStoringQueries() throws Exception {
}

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)
Expand All @@ -315,9 +314,9 @@ public void testQueryWithRewrite() throws Exception {


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"))
Expand All @@ -330,8 +329,8 @@ public void testPercolatorFieldMapperUnMappedField() throws Exception {


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()
Expand All @@ -340,7 +339,7 @@ public void testPercolatorFieldMapper_noQuery() throws Exception {
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)
Expand All @@ -352,21 +351,21 @@ public void testPercolatorFieldMapper_noQuery() throws Exception {
}

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")
Expand All @@ -383,7 +382,7 @@ public void testMultiplePercolatorFields() throws Exception {
.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);

Expand All @@ -393,7 +392,7 @@ public void testMultiplePercolatorFields() throws Exception {

// 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")
Expand All @@ -413,7 +412,7 @@ public void testNestedPercolatorField() throws Exception {
.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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious, why is it less now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because with a single type, the _type indexed and stored fields are left out.

BytesRef queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
assertQueryBuilder(queryBuilderAsBytes, queryBuilder);

Expand All @@ -424,7 +423,7 @@ public void testNestedPercolatorField() throws Exception {
.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);

Expand Down Expand Up @@ -494,7 +493,7 @@ public void testEmptyName() throws Exception {
}

public void testImplicitlySetDefaultScriptLang() throws Exception {
addQueryMapping();
addQueryFieldMappings();
XContentBuilder query = jsonBuilder();
query.startObject();
query.startObject("script");
Expand All @@ -508,7 +507,7 @@ public void testImplicitlySetDefaultScriptLang() throws Exception {
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(),
Expand Down Expand Up @@ -536,7 +535,7 @@ public void testImplicitlySetDefaultScriptLang() throws Exception {
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(),
Expand Down
Loading