Search in sources :

Example 21 with SearchModule

use of org.opensearch.search.SearchModule in project OpenSearch by opensearch-project.

the class SortBuilderTests method init.

@BeforeClass
public static void init() {
    SearchModule searchModule = new SearchModule(Settings.EMPTY, emptyList());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) BeforeClass(org.junit.BeforeClass)

Example 22 with SearchModule

use of org.opensearch.search.SearchModule in project OpenSearch by opensearch-project.

the class ExplainRequestTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    IndicesModule indicesModule = new IndicesModule(Collections.emptyList());
    SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
    List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
    entries.addAll(indicesModule.getNamedWriteables());
    entries.addAll(searchModule.getNamedWriteables());
    namedWriteableRegistry = new NamedWriteableRegistry(entries);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) IndicesModule(org.opensearch.indices.IndicesModule) ArrayList(java.util.ArrayList) SearchModule(org.opensearch.search.SearchModule)

Example 23 with SearchModule

use of org.opensearch.search.SearchModule in project OpenSearch by opensearch-project.

the class BasePipelineAggregationTestCase method setUp.

/**
 * Setup for the whole base test class.
 */
@Override
public void setUp() throws Exception {
    super.setUp();
    Settings settings = Settings.builder().put("node.name", AbstractQueryTestCase.class.toString()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
    IndicesModule indicesModule = new IndicesModule(Collections.emptyList());
    SearchModule searchModule = new SearchModule(settings, plugins());
    List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
    entries.addAll(indicesModule.getNamedWriteables());
    entries.addAll(searchModule.getNamedWriteables());
    entries.addAll(additionalNamedWriteables());
    namedWriteableRegistry = new NamedWriteableRegistry(entries);
    List<NamedXContentRegistry.Entry> xContentEntries = searchModule.getNamedXContents();
    xContentEntries.addAll(additionalNamedContents());
    xContentRegistry = new NamedXContentRegistry(xContentEntries);
    // create some random type with some default field, those types will stick around for all of the subclasses
    currentTypes = new String[randomIntBetween(0, 5)];
    for (int i = 0; i < currentTypes.length; i++) {
        String type = randomAlphaOfLengthBetween(1, 10);
        currentTypes[i] = type;
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) IndicesModule(org.opensearch.indices.IndicesModule) ArrayList(java.util.ArrayList) SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Settings(org.opensearch.common.settings.Settings) AbstractQueryTestCase(org.opensearch.test.AbstractQueryTestCase)

Example 24 with SearchModule

use of org.opensearch.search.SearchModule in project OpenSearch by opensearch-project.

the class ClusterSearchShardsResponseTests method testSerialization.

public void testSerialization() throws Exception {
    Map<String, AliasFilter> indicesAndFilters = new HashMap<>();
    Set<DiscoveryNode> nodes = new HashSet<>();
    int numShards = randomIntBetween(1, 10);
    ClusterSearchShardsGroup[] clusterSearchShardsGroups = new ClusterSearchShardsGroup[numShards];
    for (int i = 0; i < numShards; i++) {
        String index = randomAlphaOfLengthBetween(3, 10);
        ShardId shardId = new ShardId(index, randomAlphaOfLength(12), i);
        String nodeId = randomAlphaOfLength(10);
        ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, nodeId, randomBoolean(), ShardRoutingState.STARTED);
        clusterSearchShardsGroups[i] = new ClusterSearchShardsGroup(shardId, new ShardRouting[] { shardRouting });
        DiscoveryNode node = new DiscoveryNode(shardRouting.currentNodeId(), new TransportAddress(TransportAddress.META_ADDRESS, randomInt(0xFFFF)), VersionUtils.randomVersion(random()));
        nodes.add(node);
        AliasFilter aliasFilter;
        if (randomBoolean()) {
            aliasFilter = new AliasFilter(RandomQueryBuilder.createQuery(random()), "alias-" + index);
        } else {
            aliasFilter = new AliasFilter(null, Strings.EMPTY_ARRAY);
        }
        indicesAndFilters.put(index, aliasFilter);
    }
    ClusterSearchShardsResponse clusterSearchShardsResponse = new ClusterSearchShardsResponse(clusterSearchShardsGroups, nodes.toArray(new DiscoveryNode[nodes.size()]), indicesAndFilters);
    SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList());
    List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
    entries.addAll(searchModule.getNamedWriteables());
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries);
    Version version = VersionUtils.randomIndexCompatibleVersion(random());
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.setVersion(version);
        clusterSearchShardsResponse.writeTo(out);
        try (StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry)) {
            in.setVersion(version);
            ClusterSearchShardsResponse deserialized = new ClusterSearchShardsResponse(in);
            assertArrayEquals(clusterSearchShardsResponse.getNodes(), deserialized.getNodes());
            assertEquals(clusterSearchShardsResponse.getGroups().length, deserialized.getGroups().length);
            for (int i = 0; i < clusterSearchShardsResponse.getGroups().length; i++) {
                ClusterSearchShardsGroup clusterSearchShardsGroup = clusterSearchShardsResponse.getGroups()[i];
                ClusterSearchShardsGroup deserializedGroup = deserialized.getGroups()[i];
                assertEquals(clusterSearchShardsGroup.getShardId(), deserializedGroup.getShardId());
                assertArrayEquals(clusterSearchShardsGroup.getShards(), deserializedGroup.getShards());
            }
            assertEquals(clusterSearchShardsResponse.getIndicesAndFilters(), deserialized.getIndicesAndFilters());
        }
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AliasFilter(org.opensearch.search.internal.AliasFilter) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ShardId(org.opensearch.index.shard.ShardId) Version(org.opensearch.Version) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) SearchModule(org.opensearch.search.SearchModule) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) HashSet(java.util.HashSet)

