Package org.elasticsearch.action.search

Examples of org.elasticsearch.action.search.SearchResponse$Fields


        for (int i = 0; i < 5; i++) {
            client.prepareIndex("test", "type1", Integer.toString(i))
                    .setSource("title", "This is a test on the highlighting bug present in elasticsearch").setRefresh(true).execute().actionGet();
        }

        SearchResponse search = client.prepareSearch()
                .setQuery(fieldQuery("title", "bug"))
                .addHighlightedField("title", 50, 1, 10)
                .execute().actionGet();

        assertThat(search.hits().totalHits(), equalTo(5l));
        assertThat(search.hits().hits().length, equalTo(5));
        assertThat(search.getFailedShards(), equalTo(0));

        for (SearchHit hit : search.hits()) {
            // LUCENE 3.1 UPGRADE: Caused adding the space at the end...
            assertThat(hit.highlightFields().get("title").fragments()[0], equalTo("hlighting <em>bug</em> present in elasticsearch "));
        }
    }
View Full Code Here


                .endObject()).execute().actionGet();

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

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setSearchType(SearchType.COUNT)
                    .setFacets(XContentFactory.jsonBuilder().startObject()
                            .startObject("facet1")
                            .startObject("terms")
                            .field("field", "tag")
                            .endObject()
                            .endObject()
                            .endObject().copiedBytes())
                    .execute().actionGet();

            assertThat(searchResponse.hits().totalHits(), equalTo(2l));
            assertThat(searchResponse.hits().hits().length, equalTo(0));
            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("green"), equalTo("blue")));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("green"), equalTo("blue")));
View Full Code Here

                .endObject()).execute().actionGet();

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

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10))
                    .execute().actionGet();

            assertThat(searchResponse.hits().hits().length, equalTo(2));
            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("green"), equalTo("blue")));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("green"), equalTo("blue")));
            assertThat(facet.entries().get(1).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .setFilter(termFilter("tag", "blue"))
                    .addFacet(termsFacet("facet1").field("tag").size(10))
                    .execute().actionGet();

            assertThat(searchResponse.hits().hits().length, equalTo(1));
            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("green"), equalTo("blue")));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("green"), equalTo("blue")));
View Full Code Here

                .endObject()).execute().actionGet();

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

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setSize(0)
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("stag").size(10))
                    .execute().actionGet();

            assertThat(searchResponse.hits().hits().length, equalTo(0));

            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            searchResponse = client.prepareSearch()
                    .setSearchType(SearchType.QUERY_AND_FETCH)
                    .setSize(0)
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("stag").size(10))
                    .addFacet(termsFacet("facet2").field("tag").size(10))
                    .execute().actionGet();

            assertThat(searchResponse.hits().hits().length, equalTo(0));

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
        }
View Full Code Here

                .endObject()).execute().actionGet();
        client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();


        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setSize(0)
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("_index").size(10))
                    .execute().actionGet();


            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), equalTo("test1"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), equalTo("test2"));
View Full Code Here

                .endObject()).execute().actionGet();

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

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(filterFacet("facet1").filter(termFilter("stag", "111")))
                    .addFacet(filterFacet("facet2").filter(termFilter("tag", "xxx")))
                    .addFacet(filterFacet("facet3").filter(termFilter("tag", "yyy")))
                    .execute().actionGet();

            FilterFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.count(), equalTo(2l));
        }
    }
View Full Code Here

                .field("number", 3)
                .endObject()).execute().actionGet();

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

        SearchResponse searchResponse = client.prepareSearch()
                .setQuery(filteredQuery(matchAllQuery(), orFilter(rangeFilter("number").lte(2).filterName("test1"), rangeFilter("number").gt(2).filterName("test2"))))
                .execute().actionGet();

        assertThat(searchResponse.hits().totalHits(), equalTo(3l));
        for (SearchHit hit : searchResponse.hits()) {
            if (hit.id().equals("1") || hit.id().equals("2")) {
                assertThat(hit.matchedFilters().length, equalTo(1));
                assertThat(hit.matchedFilters(), hasItemInArray("test1"));
            } else if (hit.id().equals("3")) {
                assertThat(hit.matchedFilters().length, equalTo(1));
View Full Code Here

                .field("kuku", "kuku")
                .endObject()).execute().actionGet();
        client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("stag").size(10))
                    .execute().actionGet();

            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.missingCount(), equalTo(1l));
        }
    }
