Package org.elasticsearch.action.search

Examples of org.elasticsearch.action.search.SearchResponse


        System.out.println("--> Number of docs in index: " + client.prepareCount().setQuery(matchAllQuery()).execute().actionGet().count());

        System.out.println("--> Running just child query");
        // run just the child query, warm up first
        for (int j = 0; j < QUERY_WARMUP; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(termQuery("child.tag", "tag1")).execute().actionGet();
            if (j == 0) {
                System.out.println("--> Warmup took: " + searchResponse.took());
            }
            if (searchResponse.hits().totalHits() != COUNT) {
                System.err.println("--> mismatch on hits");
            }
        }

        long totalQueryTime = 0;
        for (int j = 0; j < QUERY_COUNT; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(termQuery("child.tag", "tag1")).execute().actionGet();
            if (searchResponse.hits().totalHits() != COUNT) {
                System.err.println("--> mismatch on hits");
            }
            totalQueryTime += searchResponse.tookInMillis();
        }
        System.out.println("--> Just Child Query Avg: " + (totalQueryTime / QUERY_COUNT) + "ms");

        System.out.println("--> Running has_child query");
        // run parent child constant query
        for (int j = 0; j < QUERY_WARMUP; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(hasChildQuery("child", termQuery("tag", "tag1"))).execute().actionGet();
            if (searchResponse.failedShards() > 0) {
                System.err.println("Search Failures " + Arrays.toString(searchResponse.shardFailures()));
            }
            if (searchResponse.hits().totalHits() != COUNT) {
                System.err.println("--> mismatch on hits [" + j + "], got [" + searchResponse.hits().totalHits() + "], expected [" + COUNT + "]");
            }
        }

        totalQueryTime = 0;
        for (int j = 0; j < QUERY_COUNT; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(hasChildQuery("child", termQuery("tag", "tag1"))).execute().actionGet();
            if (searchResponse.failedShards() > 0) {
                System.err.println("Search Failures " + Arrays.toString(searchResponse.shardFailures()));
            }
            if (searchResponse.hits().totalHits() != COUNT) {
                System.err.println("--> mismatch on hits [" + j + "], got [" + searchResponse.hits().totalHits() + "], expected [" + COUNT + "]");
            }
            totalQueryTime += searchResponse.tookInMillis();
        }
        System.out.println("--> has_child Query Avg: " + (totalQueryTime / QUERY_COUNT) + "ms");

        System.out.println("--> Running top_children query");
        // run parent child score query
        for (int j = 0; j < QUERY_WARMUP; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(topChildrenQuery("child", termQuery("tag", "tag1"))).execute().actionGet();
            // we expect to have mismatch on hits here
//            if (searchResponse.hits().totalHits() != COUNT) {
//                System.err.println("mismatch on hits");
//            }
        }

        totalQueryTime = 0;
        for (int j = 0; j < QUERY_COUNT; j++) {
            SearchResponse searchResponse = client.prepareSearch().setQuery(topChildrenQuery("child", termQuery("tag", "tag1"))).execute().actionGet();
            // we expect to have mismatch on hits here
//            if (searchResponse.hits().totalHits() != COUNT) {
//                System.err.println("mismatch on hits");
//            }
            totalQueryTime += searchResponse.tookInMillis();
        }
        System.out.println("--> top_children Query Avg: " + (totalQueryTime / QUERY_COUNT) + "ms");

        clientNode.close();