Example 25 with SearchModule

use of org.opensearch.search.SearchModule in project OpenSearch by opensearch-project.

the class QueryBuilderBWCIT method testQueryBuilderBWC.

public void testQueryBuilderBWC() throws Exception {
    final String type = getOldClusterVersion().before(LegacyESVersion.V_7_0_0) ? "doc" : "_doc";
    String index = "queries";
    if (isRunningAgainstOldCluster()) {
        XContentBuilder mappingsAndSettings = jsonBuilder();
        mappingsAndSettings.startObject();
        {
            mappingsAndSettings.startObject("settings");
            mappingsAndSettings.field("number_of_shards", 1);
            mappingsAndSettings.field("number_of_replicas", 0);
            mappingsAndSettings.endObject();
        }
        {
            mappingsAndSettings.startObject("mappings");
            if (isRunningAgainstAncientCluster()) {
                mappingsAndSettings.startObject(type);
            }
            mappingsAndSettings.startObject("properties");
            {
                mappingsAndSettings.startObject("query");
                mappingsAndSettings.field("type", "percolator");
                mappingsAndSettings.endObject();
            }
            {
                mappingsAndSettings.startObject("keyword_field");
                mappingsAndSettings.field("type", "keyword");
                mappingsAndSettings.endObject();
            }
            {
                mappingsAndSettings.startObject("long_field");
                mappingsAndSettings.field("type", "long");
                mappingsAndSettings.endObject();
            }
            mappingsAndSettings.endObject();
            mappingsAndSettings.endObject();
            if (isRunningAgainstAncientCluster()) {
                mappingsAndSettings.endObject();
            }
        }
        mappingsAndSettings.endObject();
        Request request = new Request("PUT", "/" + index);
        request.setOptions(allowTypesRemovalWarnings());
        request.setJsonEntity(Strings.toString(mappingsAndSettings));
        Response rsp = client().performRequest(request);
        assertEquals(200, rsp.getStatusLine().getStatusCode());
        for (int i = 0; i < CANDIDATES.size(); i++) {
            request = new Request("PUT", "/" + index + "/" + type + "/" + Integer.toString(i));
            request.setJsonEntity((String) CANDIDATES.get(i)[0]);
            rsp = client().performRequest(request);
            assertEquals(201, rsp.getStatusLine().getStatusCode());
        }
    } else {
        NamedWriteableRegistry registry = new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables());
        for (int i = 0; i < CANDIDATES.size(); i++) {
            QueryBuilder expectedQueryBuilder = (QueryBuilder) CANDIDATES.get(i)[1];
            Request request = new Request("GET", "/" + index + "/_search");
            request.setJsonEntity("{\"query\": {\"ids\": {\"values\": [\"" + Integer.toString(i) + "\"]}}, " + "\"docvalue_fields\": [{\"field\":\"query.query_builder_field\"}]}");
            Response rsp = client().performRequest(request);
            assertEquals(200, rsp.getStatusLine().getStatusCode());
            Map<?, ?> hitRsp = (Map<?, ?>) ((List<?>) ((Map<?, ?>) toMap(rsp).get("hits")).get("hits")).get(0);
            String queryBuilderStr = (String) ((List<?>) ((Map<?, ?>) hitRsp.get("fields")).get("query.query_builder_field")).get(0);
            byte[] qbSource = Base64.getDecoder().decode(queryBuilderStr);
            try (InputStream in = new ByteArrayInputStream(qbSource, 0, qbSource.length)) {
                try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), registry)) {
                    input.setVersion(getOldClusterVersion());
                    QueryBuilder queryBuilder = input.readNamedWriteable(QueryBuilder.class);
                    assert in.read() == -1;
                    assertEquals(expectedQueryBuilder, queryBuilder);
                }
            }
        }
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.opensearch.client.Request) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) SpanTermQueryBuilder(org.opensearch.index.query.SpanTermQueryBuilder) MatchPhraseQueryBuilder(org.opensearch.index.query.MatchPhraseQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) SpanNearQueryBuilder(org.opensearch.index.query.SpanNearQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) Response(org.opensearch.client.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) SearchModule(org.opensearch.search.SearchModule) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Aggregations

SearchModule (org.opensearch.search.SearchModule)31 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)20 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)19 BeforeClass (org.junit.BeforeClass)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)6 StreamInput (org.opensearch.common.io.stream.StreamInput)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 XContentParser (org.opensearch.common.xcontent.XContentParser)5 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)4 Version (org.opensearch.Version)3 Settings (org.opensearch.common.settings.Settings)3 IndicesModule (org.opensearch.indices.IndicesModule)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)2 User (org.opensearch.commons.authuser.User)2 SearchPlugin (org.opensearch.plugins.SearchPlugin)2 AsynchronousSearchContextId (org.opensearch.search.asynchronous.context.AsynchronousSearchContextId)2