Search in sources :

Example 31 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class SearchService method canMatch.

private CanMatchResponse canMatch(ShardSearchRequest request, boolean checkRefreshPending) throws IOException {
    assert request.searchType() == SearchType.QUERY_THEN_FETCH : "unexpected search type: " + request.searchType();
    final ReaderContext readerContext = request.readerId() != null ? findReaderContext(request.readerId(), request) : null;
    final Releasable markAsUsed = readerContext != null ? readerContext.markAsUsed(getKeepAlive(request)) : () -> {
    };
    try (Releasable ignored = markAsUsed) {
        final IndexService indexService;
        final Engine.Searcher canMatchSearcher;
        final boolean hasRefreshPending;
        if (readerContext != null) {
            indexService = readerContext.indexService();
            canMatchSearcher = readerContext.acquireSearcher(Engine.CAN_MATCH_SEARCH_SOURCE);
            hasRefreshPending = false;
        } else {
            indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
            IndexShard indexShard = indexService.getShard(request.shardId().getId());
            hasRefreshPending = indexShard.hasRefreshPending() && checkRefreshPending;
            canMatchSearcher = indexShard.acquireSearcher(Engine.CAN_MATCH_SEARCH_SOURCE);
        }
        try (Releasable ignored2 = canMatchSearcher) {
            QueryShardContext context = indexService.newQueryShardContext(request.shardId().id(), canMatchSearcher, request::nowInMillis, request.getClusterAlias());
            Rewriteable.rewrite(request.getRewriteable(), context, false);
            final boolean aliasFilterCanMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder == false;
            FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source());
            MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null;
            final boolean canMatch;
            if (canRewriteToMatchNone(request.source())) {
                QueryBuilder queryBuilder = request.source().query();
                canMatch = aliasFilterCanMatch && queryBuilder instanceof MatchNoneQueryBuilder == false;
            } else {
                // null query means match_all
                canMatch = aliasFilterCanMatch;
            }
            return new CanMatchResponse(canMatch || hasRefreshPending, minMax);
        }
    }
}
Also used : IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) QueryShardContext(org.opensearch.index.query.QueryShardContext) Releasable(org.opensearch.common.lease.Releasable) Engine(org.opensearch.index.engine.Engine)

Example 32 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class BucketSortTests method createTestAggregatorFactory.

@Override
protected BucketSortPipelineAggregationBuilder createTestAggregatorFactory() {
    int sortCount = randomIntBetween(0, 5);
    List<FieldSortBuilder> sorts = new ArrayList<>(sortCount);
    for (int i = 0; i < sortCount; ++i) {
        String sortField = randomAlphaOfLengthBetween(3, 20);
        SortOrder sortOrder = randomFrom(SortOrder.values());
        FieldSortBuilder fieldSortBuilder = new FieldSortBuilder(sortField);
        fieldSortBuilder.order(sortOrder);
        sorts.add(fieldSortBuilder);
    }
    BucketSortPipelineAggregationBuilder factory = new BucketSortPipelineAggregationBuilder(randomAlphaOfLengthBetween(3, 20), sorts);
    Integer from = randomIntBetween(0, 20);
    Integer size = randomBoolean() ? randomIntBetween(1, 1000) : null;
    if (randomBoolean()) {
        factory.from(from);
    }
    if (size != null) {
        factory.size(size);
    }
    if (randomBoolean()) {
        factory.gapPolicy(randomFrom(BucketHelpers.GapPolicy.values()));
    }
    // Check if the combination ended up being invalid
    if (sorts.isEmpty() && size == null && from == 0) {
        factory.size(42);
    }
    return factory;
}
Also used : ArrayList(java.util.ArrayList) SortOrder(org.opensearch.search.sort.SortOrder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder)

Example 33 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class NestedSortingTests method testMultiLevelNestedSorting.

