Search in sources :

Example 6 with TermsQueryBuilder

use of org.elasticsearch.index.query.TermsQueryBuilder in project elasticsearch by elastic.

the class ShrinkIndexIT method testCreateShrinkIndexToN.

public void testCreateShrinkIndexToN() {
    int[][] possibleShardSplits = new int[][] { { 8, 4, 2 }, { 9, 3, 1 }, { 4, 2, 1 }, { 15, 5, 1 } };
    int[] shardSplits = randomFrom(possibleShardSplits);
    assertEquals(shardSplits[0], (shardSplits[0] / shardSplits[1]) * shardSplits[1]);
    assertEquals(shardSplits[1], (shardSplits[1] / shardSplits[2]) * shardSplits[2]);
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", shardSplits[0])).get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source", "t1", Integer.toString(i)).setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    String mergeNode = discoveryNodes[0].getName();
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();
    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
    ensureGreen();
    // now merge source into a 4 shard index
    assertAcked(client().admin().indices().prepareShrinkIndex("source", "first_shrink").setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", shardSplits[1]).build()).get());
    ensureGreen();
    assertHitCount(client().prepareSearch("first_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    for (int i = 0; i < 20; i++) {
        // now update
        client().prepareIndex("first_shrink", "t1", Integer.toString(i)).setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertHitCount(client().prepareSearch("first_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    assertHitCount(client().prepareSearch("source").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("first_shrink").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
    ensureGreen();
    // now merge source into a 2 shard index
    assertAcked(client().admin().indices().prepareShrinkIndex("first_shrink", "second_shrink").setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", shardSplits[2]).build()).get());
    ensureGreen();
    assertHitCount(client().prepareSearch("second_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    // let it be allocated anywhere and bump replicas
    client().admin().indices().prepareUpdateSettings("second_shrink").setSettings(Settings.builder().putNull("index.routing.allocation.include._id").put("index.number_of_replicas", 1)).get();
    ensureGreen();
    assertHitCount(client().prepareSearch("second_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    for (int i = 0; i < 20; i++) {
        // now update
        client().prepareIndex("second_shrink", "t1", Integer.toString(i)).setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    flushAndRefresh();
    assertHitCount(client().prepareSearch("second_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    assertHitCount(client().prepareSearch("first_shrink").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
    assertHitCount(client().prepareSearch("source").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder)

Example 7 with TermsQueryBuilder

use of org.elasticsearch.index.query.TermsQueryBuilder in project elasticsearch by elastic.

the class RemoteClusterServiceTests method testProcessRemoteShards.

public void testProcessRemoteShards() throws IOException {
    try (RemoteClusterService service = new RemoteClusterService(Settings.EMPTY, null)) {
        assertFalse(service.isCrossClusterSearchEnabled());
        List<ShardIterator> iteratorList = new ArrayList<>();
        Map<String, ClusterSearchShardsResponse> searchShardsResponseMap = new HashMap<>();
        DiscoveryNode[] nodes = new DiscoveryNode[] { new DiscoveryNode("node1", buildNewFakeTransportAddress(), Version.CURRENT), new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT) };
        Map<String, AliasFilter> indicesAndAliases = new HashMap<>();
        indicesAndAliases.put("foo", new AliasFilter(new TermsQueryBuilder("foo", "bar"), Strings.EMPTY_ARRAY));
        indicesAndAliases.put("bar", new AliasFilter(new MatchAllQueryBuilder(), Strings.EMPTY_ARRAY));
        ClusterSearchShardsGroup[] groups = new ClusterSearchShardsGroup[] { new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 0), new ShardRouting[] { TestShardRouting.newShardRouting("foo", 0, "node1", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("foo", 0, "node2", false, ShardRoutingState.STARTED) }), new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 1), new ShardRouting[] { TestShardRouting.newShardRouting("foo", 0, "node1", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("foo", 1, "node2", false, ShardRoutingState.STARTED) }), new ClusterSearchShardsGroup(new ShardId("bar", "bar_id", 0), new ShardRouting[] { TestShardRouting.newShardRouting("bar", 0, "node2", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("bar", 0, "node1", false, ShardRoutingState.STARTED) }) };
        searchShardsResponseMap.put("test_cluster_1", new ClusterSearchShardsResponse(groups, nodes, indicesAndAliases));
        Map<String, AliasFilter> remoteAliases = new HashMap<>();
        service.processRemoteShards(searchShardsResponseMap, iteratorList, remoteAliases);
        assertEquals(3, iteratorList.size());
        for (ShardIterator iterator : iteratorList) {
            if (iterator.shardId().getIndexName().endsWith("foo")) {
                assertTrue(iterator.shardId().getId() == 0 || iterator.shardId().getId() == 1);
                assertEquals("test_cluster_1:foo", iterator.shardId().getIndexName());
                ShardRouting shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "foo");
                shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "foo");
                assertNull(iterator.nextOrNull());
            } else {
                assertEquals(0, iterator.shardId().getId());
                assertEquals("test_cluster_1:bar", iterator.shardId().getIndexName());
                ShardRouting shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "bar");
                shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "bar");
                assertNull(iterator.nextOrNull());
            }
        }
        assertEquals(2, remoteAliases.size());
        assertTrue(remoteAliases.toString(), remoteAliases.containsKey("foo_id"));
        assertTrue(remoteAliases.toString(), remoteAliases.containsKey("bar_id"));
        assertEquals(new TermsQueryBuilder("foo", "bar"), remoteAliases.get("foo_id").getQueryBuilder());
        assertEquals(new MatchAllQueryBuilder(), remoteAliases.get("bar_id").getQueryBuilder());
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ShardId(org.elasticsearch.index.shard.ShardId) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 8 with TermsQueryBuilder

use of org.elasticsearch.index.query.TermsQueryBuilder in project fess by codelibs.

the class EsAbstractConditionQuery method regTermsQ.

protected TermsQueryBuilder regTermsQ(String name, Collection<?> values) {
    checkEsInvalidQueryCollection(name, values);
    TermsQueryBuilder termsQuery = QueryBuilders.termsQuery(name, values);
    regQ(termsQuery);
    return termsQuery;
}
Also used : TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder)

Aggregations

TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4 CommonTermsQueryBuilder (org.elasticsearch.index.query.CommonTermsQueryBuilder)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Version (org.elasticsearch.Version)1 ClusterRerouteResponse (org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)1 ClusterSearchShardsGroup (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup)1 ClusterSearchShardsResponse (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 GetSettingsResponse (org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)1 InternalClusterInfoService (org.elasticsearch.cluster.InternalClusterInfoService)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1