Search in sources :

Example 26 with FieldSortBuilder

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

the class SearchScrollIT method testStringSortMissingAscTerminates.

public void testStringSortMissingAscTerminates() throws Exception {
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("test", "no_field", "type=keyword", "some_field", "type=keyword"));
    client().prepareIndex("test").setId("1").setSource("some_field", "test").get();
    refresh();
    SearchResponse response = client().prepareSearch("test").addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_last")).setScroll("1m").get();
    assertHitCount(response, 1);
    assertSearchHits(response, "1");
    response = client().prepareSearchScroll(response.getScrollId()).get();
    assertSearchResponse(response);
    assertHitCount(response, 1);
    assertNoSearchHits(response);
    response = client().prepareSearch("test").addSort(new FieldSortBuilder("no_field").order(SortOrder.ASC).missing("_first")).setScroll("1m").get();
    assertHitCount(response, 1);
    assertSearchHits(response, "1");
    response = client().prepareSearchScroll(response.getScrollId()).get();
    assertHitCount(response, 1);
    assertThat(response.getHits().getHits().length, equalTo(0));
}
Also used : FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 27 with FieldSortBuilder

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

the class CompletionSuggestSearchIT method testThatSortingOnCompletionFieldReturnsUsefulException.

public void testThatSortingOnCompletionFieldReturnsUsefulException() throws Exception {
    createIndexAndMapping(completionMappingBuilder);
    client().prepareIndex(INDEX).setId("1").setSource(jsonBuilder().startObject().startObject(FIELD).startArray("input").value("Nirvana").endArray().endObject().endObject()).get();
    refresh();
    SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch(INDEX).addSort(new FieldSortBuilder(FIELD)).get());
    assertThat(e.status().getStatus(), is(400));
    assertThat(e.toString(), containsString("Fielddata is not supported on field [" + FIELD + "] of type [completion]"));
}
Also used : SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder)

Example 28 with FieldSortBuilder

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

the class InnerHitsIT method testRandomParentChild.

