Search in sources :

Example 1 with ParsedDocument

use of org.opensearch.index.mapper.ParsedDocument in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testImplicitlySetDefaultScriptLang.

public void testImplicitlySetDefaultScriptLang() throws Exception {
    addQueryFieldMappings();
    XContentBuilder query = jsonBuilder();
    query.startObject();
    query.startObject("script");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            // Query builder's content is stored via BinaryFieldMapper, which has a custom encoding
            // to encode multiple binary values into a single binary doc values field.
            // This is the reason we need to first need to read the number of values and
            // then the length of the field value in bytes.
            input.readVInt();
            input.readVInt();
            ScriptQueryBuilder queryBuilder = (ScriptQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            assertEquals(Script.DEFAULT_SCRIPT_LANG, queryBuilder.script().getLang());
        }
    }
    query = jsonBuilder();
    query.startObject();
    query.startObject("function_score");
    query.startArray("functions");
    query.startObject();
    query.startObject("script_score");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    query.endArray();
    query.endObject();
    query.endObject();
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            input.readVInt();
            input.readVInt();
            FunctionScoreQueryBuilder queryBuilder = (FunctionScoreQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) queryBuilder.filterFunctionBuilders()[0].getScoreFunction();
            assertEquals(Script.DEFAULT_SCRIPT_LANG, function.getScript().getLang());
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SourceToParse(org.opensearch.index.mapper.SourceToParse) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) 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) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 2 with ParsedDocument

use of org.opensearch.index.mapper.ParsedDocument in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testNestedPercolatorField.

// 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 = "doc";
    String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties").startObject("object_field").field("type", "object").startObject("properties").startObject("query_field").field("type", "percolator").endObject().endObject().endObject().endObject().endObject().endObject());
    mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE);
    QueryBuilder queryBuilder = matchQuery("field", "value");
    ParsedDocument doc = mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startObject("object_field").field("query_field", queryBuilder).endObject().endObject()), XContentType.JSON));
    // also includes all other meta fields
    assertThat(doc.rootDoc().getFields().size(), equalTo(12));
    IndexableField queryBuilderField = doc.rootDoc().getField("object_field.query_field.query_builder_field");
    assertTrue(queryBuilderField.fieldType().omitNorms());
    IndexableField extractionResultField = doc.rootDoc().getField("object_field.query_field.extraction_result");
    assertTrue(extractionResultField.fieldType().omitNorms());
    BytesRef queryBuilderAsBytes = queryBuilderField.binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
    doc = mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startArray("object_field").startObject().field("query_field", queryBuilder).endObject().endArray().endObject()), XContentType.JSON));
    // also includes all other meta fields
    assertThat(doc.rootDoc().getFields().size(), equalTo(12));
    queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startArray("object_field").startObject().field("query_field", queryBuilder).endObject().startObject().field("query_field", queryBuilder).endObject().endArray().endObject()), XContentType.JSON));
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    assertThat(e.getCause().getMessage(), equalTo("a document can only contain one percolator query"));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) 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) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with ParsedDocument

use of org.opensearch.index.mapper.ParsedDocument in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testQueryWithRewrite.

public void testQueryWithRewrite() throws Exception {
    addQueryFieldMappings();
    client().prepareIndex("remote").setId("1").setSource("field", "value").get();
    QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "1", "field"));
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), XContentType.JSON));
    BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    QueryShardContext shardContext = indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    }, null);
    PlainActionFuture<QueryBuilder> future = new PlainActionFuture<>();
    Rewriteable.rewriteAndFetch(queryBuilder, shardContext, future);
    assertQueryBuilder(qbSource, future.get());
}
Also used : ParsedDocument(org.opensearch.index.mapper.ParsedDocument) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) SourceToParse(org.opensearch.index.mapper.SourceToParse) QueryShardContext(org.opensearch.index.query.QueryShardContext) TermsLookup(org.opensearch.indices.TermsLookup) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) 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) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with ParsedDocument

use of org.opensearch.index.mapper.ParsedDocument in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testPercolatorFieldMapper.

public void testPercolatorFieldMapper() throws Exception {
    addQueryFieldMappings();
    QueryBuilder queryBuilder = termQuery("field", "value");
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), XContentType.JSON));
    assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name()).length, equalTo(1));
    assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name())[0].binaryValue().utf8ToString(), equalTo("field\0value"));
    assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(1));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_COMPLETE));
    BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    assertQueryBuilder(qbSource, queryBuilder);
    // add an query for which we don't extract terms from
    queryBuilder = rangeQuery("field").from("a").to("z");
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), XContentType.JSON));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_FAILED));
    assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name()).length, equalTo(0));
    assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(1));
    qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    assertQueryBuilder(qbSource, queryBuilder);
    queryBuilder = rangeQuery("date_field").from("now");
    doc = mapperService.documentMapper().parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), XContentType.JSON));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1));
    assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_FAILED));
}
Also used : ParsedDocument(org.opensearch.index.mapper.ParsedDocument) SourceToParse(org.opensearch.index.mapper.SourceToParse) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) 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) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with ParsedDocument

use of org.opensearch.index.mapper.ParsedDocument in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testPercolatorFieldMapper_noQuery.

public void testPercolatorFieldMapper_noQuery() throws Exception {
    addQueryFieldMappings();
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON));
    assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0));
    try {
        mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject()), XContentType.JSON));
    } catch (MapperParsingException e) {
        assertThat(e.getDetailedMessage(), containsString("query malformed, must start with start_object"));
    }
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) SourceToParse(org.opensearch.index.mapper.SourceToParse)

Aggregations

ParsedDocument (org.opensearch.index.mapper.ParsedDocument)145 Matchers.containsString (org.hamcrest.Matchers.containsString)66 LongPoint (org.apache.lucene.document.LongPoint)43 IOException (java.io.IOException)41 IndexableField (org.apache.lucene.index.IndexableField)41 BytesArray (org.opensearch.common.bytes.BytesArray)41 Store (org.opensearch.index.store.Store)38 BytesRef (org.apache.lucene.util.BytesRef)35 MapperService (org.opensearch.index.mapper.MapperService)32 IndexSettings (org.opensearch.index.IndexSettings)31 AtomicLong (java.util.concurrent.atomic.AtomicLong)30 SourceToParse (org.opensearch.index.mapper.SourceToParse)30 HashSet (java.util.HashSet)29 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)29 ArrayList (java.util.ArrayList)25 Map (java.util.Map)24 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)24 UncheckedIOException (java.io.UncheckedIOException)23 Arrays (java.util.Arrays)23 Collections (java.util.Collections)23