Search in sources :

Example 1 with XFieldComparatorSource

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource in project OpenSearch by opensearch-project.

the class AbstractNumberNestedSortingTestCase method testNestedSorting.

public void testNestedSorting() throws Exception {
    List<Document> docs = new ArrayList<>();
    Document document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 1));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 2));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 2));
    docs.add(document);
    writer.addDocuments(docs);
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 1));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 3));
    docs.add(document);
    writer.addDocuments(docs);
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 4));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 4));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 5));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 5));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 6));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 6));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    // This doc will not be included, because it doesn't have nested docs
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 7));
    writer.addDocument(document);
    writer.commit();
    docs.clear();
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 7));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 8));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();
    // Some garbage docs, just to check if the NestedFieldComparator can deal with this.
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    MultiValueMode sortMode = MultiValueMode.SUM;
    DirectoryReader directoryReader = DirectoryReader.open(writer);
    directoryReader = OpenSearchDirectoryReader.wrap(directoryReader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);
    Query parentFilter = new TermQuery(new Term("__type", "parent"));
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(searcher, parentFilter, childFilter));
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(7L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(11));
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(7L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(13));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(11));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));
    childFilter = new TermQuery(new Term("filter_1", "T"));
    nestedComparatorSource = createFieldComparator("field2", sortMode, null, createNested(searcher, parentFilter, childFilter));
    query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(6L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
    sort = new Sort(new SortField("field2", nestedComparatorSource));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(6L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));
    nestedComparatorSource = createFieldComparator("field2", sortMode, 127, createNested(searcher, parentFilter, childFilter));
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(new TermQuery(new Term("__type", "parent")), 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(8L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(127));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(24));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(127));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(8));
    nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    sort = new Sort(new SortField("field2", nestedComparatorSource));
    topDocs = searcher.search(new TermQuery(new Term("__type", "parent")), 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(8L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(-127));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(24));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(-127));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(7));
    // Moved to method, because floating point based XFieldComparatorSource have different outcome for SortMode avg,
    // than integral number based implementations...
    assertAvgScoreMode(parentFilter, searcher);
    searcher.getIndexReader().close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) OpenSearchDirectoryReader(org.opensearch.common.lucene.index.OpenSearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) Document(org.apache.lucene.document.Document) MultiValueMode(org.opensearch.search.MultiValueMode) ShardId(org.opensearch.index.shard.ShardId) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) StringField(org.apache.lucene.document.StringField) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Sort(org.apache.lucene.search.Sort)

Example 2 with XFieldComparatorSource

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource in project OpenSearch by opensearch-project.

the class DoubleNestedSortingTests method assertAvgScoreMode.

@Override
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
    MultiValueMode sortMode = MultiValueMode.AVG;
    Query childFilter = Queries.not(parentFilter);
    XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
    Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits.value, equalTo(7L));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) MultiValueMode(org.opensearch.search.MultiValueMode)

Example 3 with XFieldComparatorSource

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testBuildNested.

/**
 * Test that the sort builder nested object gets created in the SortField
 */
public void testBuildNested() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    FieldSortBuilder sortBuilder = new FieldSortBuilder("fieldName").setNestedSort(new NestedSortBuilder("path").setFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value")));
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    XFieldComparatorSource comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    Nested nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(MAPPED_STRING_FIELDNAME, "value")), nested.getInnerQuery());
    sortBuilder = new FieldSortBuilder("fieldName").setNestedPath("path");
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(TypeFieldMapper.NAME, "__path")), nested.getInnerQuery());
    sortBuilder = new FieldSortBuilder("fieldName").setNestedPath("path").setNestedFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value"));
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(MAPPED_STRING_FIELDNAME, "value")), nested.getInnerQuery());
    // if nested path is missing, we omit any filter and return a SortedNumericSortField
    sortBuilder = new FieldSortBuilder("fieldName").setNestedFilter(QueryBuilders.termQuery(MAPPED_STRING_FIELDNAME, "value"));
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) TermQuery(org.apache.lucene.search.TermQuery) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardContext(org.opensearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) Term(org.apache.lucene.index.Term)

Example 4 with XFieldComparatorSource

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource in project OpenSearch by opensearch-project.

the class GeoDistanceSortBuilderTests method testBuildNested.

/**
 * Test that the sort builder nested object gets created in the SortField
 */
