use of org.opensearch.index.mapper.ParseContext in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testExtractTerms.
public void testExtractTerms() throws Exception {
addQueryFieldMappings();
BooleanQuery.Builder bq = new BooleanQuery.Builder();
TermQuery termQuery1 = new TermQuery(new Term("field", "term1"));
bq.add(termQuery1, Occur.SHOULD);
TermQuery termQuery2 = new TermQuery(new Term("field", "term2"));
bq.add(termQuery2, Occur.SHOULD);
DocumentMapper documentMapper = mapperService.documentMapper("doc");
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
IndexMetadata build = IndexMetadata.builder("").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
IndexSettings settings = new IndexSettings(build, Settings.EMPTY);
ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
fieldMapper.processQuery(bq.build(), parseContext);
ParseContext.Document document = parseContext.doc();
PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
List<IndexableField> fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name())));
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"));
fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.minimumShouldMatchField.name())));
assertThat(fields.size(), equalTo(1));
assertThat(fields.get(0).numericValue(), equalTo(1L));
// Now test conjunction:
bq = new BooleanQuery.Builder();
bq.add(termQuery1, Occur.MUST);
bq.add(termQuery2, Occur.MUST);
parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
fieldMapper.processQuery(bq.build(), parseContext);
document = parseContext.doc();
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name())));
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"));
fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.minimumShouldMatchField.name())));
assertThat(fields.size(), equalTo(1));
assertThat(fields.get(0).numericValue(), equalTo(2L));
}
use of org.opensearch.index.mapper.ParseContext in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testExtractTermsAndRanges_failed.
public void testExtractTermsAndRanges_failed() throws Exception {
addQueryFieldMappings();
TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true);
DocumentMapper documentMapper = mapperService.documentMapper("doc");
PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
IndexMetadata build = IndexMetadata.builder("").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
IndexSettings settings = new IndexSettings(build, Settings.EMPTY);
ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
fieldMapper.processQuery(query, parseContext);
ParseContext.Document document = parseContext.doc();
PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
assertThat(document.getFields().size(), equalTo(1));
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED));
}
use of org.opensearch.index.mapper.ParseContext in project OpenSearch by opensearch-project.
the class QueryBuilderStoreTests method testStoringQueryBuilders.
public void testStoringQueryBuilders() throws IOException {
try (Directory directory = newDirectory()) {
TermQueryBuilder[] queryBuilders = new TermQueryBuilder[randomIntBetween(1, 16)];
IndexWriterConfig config = new IndexWriterConfig(new WhitespaceAnalyzer());
config.setMergePolicy(NoMergePolicy.INSTANCE);
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
BinaryFieldMapper fieldMapper = PercolatorFieldMapper.Builder.createQueryBuilderFieldBuilder(new Mapper.BuilderContext(settings, new ContentPath(0)));
Version version = Version.CURRENT;
try (IndexWriter indexWriter = new IndexWriter(directory, config)) {
for (int i = 0; i < queryBuilders.length; i++) {
queryBuilders[i] = new TermQueryBuilder(randomAlphaOfLength(4), randomAlphaOfLength(8));
ParseContext parseContext = mock(ParseContext.class);
ParseContext.Document document = new ParseContext.Document();
when(parseContext.doc()).thenReturn(document);
PercolatorFieldMapper.createQueryBuilderField(version, fieldMapper, queryBuilders[i], parseContext);
indexWriter.addDocument(document);
}
}
QueryShardContext queryShardContext = mock(QueryShardContext.class);
when(queryShardContext.indexVersionCreated()).thenReturn(version);
when(queryShardContext.getWriteableRegistry()).thenReturn(writableRegistry());
when(queryShardContext.getXContentRegistry()).thenReturn(xContentRegistry());
when(queryShardContext.getForField(fieldMapper.fieldType())).thenReturn(new BytesBinaryIndexFieldData(fieldMapper.name(), CoreValuesSourceType.BYTES));
when(queryShardContext.fieldMapper(Mockito.anyString())).thenAnswer(invocation -> {
final String fieldName = (String) invocation.getArguments()[0];
return new KeywordFieldMapper.KeywordFieldType(fieldName);
});
PercolateQuery.QueryStore queryStore = PercolateQueryBuilder.createStore(fieldMapper.fieldType(), queryShardContext);
try (IndexReader indexReader = DirectoryReader.open(directory)) {
LeafReaderContext leafContext = indexReader.leaves().get(0);
CheckedFunction<Integer, Query, IOException> queries = queryStore.getQueries(leafContext);
assertEquals(queryBuilders.length, leafContext.reader().numDocs());
for (int i = 0; i < queryBuilders.length; i++) {
TermQuery query = (TermQuery) queries.apply(i);
assertEquals(queryBuilders[i].fieldName(), query.getTerm().field());
assertEquals(queryBuilders[i].value(), query.getTerm().text());
}
}
}
}
use of org.opensearch.index.mapper.ParseContext in project OpenSearch by opensearch-project.
the class PercolatorFieldMapper method processQuery.
void processQuery(Query query, ParseContext context) {
ParseContext.Document doc = context.doc();
PercolatorFieldType pft = (PercolatorFieldType) this.fieldType();
QueryAnalyzer.Result result;
Version indexVersion = context.mapperService().getIndexSettings().getIndexVersionCreated();
result = QueryAnalyzer.analyze(query, indexVersion);
if (result == QueryAnalyzer.Result.UNKNOWN) {
doc.add(new Field(pft.extractionResultField.name(), EXTRACTION_FAILED, INDEXED_KEYWORD));
return;
}
for (QueryAnalyzer.QueryExtraction extraction : result.extractions) {
if (extraction.term != null) {
BytesRefBuilder builder = new BytesRefBuilder();
builder.append(new BytesRef(extraction.field()));
builder.append(FIELD_VALUE_SEPARATOR);
builder.append(extraction.bytes());
doc.add(new Field(queryTermsField.name(), builder.toBytesRef(), INDEXED_KEYWORD));
} else if (extraction.range != null) {
byte[] min = extraction.range.lowerPoint;
byte[] max = extraction.range.upperPoint;
doc.add(new BinaryRange(rangeFieldMapper.name(), encodeRange(extraction.range.fieldName, min, max)));
}
}
if (result.matchAllDocs) {
doc.add(new Field(extractionResultField.name(), EXTRACTION_FAILED, INDEXED_KEYWORD));
if (result.verified) {
doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, INDEXED_KEYWORD));
}
} else if (result.verified) {
doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, INDEXED_KEYWORD));
} else {
doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, INDEXED_KEYWORD));
}
createFieldNamesField(context);
doc.add(new NumericDocValuesField(minimumShouldMatchFieldMapper.name(), result.minimumShouldMatch));
}
use of org.opensearch.index.mapper.ParseContext in project OpenSearch by opensearch-project.
the class ParentJoinFieldMapper method parse.
@Override
public void parse(ParseContext context) throws IOException {
context.path().add(simpleName());
XContentParser.Token token = context.parser().currentToken();
String name = null;
String parent = null;
if (token == XContentParser.Token.START_OBJECT) {
String currentFieldName = null;
while ((token = context.parser().nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = context.parser().currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("name".equals(currentFieldName)) {
name = context.parser().text();
} else if ("parent".equals(currentFieldName)) {
parent = context.parser().text();
} else {
throw new IllegalArgumentException("unknown field name [" + currentFieldName + "] in join field [" + name() + "]");
}
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if ("parent".equals(currentFieldName)) {
parent = context.parser().numberValue().toString();
} else {
throw new IllegalArgumentException("unknown field name [" + currentFieldName + "] in join field [" + name() + "]");
}
}
}
} else if (token == XContentParser.Token.VALUE_STRING) {
name = context.parser().text();
parent = null;
} else {
throw new IllegalStateException("[" + name + "] expected START_OBJECT or VALUE_STRING but was: " + token);
}
ParentIdFieldMapper parentIdField = getParentIdFieldMapper(name, true);
ParentIdFieldMapper childParentIdField = getParentIdFieldMapper(name, false);
if (parentIdField == null && childParentIdField == null) {
throw new IllegalArgumentException("unknown join name [" + name + "] for field [" + name() + "]");
}
if (childParentIdField != null) {
// Index the document as a child
if (parent == null) {
throw new IllegalArgumentException("[parent] is missing for join field [" + name() + "]");
}
if (context.sourceToParse().routing() == null) {
throw new IllegalArgumentException("[routing] is missing for join field [" + name() + "]");
}
assert childParentIdField.getChildren().contains(name);
ParseContext externalContext = context.createExternalValueContext(parent);
childParentIdField.parse(externalContext);
}
if (parentIdField != null) {
// Index the document as a parent
assert parentIdField.getParentName().equals(name);
ParseContext externalContext = context.createExternalValueContext(context.sourceToParse().id());
parentIdField.parse(externalContext);
}
BytesRef binaryValue = new BytesRef(name);
Field field = new Field(fieldType().name(), binaryValue, fieldType);
context.doc().add(field);
context.doc().add(new SortedDocValuesField(fieldType().name(), binaryValue));
context.path().remove();
}
Aggregations