public void testMultiLevelNestedSorting() throws IOException {
    XContentBuilder mapping = XContentFactory.jsonBuilder();
    mapping.startObject();
    {
        mapping.startObject("_doc");
        {
            mapping.startObject("properties");
            {
                {
                    mapping.startObject("title");
                    mapping.field("type", "text");
                    mapping.endObject();
                }
                {
                    mapping.startObject("genre");
                    mapping.field("type", "keyword");
                    mapping.endObject();
                }
                {
                    mapping.startObject("chapters");
                    mapping.field("type", "nested");
                    {
                        mapping.startObject("properties");
                        {
                            mapping.startObject("title");
                            mapping.field("type", "text");
                            mapping.endObject();
                        }
                        {
                            mapping.startObject("read_time_seconds");
                            mapping.field("type", "integer");
                            mapping.endObject();
                        }
                        {
                            mapping.startObject("paragraphs");
                            mapping.field("type", "nested");
                            {
                                mapping.startObject("properties");
                                {
                                    {
                                        mapping.startObject("header");
                                        mapping.field("type", "text");
                                        mapping.endObject();
                                    }
                                    {
                                        mapping.startObject("content");
                                        mapping.field("type", "text");
                                        mapping.endObject();
                                    }
                                    {
                                        mapping.startObject("word_count");
                                        mapping.field("type", "integer");
                                        mapping.endObject();
                                    }
                                }
                                mapping.endObject();
                            }
                            mapping.endObject();
                        }
                        mapping.endObject();
                    }
                    mapping.endObject();
                }
            }
            mapping.endObject();
        }
        mapping.endObject();
    }
    mapping.endObject();
    IndexService indexService = createIndex("nested_sorting", Settings.EMPTY, "_doc", mapping);
    List<List<Document>> books = new ArrayList<>();
    {
        List<Document> book = new ArrayList<>();
        Document document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "Paragraph 1", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 743));
        document.add(new IntPoint("chapters.paragraphs.word_count", 743));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "chapter 3", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 400));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 400));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "Paragraph 1", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 234));
        document.add(new IntPoint("chapters.paragraphs.word_count", 234));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "chapter 2", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 200));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 200));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "Paragraph 2", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 478));
        document.add(new IntPoint("chapters.paragraphs.word_count", 478));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "Paragraph 1", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 849));
        document.add(new IntPoint("chapters.paragraphs.word_count", 849));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "chapter 1", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 1400));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 1400));
        book.add(document);
        document = new Document();
        document.add(new StringField("genre", "science fiction", Field.Store.NO));
        document.add(new StringField("_type", "_doc", Field.Store.NO));
        document.add(new StringField("_id", "1", Field.Store.YES));
        document.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0));
        book.add(document);
        books.add(book);
    }
    {
        List<Document> book = new ArrayList<>();
        Document document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "Introduction", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 76));
        document.add(new IntPoint("chapters.paragraphs.word_count", 76));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "chapter 1", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 20));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 20));
        book.add(document);
        document = new Document();
        document.add(new StringField("genre", "romance", Field.Store.NO));
        document.add(new StringField("_type", "_doc", Field.Store.NO));
        document.add(new StringField("_id", "2", Field.Store.YES));
        document.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0));
        book.add(document);
        books.add(book);
    }
    {
        List<Document> book = new ArrayList<>();
        Document document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "A bad dream", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 976));
        document.add(new IntPoint("chapters.paragraphs.word_count", 976));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "The beginning of the end", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 1200));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 1200));
        book.add(document);
        document = new Document();
        document.add(new StringField("genre", "horror", Field.Store.NO));
        document.add(new StringField("_type", "_doc", Field.Store.NO));
        document.add(new StringField("_id", "3", Field.Store.YES));
        document.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0));
        book.add(document);
        books.add(book);
    }
    {
        List<Document> book = new ArrayList<>();
        Document document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "macaroni", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 180));
        document.add(new IntPoint("chapters.paragraphs.word_count", 180));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "hamburger", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 150));
        document.add(new IntPoint("chapters.paragraphs.word_count", 150));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "tosti", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 120));
        document.add(new IntPoint("chapters.paragraphs.word_count", 120));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "easy meals", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 800));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 800));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.paragraphs.header", "introduction", Field.Store.NO));
        document.add(new StringField("_type", "__chapters.paragraphs", Field.Store.NO));
        document.add(new TextField("chapters.paragraphs.text", "some text...", Field.Store.NO));
        document.add(new SortedNumericDocValuesField("chapters.paragraphs.word_count", 87));
        document.add(new IntPoint("chapters.paragraphs.word_count", 87));
        book.add(document);
        document = new Document();
        document.add(new TextField("chapters.title", "introduction", Field.Store.NO));
        document.add(new StringField("_type", "__chapters", Field.Store.NO));
        document.add(new IntPoint("chapters.read_time_seconds", 10));
        document.add(new NumericDocValuesField("chapters.read_time_seconds", 10));
        book.add(document);
        document = new Document();
        document.add(new StringField("genre", "cooking", Field.Store.NO));
        document.add(new StringField("_type", "_doc", Field.Store.NO));
        document.add(new StringField("_id", "4", Field.Store.YES));
        document.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0));
        book.add(document);
        books.add(book);
    }
    {
        List<Document> book = new ArrayList<>();
        Document document = new Document();
        document.add(new StringField("genre", "unknown", Field.Store.NO));
        document.add(new StringField("_type", "_doc", Field.Store.NO));
        document.add(new StringField("_id", "5", Field.Store.YES));
        document.add(new NumericDocValuesField(PRIMARY_TERM_NAME, 0));
        book.add(document);
        books.add(book);
    }
    Collections.shuffle(books, random());
    for (List<Document> book : books) {
        writer.addDocuments(book);
        if (randomBoolean()) {
            writer.commit();
        }
    }
    DirectoryReader reader = DirectoryReader.open(writer);
    reader = OpenSearchDirectoryReader.wrap(reader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryShardContext queryShardContext = indexService.newQueryShardContext(0, searcher, () -> 0L, null);
    FieldSortBuilder sortBuilder = new FieldSortBuilder("chapters.paragraphs.word_count");
    sortBuilder.setNestedSort(new NestedSortBuilder("chapters").setNestedSort(new NestedSortBuilder("chapters.paragraphs")));
    QueryBuilder queryBuilder = new MatchAllQueryBuilder();
    TopFieldDocs topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
    assertThat(topFields.totalHits.value, equalTo(5L));
    assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("2"));
    assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(76L));
    assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("4"));
    assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(87L));
    assertThat(searcher.doc(topFields.scoreDocs[2].doc).get("_id"), equalTo("1"));
    assertThat(((FieldDoc) topFields.scoreDocs[2]).fields[0], equalTo(234L));
    assertThat(searcher.doc(topFields.scoreDocs[3].doc).get("_id"), equalTo("3"));
    assertThat(((FieldDoc) topFields.scoreDocs[3]).fields[0], equalTo(976L));
    assertThat(searcher.doc(topFields.scoreDocs[4].doc).get("_id"), equalTo("5"));
    assertThat(((FieldDoc) topFields.scoreDocs[4]).fields[0], equalTo(Long.MAX_VALUE));
    // Specific genre
    {
        queryBuilder = new TermQueryBuilder("genre", "romance");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(76L));
        queryBuilder = new TermQueryBuilder("genre", "science fiction");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("1"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(234L));
        queryBuilder = new TermQueryBuilder("genre", "horror");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("3"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(976L));
        queryBuilder = new TermQueryBuilder("genre", "cooking");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(87L));
    }
    // reverse sort order
    {
        sortBuilder.order(SortOrder.DESC);
        queryBuilder = new MatchAllQueryBuilder();
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(5L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("3"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(976L));
        assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("1"));
        assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(849L));
        assertThat(searcher.doc(topFields.scoreDocs[2].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[2]).fields[0], equalTo(180L));
        assertThat(searcher.doc(topFields.scoreDocs[3].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[3]).fields[0], equalTo(76L));
        assertThat(searcher.doc(topFields.scoreDocs[4].doc).get("_id"), equalTo("5"));
        assertThat(((FieldDoc) topFields.scoreDocs[4]).fields[0], equalTo(Long.MIN_VALUE));
    }
    // Specific genre and reverse sort order
    {
        queryBuilder = new TermQueryBuilder("genre", "romance");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(76L));
        queryBuilder = new TermQueryBuilder("genre", "science fiction");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("1"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(849L));
        queryBuilder = new TermQueryBuilder("genre", "horror");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("3"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(976L));
        queryBuilder = new TermQueryBuilder("genre", "cooking");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(180L));
    }
    // Nested filter + query
    {
        queryBuilder = new RangeQueryBuilder("chapters.read_time_seconds").to(50L);
        sortBuilder = new FieldSortBuilder("chapters.paragraphs.word_count");
        sortBuilder.setNestedSort(new NestedSortBuilder("chapters").setFilter(queryBuilder).setNestedSort(new NestedSortBuilder("chapters.paragraphs")));
        topFields = search(new NestedQueryBuilder("chapters", queryBuilder, ScoreMode.None), sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(2L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(76L));
        assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(87L));
        sortBuilder.order(SortOrder.DESC);
        topFields = search(new NestedQueryBuilder("chapters", queryBuilder, ScoreMode.None), sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(2L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(87L));
        assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(76L));
    }
    // Multiple Nested filters + query
    {
        queryBuilder = new RangeQueryBuilder("chapters.read_time_seconds").to(50L);
        sortBuilder = new FieldSortBuilder("chapters.paragraphs.word_count");
        sortBuilder.setNestedSort(new NestedSortBuilder("chapters").setFilter(queryBuilder).setNestedSort(new NestedSortBuilder("chapters.paragraphs").setFilter(new RangeQueryBuilder("chapters.paragraphs.word_count").from(80L))));
        topFields = search(new NestedQueryBuilder("chapters", queryBuilder, ScoreMode.None), sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(2L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(87L));
        assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(Long.MAX_VALUE));
        sortBuilder.order(SortOrder.DESC);
        topFields = search(new NestedQueryBuilder("chapters", queryBuilder, ScoreMode.None), sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(2L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(87L));
        assertThat(searcher.doc(topFields.scoreDocs[1].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[1]).fields[0], equalTo(Long.MIN_VALUE));
    }
    // Nested filter + Specific genre
    {
        sortBuilder = new FieldSortBuilder("chapters.paragraphs.word_count");
        sortBuilder.setNestedSort(new NestedSortBuilder("chapters").setFilter(new RangeQueryBuilder("chapters.read_time_seconds").to(50L)).setNestedSort(new NestedSortBuilder("chapters.paragraphs")));
        queryBuilder = new TermQueryBuilder("genre", "romance");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("2"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(76L));
        queryBuilder = new TermQueryBuilder("genre", "science fiction");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("1"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(Long.MAX_VALUE));
        queryBuilder = new TermQueryBuilder("genre", "horror");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("3"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(Long.MAX_VALUE));
        queryBuilder = new TermQueryBuilder("genre", "cooking");
        topFields = search(queryBuilder, sortBuilder, queryShardContext, searcher);
        assertThat(topFields.totalHits.value, equalTo(1L));
        assertThat(searcher.doc(topFields.scoreDocs[0].doc).get("_id"), equalTo("4"));
        assertThat(((FieldDoc) topFields.scoreDocs[0]).fields[0], equalTo(87L));
    }
    searcher.getIndexReader().close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FieldDoc(org.apache.lucene.search.FieldDoc) IndexService(org.opensearch.index.IndexService) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) Document(org.apache.lucene.document.Document) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) ShardId(org.opensearch.index.shard.ShardId) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) TextField(org.apache.lucene.document.TextField) QueryShardContext(org.opensearch.index.query.QueryShardContext) List(java.util.List) ArrayList(java.util.ArrayList) DirectoryReader(org.apache.lucene.index.DirectoryReader) OpenSearchDirectoryReader(org.opensearch.common.lucene.index.OpenSearchDirectoryReader) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) IntPoint(org.apache.lucene.document.IntPoint) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NestedSortBuilder(org.opensearch.search.sort.NestedSortBuilder) StringField(org.apache.lucene.document.StringField) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 34 with FieldSortBuilder

