Search in sources :

Example 1 with XContentFactory.jsonBuilder

use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.

the class QueryRescorerIT method testMoreDocs.

public void testMoreDocs() throws Exception {
    Builder builder = Settings.builder();
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test").setId("1").setSource("field1", "massachusetts avenue boston massachusetts").get();
    client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts").get();
    client().prepareIndex("test").setId("3").setSource("field1", "boston avenue lexington massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("4").setSource("field1", "boston road lexington massachusetts").get();
    client().prepareIndex("test").setId("5").setSource("field1", "lexington street lexington massachusetts").get();
    client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
    client().prepareIndex("test").setId("7").setSource("field1", "bosten street san franciso california").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("8").setSource("field1", "hollywood boulevard los angeles california").get();
    client().prepareIndex("test").setId("9").setSource("field1", "1st street boston massachussetts").get();
    client().prepareIndex("test").setId("10").setSource("field1", "1st street boston massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("11").setSource("field1", "2st street boston massachusetts").get();
    client().prepareIndex("test").setId("12").setSource("field1", "3st street boston massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    // Make sure non-zero from works:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(2).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), greaterThan(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
}
Also used : QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) Builder(org.opensearch.common.settings.Settings.Builder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with XContentFactory.jsonBuilder

use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testDuplicatedClauses.

public void testDuplicatedClauses() throws Exception {
    addQueryFieldMappings();
    QueryBuilder qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))).must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3")));
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
    List<String> values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
    assertThat(values.size(), equalTo(3));
    assertThat(values.get(0), equalTo("field\0value1"));
    assertThat(values.get(1), equalTo("field\0value2"));
    assertThat(values.get(2), equalTo("field\0value3"));
    int msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
    assertThat(msm, equalTo(3));
    qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))).must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))).must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4"))).must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
    values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
    assertThat(values.size(), equalTo(5));
    assertThat(values.get(0), equalTo("field\0value1"));
    assertThat(values.get(1), equalTo("field\0value2"));
    assertThat(values.get(2), equalTo("field\0value3"));
    assertThat(values.get(3), equalTo("field\0value4"));
    assertThat(values.get(4), equalTo("field\0value5"));
    msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
    assertThat(msm, equalTo(4));
    qb = boolQuery().minimumShouldMatch(3).should(boolQuery().should(termQuery("field", "value1")).should(termQuery("field", "value2"))).should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3"))).should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4"))).should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
    values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
    assertThat(values.size(), equalTo(5));
    assertThat(values.get(0), equalTo("field\0value1"));
    assertThat(values.get(1), equalTo("field\0value2"));
    assertThat(values.get(2), equalTo("field\0value3"));
    assertThat(values.get(3), equalTo("field\0value4"));
    assertThat(values.get(4), equalTo("field\0value5"));
    msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
    assertThat(msm, equalTo(1));
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) OpenSearchSingleNodeTestCase(org.opensearch.test.OpenSearchSingleNodeTestCase) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) Version(org.opensearch.Version) Strings(org.opensearch.common.Strings) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Map(java.util.Map) EXTRACTION_PARTIAL(org.opensearch.percolator.PercolatorFieldMapper.EXTRACTION_PARTIAL) EXTRACTION_COMPLETE(org.opensearch.percolator.PercolatorFieldMapper.EXTRACTION_COMPLETE) Occur(org.apache.lucene.search.BooleanClause.Occur) CoveringQuery(org.apache.lucene.search.CoveringQuery) Settings(org.opensearch.common.settings.Settings) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) ScoreMode(org.apache.lucene.search.join.ScoreMode) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) BytesArray(org.opensearch.common.bytes.BytesArray) ParentJoinPlugin(org.opensearch.join.ParentJoinPlugin) XContentType(org.opensearch.common.xcontent.XContentType) MockScriptPlugin(org.opensearch.script.MockScriptPlugin) Matchers.containsString(org.hamcrest.Matchers.containsString) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) EXTRACTION_FAILED(org.opensearch.percolator.PercolatorFieldMapper.EXTRACTION_FAILED) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) ArrayList(java.util.ArrayList) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) Before(org.junit.Before) ParseContext(org.opensearch.index.mapper.ParseContext) FloatPoint(org.apache.lucene.document.FloatPoint) Rewriteable(org.opensearch.index.query.Rewriteable) IOException(java.io.IOException) IndexService(org.opensearch.index.IndexService) Plugin(org.opensearch.plugins.Plugin) 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) IndexableField(org.apache.lucene.index.IndexableField) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) QueryBuilders.prefixQuery(org.opensearch.index.query.QueryBuilders.prefixQuery) ByteBuffer(java.nio.ByteBuffer) DoublePoint(org.apache.lucene.document.DoublePoint) MapperService(org.opensearch.index.mapper.MapperService) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryBuilders.matchAllQuery(org.opensearch.index.query.QueryBuilders.matchAllQuery) XContentFactory(org.opensearch.common.xcontent.XContentFactory) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BytesRef(org.apache.lucene.util.BytesRef) Script(org.opensearch.script.Script) Collection(java.util.Collection) QueryShardException(org.opensearch.index.query.QueryShardException) QueryBuilders.matchPhraseQuery(org.opensearch.index.query.QueryBuilders.matchPhraseQuery) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) QueryBuilders.wildcardQuery(org.opensearch.index.query.QueryBuilders.wildcardQuery) List(java.util.List) QueryBuilder(org.opensearch.index.query.QueryBuilder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) IndexReader(org.apache.lucene.index.IndexReader) LongPoint(org.apache.lucene.document.LongPoint) InetAddresses(org.opensearch.common.network.InetAddresses) BytesReference(org.opensearch.common.bytes.BytesReference) QueryBuilders.boolQuery(org.opensearch.index.query.QueryBuilders.boolQuery) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DocumentMapperParser(org.opensearch.index.mapper.DocumentMapperParser) QueryBuilders.rangeQuery(org.opensearch.index.query.QueryBuilders.rangeQuery) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) Function(java.util.function.Function) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) SourceToParse(org.opensearch.index.mapper.SourceToParse) InternalSettingsPlugin(org.opensearch.test.InternalSettingsPlugin) IntPoint(org.apache.lucene.document.IntPoint) StreamInput(org.opensearch.common.io.stream.StreamInput) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) TermsLookup(org.opensearch.indices.TermsLookup) QueryBuilders.termsLookupQuery(org.opensearch.index.query.QueryBuilders.termsLookupQuery) QueryBuilders.matchQuery(org.opensearch.index.query.QueryBuilders.matchQuery) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MurmurHash3(org.opensearch.common.hash.MurmurHash3) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) 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) Matchers.containsString(org.hamcrest.Matchers.containsString) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint)