View Full Code Here


                for (; counter < max; counter++) {
                    Client client = client(counter);
                    QueryBuilder query = termQuery("num", counter % fieldNumLimit);
                    query = constantScoreQuery(queryFilter(query));

                    SearchResponse search = client.search(searchRequest()
                            .source(searchSource().query(query)))
                            .actionGet();
//                    System.out.println("Got search response, hits [" + search.hits().totalHits() + "]");
                }
            } catch (Exception e) {
View Full Code Here

     */
    public List<String> executeSearch(String source) {
        SearchRequest request = Requests.searchRequest(INDEX_NAME).source(source);

        List<ShardSearchFailure> failures;
        SearchResponse response;

        response = client.search(request).actionGet();
        failures = Arrays.asList(response.getShardFailures());

        // throw an exception so that we see the shard failures
        if (failures.size() != 0) {
            String failuresStr = failures.toString();
            if (!failuresStr.contains("reason [No active shards]")) {
                throw new RuntimeException(failures.toString());
            }
        }

        ArrayList<String> results = new ArrayList<String>();
        if (response != null) {
            for (SearchHit hit : response.hits()) {
                String sourceStr = hit.sourceAsString();
                results.add(sourceStr);
            }
        }
        return results;
View Full Code Here

                logger.warn("count does not match!");
            }
        }

        // scan all the docs, verify all have the same version based on the number of replicas
        SearchResponse searchResponse = client.client().prepareSearch()
                .setSearchType(SearchType.SCAN)
                .setQuery(matchAllQuery())
                .setSize(50)
                .setScroll(TimeValue.timeValueMinutes(2))
                .execute().actionGet();
        logger.info("Verifying versions for {} hits...", searchResponse.hits().totalHits());

        while (true) {
            searchResponse = client.client().prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
            if (searchResponse.failedShards() > 0) {
                logger.warn("Search Failures " + Arrays.toString(searchResponse.shardFailures()));
            }
            for (SearchHit hit : searchResponse.hits()) {
                long version = -1;
                for (int i = 0; i < (numberOfReplicas + 1); i++) {
                    GetResponse getResponse = client.client().prepareGet(hit.index(), hit.type(), hit.id()).execute().actionGet();
                    if (version == -1) {
                        version = getResponse.version();
                    } else {
                        if (version != getResponse.version()) {
                            logger.warn("Doc {} has different version numbers {} and {}", hit.id(), version, getResponse.version());
                        }
                    }
                }
            }
            if (searchResponse.hits().hits().length == 0) {
                break;
            }
        }
        logger.info("Done verifying versions");
View Full Code Here

        System.out.println("done indexing, verifying docs");
        client.client().admin().indices().prepareRefresh().execute().actionGet();
        for (int i = 0; i < NUMBER_OF_DOCS; i++) {
            String id = Integer.toString(i);
            for (int j = 0; j < 5; j++) {
                SearchResponse response = client.client().prepareSearch().setQuery(QueryBuilders.termQuery("_id", id)).execute().actionGet();
                if (response.hits().totalHits() > 1) {
                    System.err.println("[" + i + "] FAIL, HITS [" + response.hits().totalHits() + "]");
                }
            }
            GetResponse getResponse = client.client().prepareGet("test", "type1", id).execute().actionGet();
            if (getResponse.exists()) {
                long version = getResponse.version();
View Full Code Here

                        builder.setFrom(size / 2);
                    }
                    String value = nextFieldValue();
                    builder.setQuery(termQuery("field", value));
                    searchCounter.incrementAndGet();
                    SearchResponse searchResponse = builder.execute().actionGet();
                    if (searchResponse.failedShards() > 0) {
                        logger.warn("failed search " + Arrays.toString(searchResponse.shardFailures()));
                    }
                    // verify that all come from the requested index
                    for (SearchHit hit : searchResponse.hits()) {
                        if (!hit.shard().index().equals(indexName)) {
                            logger.warn("got wrong index, asked for [{}], got [{}]", indexName, hit.shard().index());
                        }
                    }
                    // verify that all has the relevant value
                    for (SearchHit hit : searchResponse.hits()) {
                        if (!value.equals(hit.sourceAsMap().get("field"))) {
                            logger.warn("got wrong field, asked for [{}], got [{}]", value, hit.sourceAsMap().get("field"));
                        }
                    }
                    Thread.sleep(searcherThrottle.millis());
View Full Code Here

            }

            // verify search
            for (int i = 0; i < (nodes.length * 5); i++) {
                // do a search with norms field, so we don't rely on match all filtering cache
                SearchResponse search = client.client().prepareSearch().setQuery(matchAllQuery().normsField("field")).execute().actionGet();
                logger.debug("index_count [{}], expected_count [{}]", search.hits().totalHits(), indexCounter.get());
                if (count.count() != indexCounter.get()) {
                    logger.warn("!!! search does not match, index_count [{}], expected_count [{}]", search.hits().totalHits(), indexCounter.get());
                    throw new Exception("failed test, count does not match...");
                }
            }

            logger.info("*** ROUND {}", ++numberOfRounds);