View Full Code Here

                .endObject()).execute().actionGet();

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

        for (int i = 0; i < numberOfRuns(); i++) {
            SearchResponse searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("stag").size(10).executionHint(executionHint))
                    .addFacet(termsFacet("facet2").field("tag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            TermsFacet facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.getTotalCount(), equalTo(2l));
            assertThat(facet.getOtherCount(), equalTo(0l));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            facet = searchResponse.facets().facet("facet2");
            assertThat(facet.name(), equalTo("facet2"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("yyy"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            // Numeric

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("lstag").size(10).executionHint(executionHint))
                    .addFacet(termsFacet("facet2").field("ltag").size(10).executionHint(executionHint))
                    .addFacet(termsFacet("facet3").field("ltag").size(10).exclude(3000).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet, instanceOf(InternalLongTermsFacet.class));
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            facet = searchResponse.facets().facet("facet2");
            assertThat(facet, instanceOf(InternalLongTermsFacet.class));
            assertThat(facet.name(), equalTo("facet2"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("2000"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("1000"), equalTo("3000")));
            assertThat(facet.entries().get(1).count(), equalTo(1));
            assertThat(facet.entries().get(2).term(), anyOf(equalTo("1000"), equalTo("3000")));
            assertThat(facet.entries().get(2).count(), equalTo(1));

            facet = searchResponse.facets().facet("facet3");
            assertThat(facet, instanceOf(InternalLongTermsFacet.class));
            assertThat(facet.name(), equalTo("facet3"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), equalTo("2000"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), equalTo("1000"));
            assertThat(facet.entries().get(1).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("dstag").size(10).executionHint(executionHint))
                    .addFacet(termsFacet("facet2").field("dtag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet, instanceOf(InternalDoubleTermsFacet.class));
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111.1"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            facet = searchResponse.facets().facet("facet2");
            assertThat(facet, instanceOf(InternalDoubleTermsFacet.class));
            assertThat(facet.name(), equalTo("facet2"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("2000.1"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("1000.1"), equalTo("3000.1")));
            assertThat(facet.entries().get(1).count(), equalTo(1));
            assertThat(facet.entries().get(2).term(), anyOf(equalTo("1000.1"), equalTo("3000.1")));
            assertThat(facet.entries().get(2).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("bstag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet, instanceOf(InternalByteTermsFacet.class));
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("istag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet, instanceOf(InternalIntTermsFacet.class));
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("stag", "111"))
                    .addFacet(termsFacet("facet1").field("shstag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet, instanceOf(InternalShortTermsFacet.class));
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            // Test Facet Filter

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(1));

            // now with global
            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).global(true).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(1));

            // Test Facet Filter (with a type)

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("type1.stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("yyy"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
            assertThat(facet.entries().get(1).count(), equalTo(1));
            assertThat(facet.entries().get(2).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
            assertThat(facet.entries().get(2).count(), equalTo(1));

            // Bounded Size

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(2).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), equalTo("yyy"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
            assertThat(facet.entries().get(1).count(), equalTo(1));

            // Test Exclude

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).exclude("yyy").executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
            assertThat(facet.entries().get(1).count(), equalTo(1));

            // Test Order

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("xxx"));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), equalTo("yyy"));
            assertThat(facet.entries().get(1).count(), equalTo(2));
            assertThat(facet.entries().get(2).term(), equalTo("zzz"));
            assertThat(facet.entries().get(2).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.REVERSE_TERM).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(2).term(), equalTo("xxx"));
            assertThat(facet.entries().get(2).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), equalTo("yyy"));
            assertThat(facet.entries().get(1).count(), equalTo(2));
            assertThat(facet.entries().get(0).term(), equalTo("zzz"));
            assertThat(facet.entries().get(0).count(), equalTo(1));

            // Script

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).script("term + param1").param("param1", "a").order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("xxxa"));
            assertThat(facet.entries().get(0).count(), equalTo(1));
            assertThat(facet.entries().get(1).term(), equalTo("yyya"));
            assertThat(facet.entries().get(1).count(), equalTo(2));
            assertThat(facet.entries().get(2).term(), equalTo("zzza"));
            assertThat(facet.entries().get(2).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").field("tag").size(10).script("term == 'xxx' ? false : true").order(TermsFacet.ComparatorType.TERM).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(2));
            assertThat(facet.entries().get(0).term(), equalTo("yyy"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), equalTo("zzz"));
            assertThat(facet.entries().get(1).count(), equalTo(1));

            // Fields Facets

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").fields("stag", "tag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(4));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("111"), equalTo("yyy")));
            assertThat(facet.entries().get(0).count(), equalTo(2));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("111"), equalTo("yyy")));
            assertThat(facet.entries().get(1).count(), equalTo(2));
            assertThat(facet.entries().get(2).term(), anyOf(equalTo("zzz"), equalTo("xxx")));
            assertThat(facet.entries().get(2).count(), equalTo(1));
            assertThat(facet.entries().get(3).term(), anyOf(equalTo("zzz"), equalTo("xxx")));
            assertThat(facet.entries().get(3).count(), equalTo(1));

            searchResponse = client.prepareSearch()
                    .setQuery(termQuery("xxx", "yyy")) // don't match anything
                    .addFacet(termsFacet("facet1").field("tag").size(10).allTerms(true).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
            assertThat(facet.entries().get(0).count(), equalTo(0));
            assertThat(facet.entries().get(1).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
            assertThat(facet.entries().get(1).count(), equalTo(0));
            assertThat(facet.entries().get(2).term(), anyOf(equalTo("xxx"), equalTo("yyy"), equalTo("zzz")));
            assertThat(facet.entries().get(2).count(), equalTo(0));

            // Script Field

            searchResponse = client.prepareSearch()
                    .setQuery(matchAllQuery())
                    .addFacet(termsFacet("facet1").scriptField("_source.stag").size(10).executionHint(executionHint))
                    .addFacet(termsFacet("facet2").scriptField("_source.tag").size(10).executionHint(executionHint))
                    .execute().actionGet();

            facet = searchResponse.facets().facet("facet1");
            assertThat(facet.name(), equalTo("facet1"));
            assertThat(facet.entries().size(), equalTo(1));
            assertThat(facet.entries().get(0).term(), equalTo("111"));
            assertThat(facet.entries().get(0).count(), equalTo(2));

            facet = searchResponse.facets().facet("facet2");
            assertThat(facet.name(), equalTo("facet2"));
            assertThat(facet.entries().size(), equalTo(3));
            assertThat(facet.entries().get(0).term(), equalTo("yyy"));
            assertThat(facet.entries().get(0).count(), equalTo(2));
        }
View Full Code Here

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

        // TOP CHILDREN QUERY

        SearchResponse searchResponse = client.prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "yellow"))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p1"));

        searchResponse = client.prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "blue"))).execute().actionGet();
        if (searchResponse.failedShards() > 0) {
            logger.warn("Failed shards:");
            for (ShardSearchFailure shardSearchFailure : searchResponse.shardFailures()) {
                logger.warn("-> {}", shardSearchFailure);
            }
        }
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p2"));

        searchResponse = client.prepareSearch("test").setQuery(topChildrenQuery("child", termQuery("c_field", "red"))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(2l));
        assertThat(searchResponse.hits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1")));
        assertThat(searchResponse.hits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1")));

        // HAS CHILD QUERY

        searchResponse = client.prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "yellow"))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p1"));

        searchResponse = client.prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "blue"))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p2"));

        searchResponse = client.prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "red"))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(2l));
        assertThat(searchResponse.hits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1")));
        assertThat(searchResponse.hits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1")));

        // HAS CHILD FILTER

        searchResponse = client.prepareSearch("test").setQuery(constantScoreQuery(hasChildFilter("child", termQuery("c_field", "yellow")))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p1"));

        searchResponse = client.prepareSearch("test").setQuery(constantScoreQuery(hasChildFilter("child", termQuery("c_field", "blue")))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(1l));
        assertThat(searchResponse.hits().getAt(0).id(), equalTo("p2"));

        searchResponse = client.prepareSearch("test").setQuery(constantScoreQuery(hasChildFilter("child", termQuery("c_field", "red")))).execute().actionGet();
        assertThat("Failures " + Arrays.toString(searchResponse.shardFailures()), searchResponse.shardFailures().length, equalTo(0));
        assertThat(searchResponse.failedShards(), equalTo(0));
        assertThat(searchResponse.hits().totalHits(), equalTo(2l));
        assertThat(searchResponse.hits().getAt(0).id(), anyOf(equalTo("p2"), equalTo("p1")));
        assertThat(searchResponse.hits().getAt(1).id(), anyOf(equalTo("p2"), equalTo("p1")));
    }
View Full Code Here

TOP

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

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.