public void testRandomParentChild() throws Exception {
    assertAcked(prepareCreate("idx").addMapping("doc", jsonBuilder().startObject().startObject("doc").startObject("properties").startObject("id").field("type", "keyword").endObject().startObject("join_field").field("type", "join").startObject("relations").field("parent", new String[] { "child1", "child2" }).endObject().endObject().endObject().endObject().endObject()));
    int numDocs = scaledRandomIntBetween(5, 50);
    List<IndexRequestBuilder> requestBuilders = new ArrayList<>();
    int child1 = 0;
    int child2 = 0;
    int[] child1InnerObjects = new int[numDocs];
    int[] child2InnerObjects = new int[numDocs];
    for (int parent = 0; parent < numDocs; parent++) {
        String parentId = String.format(Locale.ENGLISH, "p_%03d", parent);
        requestBuilders.add(createIndexRequest("idx", "parent", parentId, null));
        int numChildDocs = child1InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        int limit = child1 + numChildDocs;
        for (; child1 < limit; child1++) {
            requestBuilders.add(createIndexRequest("idx", "child1", String.format(Locale.ENGLISH, "c1_%04d", child1), parentId));
        }
        numChildDocs = child2InnerObjects[parent] = scaledRandomIntBetween(1, numDocs);
        limit = child2 + numChildDocs;
        for (; child2 < limit; child2++) {
            requestBuilders.add(createIndexRequest("idx", "child2", String.format(Locale.ENGLISH, "c2_%04d", child2), parentId));
        }
    }
    indexRandom(true, requestBuilders);
    int size = randomIntBetween(0, numDocs);
    BoolQueryBuilder boolQuery = new BoolQueryBuilder();
    boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size))));
    boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size))));
    SearchResponse searchResponse = client().prepareSearch("idx").setSize(numDocs).addSort("id", SortOrder.ASC).setQuery(boolQuery).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, numDocs);
    assertThat(searchResponse.getHits().getHits().length, equalTo(numDocs));
    int offset1 = 0;
    int offset2 = 0;
    for (int parent = 0; parent < numDocs; parent++) {
        SearchHit searchHit = searchResponse.getHits().getAt(parent);
        assertThat(searchHit.getId(), equalTo(String.format(Locale.ENGLISH, "p_%03d", parent)));
        assertThat(searchHit.getShard(), notNullValue());
        SearchHits inner = searchHit.getInnerHits().get("a");
        assertThat(inner.getTotalHits().value, equalTo((long) child1InnerObjects[parent]));
        for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            String childId = String.format(Locale.ENGLISH, "c1_%04d", offset1 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset1 += child1InnerObjects[parent];
        inner = searchHit.getInnerHits().get("b");
        assertThat(inner.getTotalHits().value, equalTo((long) child2InnerObjects[parent]));
        for (int child = 0; child < child2InnerObjects[parent] && child < size; child++) {
            SearchHit innerHit = inner.getAt(child);
            String childId = String.format(Locale.ENGLISH, "c2_%04d", offset2 + child);
            assertThat(innerHit.getId(), equalTo(childId));
            assertThat(innerHit.getNestedIdentity(), nullValue());
        }
        offset2 += child2InnerObjects[parent];
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchHit(org.opensearch.search.SearchHit) OpenSearchAssertions.assertSearchHit(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchHits(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 29 with FieldSortBuilder

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

the class InnerHitsIT method testMatchesQueriesNestedInnerHits.

public void testMatchesQueriesNestedInnerHits() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("n_field1").field("type", "keyword").endObject().endObject().endObject().startObject("field1").field("type", "long").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", builder));
    ensureGreen();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = randomIntBetween(2, 35);
    requests.add(client().prepareIndex("test").setId("0").setSource(jsonBuilder().startObject().field("field1", 0).startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    requests.add(client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("field1", 1).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_3").field("n_field2", "n_value2_1").endObject().endArray().endObject()));
    for (int i = 2; i < numDocs; i++) {
        requests.add(client().prepareIndex("test").setId(String.valueOf(i)).setSource(jsonBuilder().startObject().field("field1", i).startArray("nested1").startObject().field("n_field1", "n_value1_8").field("n_field2", "n_value2_5").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()));
    }
    indexRandom(true, requests);
    waitForRelocation(ClusterHealthStatus.GREEN);
    QueryBuilder query = boolQuery().should(termQuery("nested1.n_field1", "n_value1_1").queryName("test1")).should(termQuery("nested1.n_field1", "n_value1_3").queryName("test2")).should(termQuery("nested1.n_field2", "n_value2_2").queryName("test3"));
    query = nestedQuery("nested1", query, ScoreMode.Avg).innerHit(new InnerHitBuilder().addSort(new FieldSortBuilder("nested1.n_field1").order(SortOrder.ASC)));
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(query).setSize(numDocs).addSort("field1", SortOrder.ASC).get();
    assertNoFailures(searchResponse);
    assertAllSuccessful(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocs));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("0"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getTotalHits().value, equalTo(2L));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test1"));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getInnerHits().get("nested1").getAt(1).getMatchedQueries()[0], equalTo("test3"));
    assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1"));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getTotalHits().value, equalTo(1L));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(1).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test2"));
    for (int i = 2; i < numDocs; i++) {
        assertThat(searchResponse.getHits().getAt(i).getId(), equalTo(String.valueOf(i)));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getTotalHits().value, equalTo(1L));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries().length, equalTo(1));
        assertThat(searchResponse.getHits().getAt(i).getInnerHits().get("nested1").getAt(0).getMatchedQueries()[0], equalTo("test3"));
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 30 with FieldSortBuilder

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

the class CanMatchPreFilterSearchPhase method getIterator.

private GroupShardsIterator<SearchShardIterator> getIterator(CanMatchSearchPhaseResults results, GroupShardsIterator<SearchShardIterator> shardsIts) {
    int cardinality = results.getNumPossibleMatches();
    FixedBitSet possibleMatches = results.getPossibleMatches();
    if (cardinality == 0) {
        // this is a special case where we have no hit but we need to get at least one search response in order
        // to produce a valid search result with all the aggs etc.
        possibleMatches.set(0);
    }
    SearchSourceBuilder source = getRequest().source();
    int i = 0;
    for (SearchShardIterator iter : shardsIts) {
        if (possibleMatches.get(i++)) {
            iter.reset();
        } else {
            iter.resetAndSkip();
        }
    }
    if (shouldSortShards(results.minAndMaxes) == false) {
        return shardsIts;
    }
    FieldSortBuilder fieldSort = FieldSortBuilder.getPrimaryFieldSortOrNull(source);
    return new GroupShardsIterator<>(sortShards(shardsIts, results.minAndMaxes, fieldSort.order()));
}
Also used : GroupShardsIterator(org.opensearch.cluster.routing.GroupShardsIterator) FixedBitSet(org.apache.lucene.util.FixedBitSet) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

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