View Full Code Here

                .startObject("location").field("lat", 40.65).field("lon", -73.95).endObject()
                .endObject()).execute().actionGet();

        client.admin().indices().prepareRefresh().execute().actionGet();

        SearchResponse searchResponse = client.prepareSearch() // from NY
                .setQuery(filteredQuery(matchAllQuery(), geoBoundingBoxFilter("location").topLeft(40.73, -74.1).bottomRight(40.717, -73.99)))
                .execute().actionGet();
        assertThat(searchResponse.hits().getTotalHits(), equalTo(2l));
        assertThat(searchResponse.hits().hits().length, equalTo(2));
        for (SearchHit hit : searchResponse.hits()) {
            System.err.println("-->" + hit.id());
            assertThat(hit.id(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
        }
    }
View Full Code Here

                .startObject("location").field("lat", -10).field("lon", 170).endObject()
                .endObject()).execute().actionGet();

        client.admin().indices().prepareRefresh().execute().actionGet();

        SearchResponse searchResponse = client.prepareSearch()
                .setQuery(filteredQuery(matchAllQuery(), geoBoundingBoxFilter("location").topLeft(41, -11).bottomRight(40, 9)))
                .execute().actionGet();
        assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
        assertThat(searchResponse.hits().hits().length, equalTo(1));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("2"));

        searchResponse = client.prepareSearch()
                .setQuery(filteredQuery(matchAllQuery(), geoBoundingBoxFilter("location").topLeft(41, -9).bottomRight(40, 11)))
                .execute().actionGet();
        assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
        assertThat(searchResponse.hits().hits().length, equalTo(1));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("3"));

        searchResponse = client.prepareSearch()
                .setQuery(filteredQuery(matchAllQuery(), geoBoundingBoxFilter("location").topLeft(11, 171).bottomRight(1, -169)))
                .execute().actionGet();
        assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
        assertThat(searchResponse.hits().hits().length, equalTo(1));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("5"));

        searchResponse = client.prepareSearch()
                .setQuery(filteredQuery(matchAllQuery(), geoBoundingBoxFilter("location").topLeft(9, 169).bottomRight(-1, -171)))
                .execute().actionGet();
        assertThat(searchResponse.hits().getTotalHits(), equalTo(1l));
        assertThat(searchResponse.hits().hits().length, equalTo(1));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("9"));
    }
View Full Code Here

        assertThat(refreshResponse.totalShards(), equalTo(9));
        assertThat(refreshResponse.successfulShards(), equalTo(3));
        assertThat(refreshResponse.failedShards(), equalTo(0));
        for (int i = 0; i < 5; i++) {
            try {
                SearchResponse searchResponse = client("server1").search(searchRequest("test").source(Unicode.fromStringAsBytes("{ xxx }"))).actionGet();
                assertThat(searchResponse.totalShards(), equalTo(3));
                assertThat(searchResponse.successfulShards(), equalTo(0));
                assertThat(searchResponse.failedShards(), equalTo(3));
                assert false : "search should fail";
            } catch (ElasticSearchException e) {
                assertThat(e.unwrapCause(), instanceOf(SearchPhaseExecutionException.class));
                // all is well
            }
        }

        startNode("server2");
        assertThat(client("server1").admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet().timedOut(), equalTo(false));

        logger.info("Running Cluster Health");
        ClusterHealthResponse clusterHealth = client("server1").admin().cluster().health(clusterHealthRequest("test")
                .waitForYellowStatus().waitForRelocatingShards(0).waitForActiveShards(6)).actionGet();
        logger.info("Done Cluster Health, status " + clusterHealth.status());
        assertThat(clusterHealth.timedOut(), equalTo(false));
        assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.YELLOW));
        assertThat(clusterHealth.activeShards(), equalTo(6));

        refreshResponse = client("server1").admin().indices().refresh(refreshRequest("test")).actionGet();
        assertThat(refreshResponse.totalShards(), equalTo(9));
        assertThat(refreshResponse.successfulShards(), equalTo(6));
        assertThat(refreshResponse.failedShards(), equalTo(0));

        for (int i = 0; i < 5; i++) {
            try {
                SearchResponse searchResponse = client("server1").search(searchRequest("test").source(Unicode.fromStringAsBytes("{ xxx }"))).actionGet();
                assertThat(searchResponse.totalShards(), equalTo(3));
                assertThat(searchResponse.successfulShards(), equalTo(0));
                assertThat(searchResponse.failedShards(), equalTo(3));
                assert false : "search should fail";
            } catch (ElasticSearchException e) {
                assertThat(e.unwrapCause(), instanceOf(SearchPhaseExecutionException.class));
                // all is well
            }
View Full Code Here

TOP

Related Classes of org.elasticsearch.action.search.SearchResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.