Search in sources :

Example 1 with SearchType

use of org.opensearch.action.search.SearchType in project OpenSearch by opensearch-project.

the class SearchWithRejectionsIT method testOpenContextsAfterRejections.

public void testOpenContextsAfterRejections() throws Exception {
    createIndex("test");
    ensureGreen("test");
    final int docs = scaledRandomIntBetween(20, 50);
    for (int i = 0; i < docs; i++) {
        client().prepareIndex("test").setId(Integer.toString(i)).setSource("field", "value").get();
    }
    IndicesStatsResponse indicesStats = client().admin().indices().prepareStats().get();
    assertThat(indicesStats.getTotal().getSearch().getOpenContexts(), equalTo(0L));
    refresh();
    int numSearches = 10;
    Future<SearchResponse>[] responses = new Future[numSearches];
    SearchType searchType = randomFrom(SearchType.DEFAULT, SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH);
    logger.info("search type is {}", searchType);
    for (int i = 0; i < numSearches; i++) {
        responses[i] = client().prepareSearch().setQuery(matchAllQuery()).setSearchType(searchType).execute();
    }
    for (int i = 0; i < numSearches; i++) {
        try {
            responses[i].get();
        } catch (Exception t) {
        }
    }
    assertBusy(() -> assertThat(client().admin().indices().prepareStats().get().getTotal().getSearch().getOpenContexts(), equalTo(0L)), 1, TimeUnit.SECONDS);
}
Also used : IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) Future(java.util.concurrent.Future) SearchType(org.opensearch.action.search.SearchType)

Example 2 with SearchType

use of org.opensearch.action.search.SearchType in project OpenSearch by opensearch-project.

the class ChildQuerySearchIT method testSimpleQueryRewrite.

public void testSimpleQueryRewrite() throws Exception {
    assertAcked(prepareCreate("test").addMapping("doc", addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), "c_field", "keyword", "p_field", "keyword")));
    ensureGreen();
    // index simple data
    int childId = 0;
    for (int i = 0; i < 10; i++) {
        String parentId = String.format(Locale.ROOT, "p%03d", i);
        createIndexRequest("test", "parent", parentId, null, "p_field", parentId).get();
        int j = childId;
        for (; j < childId + 50; j++) {
            String childUid = String.format(Locale.ROOT, "c%03d", j);
            createIndexRequest("test", "child", childUid, parentId, "c_field", childUid).get();
        }
        childId = j;
    }
    refresh();
    SearchType[] searchTypes = new SearchType[] { SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH };
    for (SearchType searchType : searchTypes) {
        SearchResponse searchResponse = client().prepareSearch("test").setSearchType(searchType).setQuery(hasChildQuery("child", prefixQuery("c_field", "c"), ScoreMode.Max)).addSort("p_field", SortOrder.ASC).setSize(5).get();
        assertNoFailures(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(10L));
        assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("p000"));
        assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("p001"));
        assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("p002"));
        assertThat(searchResponse.getHits().getHits()[3].getId(), equalTo("p003"));
        assertThat(searchResponse.getHits().getHits()[4].getId(), equalTo("p004"));
        searchResponse = client().prepareSearch("test").setSearchType(searchType).setQuery(hasParentQuery("parent", prefixQuery("p_field", "p"), true)).addSort("c_field", SortOrder.ASC).setSize(5).get();
        assertNoFailures(searchResponse);
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(500L));
        assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("c000"));
        assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("c001"));
        assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("c002"));
        assertThat(searchResponse.getHits().getHits()[3].getId(), equalTo("c003"));
        assertThat(searchResponse.getHits().getHits()[4].getId(), equalTo("c004"));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SearchType(org.opensearch.action.search.SearchType) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with SearchType

use of org.opensearch.action.search.SearchType in project OpenSearch by opensearch-project.

the class DuelScrollIT method testDuelIndexOrderQueryThenFetch.

public void testDuelIndexOrderQueryThenFetch() throws Exception {
    final SearchType searchType = RandomPicks.randomFrom(random(), Arrays.asList(SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH));
    final int numDocs = createIndex(false);
    testDuelIndexOrder(searchType, false, numDocs);
    testDuelIndexOrder(searchType, true, numDocs);
}
Also used : SearchType(org.opensearch.action.search.SearchType)

Example 4 with SearchType

use of org.opensearch.action.search.SearchType in project OpenSearch by opensearch-project.

the class DuelScrollIT method create.