Example 3 with XContentFactory.jsonBuilder

use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.

the class QueryRescorerIT method testRescorerMadeScoresWorse.

// Tests a rescorer that penalizes the scores:
public void testRescorerMadeScoresWorse() throws Exception {
    Builder builder = Settings.builder();
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test").setId("3").setSource("field1", "massachusetts").get();
    client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("1").setSource("field1", "lexington massachusetts avenue").get();
    client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts road").get();
    client().admin().indices().prepareRefresh("test").get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("1"));
    assertFourthHit(searchResponse, hasId("2"));
    // Now, penalizing rescore (nothing matches the rescore query):
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(1.0f).setRescoreQueryWeight(-1f), 3).get();
    // 6 and 1 got worse, and then the hit (2) outside the rescore window were sorted ahead:
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
    assertSecondHit(searchResponse, hasId("2"));
    assertThirdHit(searchResponse, hasId("6"));
    assertFourthHit(searchResponse, hasId("1"));
}
Also used : QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) Builder(org.opensearch.common.settings.Settings.Builder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with XContentFactory.jsonBuilder

use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.

the class QueryRescorerIT method testSmallRescoreWindow.

// Tests a rescore window smaller than number of hits:
public void testSmallRescoreWindow() throws Exception {
    Builder builder = Settings.builder();
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test").setId("3").setSource("field1", "massachusetts").get();
    client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("1").setSource("field1", "lexington massachusetts avenue").get();
    client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts road").get();
    client().admin().indices().prepareRefresh("test").get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("1"));
    assertFourthHit(searchResponse, hasId("2"));
    // Now, rescore only top 2 hits w/ proximity:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 2).get();
    // Only top 2 hits were re-ordered:
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("6"));
    assertSecondHit(searchResponse, hasId("3"));
    assertThirdHit(searchResponse, hasId("1"));
    assertFourthHit(searchResponse, hasId("2"));
    // Now, rescore only top 3 hits w/ proximity:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 3).get();
    // Only top 3 hits were re-ordered:
    assertThat(searchResponse.getHits().getHits().length, equalTo(4));
    assertHitCount(searchResponse, 4);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("1"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    assertFourthHit(searchResponse, hasId("2"));
}
Also used : QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) Builder(org.opensearch.common.settings.Settings.Builder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with XContentFactory.jsonBuilder

use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.

the class NestedObjectMapperTests method testLimitOfNestedFieldsPerIndex.

public void testLimitOfNestedFieldsPerIndex() throws Exception {
    Function<String, String> mapping = type -> {
        try {
            return Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("nested2").field("type", "nested").endObject().endObject().endObject().endObject().endObject().endObject());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    // default limit allows at least two nested fields
    createIndex("test1").mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE);
    // explicitly setting limit to 0 prevents nested fields
    Exception e = expectThrows(IllegalArgumentException.class, () -> createIndex("test2", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE));
    assertThat(e.getMessage(), containsString("Limit of nested fields [0] has been exceeded"));
    // setting limit to 1 with 2 nested fields fails
    e = expectThrows(IllegalArgumentException.class, () -> createIndex("test3", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 1).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE));
    assertThat(e.getMessage(), containsString("Limit of nested fields [1] has been exceeded"));
    // do not check nested fields limit if mapping is not updated
    createIndex("test4", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_RECOVERY);
}
Also used : XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) BytesReference(org.opensearch.common.bytes.BytesReference) OpenSearchSingleNodeTestCase(org.opensearch.test.OpenSearchSingleNodeTestCase) IndexableField(org.apache.lucene.index.IndexableField) Collection(java.util.Collection) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Dynamic(org.opensearch.index.mapper.ObjectMapper.Dynamic) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Function(java.util.function.Function) Plugin(org.opensearch.plugins.Plugin) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) InternalSettingsPlugin(org.opensearch.test.InternalSettingsPlugin) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) XContentFactory(org.opensearch.common.xcontent.XContentFactory) XContentType(org.opensearch.common.xcontent.XContentType) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) CompressedXContent(org.opensearch.common.compress.CompressedXContent) UncheckedIOException(java.io.UncheckedIOException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 XContentFactory.jsonBuilder (org.opensearch.common.xcontent.XContentFactory.jsonBuilder)5 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)3 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 Builder (org.opensearch.common.settings.Settings.Builder)3 QueryRescorerBuilder (org.opensearch.search.rescore.QueryRescorerBuilder)3 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Function (java.util.function.Function)2 IndexableField (org.apache.lucene.index.IndexableField)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Strings (org.opensearch.common.Strings)2 BytesReference (org.opensearch.common.bytes.BytesReference)2 CompressedXContent (org.opensearch.common.compress.CompressedXContent)2 Settings (org.opensearch.common.settings.Settings)2