use of org.opensearch.search.sort.FieldSortBuilder in project OpenSearch by opensearch-project.

the class RareTermsAggregatorTests method testWithNestedScoringAggregations.

public void testWithNestedScoringAggregations() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < 10; i++) {
                int[] nestedValues = new int[i];
                for (int j = 0; j < i; j++) {
                    nestedValues[j] = j;
                }
                indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
            }
            indexWriter.commit();
            for (boolean withScore : new boolean[] { true, false }) {
                NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new RareTermsAggregationBuilder("terms").field("nested_value").maxDocCount(2).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
                MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
                try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
                    if (withScore) {
                        IllegalStateException e = expectThrows(IllegalStateException.class, () -> searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType));
                        assertThat(e.getMessage(), equalTo("RareTerms agg [terms] is the child of the nested agg [nested], " + "and also has a scoring child agg [top_hits].  This combination is not supported because it requires " + "executing in [depth_first] mode, which the RareTerms agg cannot do."));
                    } else {
                        InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only
                        new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
                        InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
                        assertThat(terms.getBuckets().size(), equalTo(2));
                        long counter = 1;
                        for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
                            InternalTopHits topHits = bucket.getAggregations().get("top_hits");
                            TotalHits hits = topHits.getHits().getTotalHits();
                            assertNotNull(hits);
                            assertThat(hits.value, equalTo(counter));
                            assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
                            counter += 1;
                        }
                    }
                }
            }
        }
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) InternalNested(org.opensearch.search.aggregations.bucket.nested.InternalNested) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) LongPoint(org.apache.lucene.document.LongPoint) InternalTopHits(org.opensearch.search.aggregations.metrics.InternalTopHits) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)

Aggregations

FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)34 SearchResponse (org.opensearch.action.search.SearchResponse)19 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)13 ArrayList (java.util.ArrayList)8 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)6 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)6 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)5 QueryBuilder (org.opensearch.index.query.QueryBuilder)5 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)5 SearchRequest (org.opensearch.action.search.SearchRequest)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)4 SearchHit (org.opensearch.search.SearchHit)4 IOException (java.io.IOException)3 List (java.util.List)3 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)3 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)3 Map (java.util.Map)2