public void testBuildNested() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    GeoDistanceSortBuilder sortBuilder = new GeoDistanceSortBuilder("fieldName", 1.0, 1.0).setNestedSort(new NestedSortBuilder("path").setFilter(QueryBuilders.matchAllQuery()));
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    XFieldComparatorSource comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    Nested nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new MatchAllDocsQuery(), nested.getInnerQuery());
    sortBuilder = new GeoDistanceSortBuilder("fieldName", 1.0, 1.0).setNestedPath("path");
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new TermQuery(new Term(TypeFieldMapper.NAME, "__path")), nested.getInnerQuery());
    sortBuilder = new GeoDistanceSortBuilder("fieldName", 1.0, 1.0).setNestedPath("path").setNestedFilter(QueryBuilders.matchAllQuery());
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    nested = comparatorSource.nested();
    assertNotNull(nested);
    assertEquals(new MatchAllDocsQuery(), nested.getInnerQuery());
    // if nested path is missing, we omit any filter and return a regular SortField
    // (LatLonSortField)
    sortBuilder = new GeoDistanceSortBuilder("fieldName", 1.0, 1.0).setNestedFilter(QueryBuilders.termQuery("fieldName", "value"));
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortField.class));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) QueryShardContext(org.opensearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 5 with XFieldComparatorSource

use of org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource in project OpenSearch by opensearch-project.

the class FieldSortBuilderTests method testMultiValueMode.

/**
 * Test that the sort builder mode gets transferred correctly to the SortField
 */
public void testMultiValueMode() throws IOException {
    QueryShardContext shardContextMock = createMockShardContext();
    FieldSortBuilder sortBuilder = new FieldSortBuilder("value").sortMode(SortMode.MIN);
    SortField sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
    SortedNumericSortField numericSortField = (SortedNumericSortField) sortField;
    assertEquals(SortedNumericSelector.Type.MIN, numericSortField.getSelector());
    sortBuilder = new FieldSortBuilder("value").sortMode(SortMode.MAX);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
    numericSortField = (SortedNumericSortField) sortField;
    assertEquals(SortedNumericSelector.Type.MAX, numericSortField.getSelector());
    sortBuilder = new FieldSortBuilder("value").sortMode(SortMode.SUM);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    XFieldComparatorSource comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    assertEquals(MultiValueMode.SUM, comparatorSource.sortMode());
    sortBuilder = new FieldSortBuilder("value").sortMode(SortMode.AVG);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    assertEquals(MultiValueMode.AVG, comparatorSource.sortMode());
    sortBuilder = new FieldSortBuilder("value").sortMode(SortMode.MEDIAN);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField.getComparatorSource(), instanceOf(XFieldComparatorSource.class));
    comparatorSource = (XFieldComparatorSource) sortField.getComparatorSource();
    assertEquals(MultiValueMode.MEDIAN, comparatorSource.sortMode());
    // sort mode should also be set by build() implicitly to MIN or MAX if not set explicitly on builder
    sortBuilder = new FieldSortBuilder("value");
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
    numericSortField = (SortedNumericSortField) sortField;
    assertEquals(SortedNumericSelector.Type.MIN, numericSortField.getSelector());
    sortBuilder = new FieldSortBuilder("value").order(SortOrder.DESC);
    sortField = sortBuilder.build(shardContextMock).field;
    assertThat(sortField, instanceOf(SortedNumericSortField.class));
    numericSortField = (SortedNumericSortField) sortField;
    assertEquals(SortedNumericSelector.Type.MAX, numericSortField.getSelector());
}
Also used : SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) QueryShardContext(org.opensearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) XFieldComparatorSource(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource)

Aggregations

SortField (org.apache.lucene.search.SortField)10 XFieldComparatorSource (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource)10 QueryShardContext (org.opensearch.index.query.QueryShardContext)6 TermQuery (org.apache.lucene.search.TermQuery)5 Term (org.apache.lucene.index.Term)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)4 Query (org.apache.lucene.search.Query)4 Sort (org.apache.lucene.search.Sort)4 QueryBitSetProducer (org.apache.lucene.search.join.QueryBitSetProducer)4 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)4 MultiValueMode (org.opensearch.search.MultiValueMode)4 TopDocs (org.apache.lucene.search.TopDocs)3 Nested (org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)2 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 StringField (org.apache.lucene.document.StringField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1