private TestContext create(SearchType... searchTypes) throws Exception {
    assertAcked(prepareCreate("index").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "long").endObject().startObject("field2").field("type", "keyword").endObject().startObject("nested").field("type", "nested").startObject("properties").startObject("field3").field("type", "long").endObject().startObject("field4").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject()));
    int numDocs = 2 + randomInt(512);
    int scrollRequestSize = randomIntBetween(1, rarely() ? numDocs : numDocs / 2);
    boolean unevenRouting = randomBoolean();
    int numMissingDocs = scaledRandomIntBetween(0, numDocs / 100);
    IntHashSet missingDocs = new IntHashSet(numMissingDocs);
    for (int i = 0; i < numMissingDocs; i++) {
        while (!missingDocs.add(randomInt(numDocs))) {
        }
    }
    for (int i = 1; i <= numDocs; i++) {
        IndexRequestBuilder indexRequestBuilder = client().prepareIndex("index").setId(String.valueOf(i));
        if (missingDocs.contains(i)) {
            indexRequestBuilder.setSource("x", "y");
        } else {
            indexRequestBuilder.setSource(jsonBuilder().startObject().field("field1", i).field("field2", String.valueOf(i)).startObject("nested").field("field3", i).field("field4", String.valueOf(i)).endObject().endObject());
        }
        if (unevenRouting && randomInt(3) <= 2) {
            indexRequestBuilder.setRouting("a");
        }
        indexRandom(false, indexRequestBuilder);
    }
    refresh();
    final SortBuilder sort;
    if (randomBoolean()) {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("field1").missing(1);
        } else {
            sort = SortBuilders.fieldSort("field2").missing("1");
        }
    } else {
        if (randomBoolean()) {
            sort = SortBuilders.fieldSort("nested.field3").missing(1);
        } else {
            sort = SortBuilders.fieldSort("nested.field4").missing("1");
        }
    }
    sort.order(randomBoolean() ? SortOrder.ASC : SortOrder.DESC);
    SearchType searchType = RandomPicks.randomFrom(random(), Arrays.asList(searchTypes));
    logger.info("numDocs={}, scrollRequestSize={}, sort={}, searchType={}", numDocs, scrollRequestSize, sort, searchType);
    return new TestContext(numDocs, scrollRequestSize, sort, searchType);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SortBuilder(org.opensearch.search.sort.SortBuilder) IntHashSet(com.carrotsearch.hppc.IntHashSet) SearchType(org.opensearch.action.search.SearchType)

Example 5 with SearchType

use of org.opensearch.action.search.SearchType in project OpenSearch by opensearch-project.

the class TransportRankEvalActionTests method testTransferRequestParameters.

/**
 * Test that request parameters like indicesOptions or searchType from ranking evaluation request are transfered to msearch request
 */
public void testTransferRequestParameters() throws Exception {
    String indexName = "test_index";
    List<RatedRequest> specifications = new ArrayList<>();
    specifications.add(new RatedRequest("amsterdam_query", Arrays.asList(new RatedDocument(indexName, "1", 3)), new SearchSourceBuilder()));
    RankEvalRequest rankEvalRequest = new RankEvalRequest(new RankEvalSpec(specifications, new DiscountedCumulativeGain()), new String[] { indexName });
    SearchType expectedSearchType = randomFrom(SearchType.CURRENTLY_SUPPORTED);
    rankEvalRequest.searchType(expectedSearchType);
    IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    rankEvalRequest.indicesOptions(expectedIndicesOptions);
    NodeClient client = new NodeClient(settings, null) {

        @Override
        public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) {
            assertEquals(1, request.requests().size());
            assertEquals(expectedSearchType, request.requests().get(0).searchType());
            assertArrayEquals(new String[] { indexName }, request.requests().get(0).indices());
            assertEquals(expectedIndicesOptions, request.requests().get(0).indicesOptions());
        }
    };
    TransportRankEvalAction action = new TransportRankEvalAction(mock(ActionFilters.class), client, mock(TransportService.class), mock(ScriptService.class), NamedXContentRegistry.EMPTY);
    action.doExecute(null, rankEvalRequest, null);
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ArrayList(java.util.ArrayList) ActionFilters(org.opensearch.action.support.ActionFilters) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) ScriptService(org.opensearch.script.ScriptService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchType(org.opensearch.action.search.SearchType) IndicesOptions(org.opensearch.action.support.IndicesOptions)

Aggregations

SearchType (org.opensearch.action.search.SearchType)6 Matchers.containsString (org.hamcrest.Matchers.containsString)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 ActionListener (org.opensearch.action.ActionListener)1 IndicesStatsResponse (org.opensearch.action.admin.indices.stats.IndicesStatsResponse)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 IndicesOptions (org.opensearch.action.support.IndicesOptions)1 NodeClient (org.opensearch.client.node.NodeClient)1 ScriptService (org.opensearch.script.ScriptService)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1 SortBuilder (org.opensearch.search.sort.SortBuilder)1 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)1 TransportService (org.opensearch.transport.TransportService)1