Search in sources :

Example 1 with ParseContext

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));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexSettings(org.opensearch.index.IndexSettings) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) IndexableField(org.apache.lucene.index.IndexableField) ParseContext(org.opensearch.index.mapper.ParseContext) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with ParseContext

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));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexSettings(org.opensearch.index.IndexSettings) ParseContext(org.opensearch.index.mapper.ParseContext) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with ParseContext

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());
            }
        }
    }
}
Also used : BytesBinaryIndexFieldData(org.opensearch.index.fielddata.plain.BytesBinaryIndexFieldData) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) Mapper(org.opensearch.index.mapper.Mapper) Version(org.opensearch.Version) QueryShardContext(org.opensearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Settings(org.opensearch.common.settings.Settings) Directory(org.apache.lucene.store.Directory) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) TermQuery(org.apache.lucene.search.TermQuery) ContentPath(org.opensearch.index.mapper.ContentPath) IOException(java.io.IOException) IndexWriter(org.apache.lucene.index.IndexWriter) ParseContext(org.opensearch.index.mapper.ParseContext) IndexReader(org.apache.lucene.index.IndexReader) BinaryFieldMapper(org.opensearch.index.mapper.BinaryFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 4 with ParseContext

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));
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Version(org.opensearch.Version) ParseContext(org.opensearch.index.mapper.ParseContext) BinaryRange(org.apache.lucene.document.BinaryRange) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with ParseContext

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();
}
Also used : SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) ParseContext(org.opensearch.index.mapper.ParseContext) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) XContentParser(org.opensearch.common.xcontent.XContentParser) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ParseContext (org.opensearch.index.mapper.ParseContext)8 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)5 IndexSettings (org.opensearch.index.IndexSettings)5 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)4 TermQuery (org.apache.lucene.search.TermQuery)3 BytesRef (org.apache.lucene.util.BytesRef)3 ArrayList (java.util.ArrayList)2 Field (org.apache.lucene.document.Field)2 IndexableField (org.apache.lucene.index.IndexableField)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 Query (org.apache.lucene.search.Query)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2 Version (org.opensearch.Version)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 XContentFactory.jsonBuilder (org.opensearch.common.xcontent.XContentFactory.jsonBuilder)2 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)2 BoostingQueryBuilder (org.opensearch.index.query.BoostingQueryBuilder)2 ConstantScoreQueryBuilder (org.opensearch.index.query.ConstantScoreQueryBuilder)2 DisMaxQueryBuilder (org.opensearch.index.query.DisMaxQueryBuilder)2