Search in sources :

Example 1 with TermsFacet

use of org.elasticsearch.search.facet.terms.TermsFacet in project elasticsearch-opennlp-plugin by spinscale.

the class OpenNlpPluginIntegrationTest method testThatFacetingIsWorking.

@Test
public void testThatFacetingIsWorking() throws Exception {
    putMapping("/test-mapping-analyzers.json");
    String sampleText = copyToStringFromClasspath("/sample-text.txt");
    IndexResponse indexResponse = indexElement(sampleText);
    SearchResponse searchResponse = new SearchRequestBuilder(node.client()).setIndices(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).addFacet(new TermsFacetBuilder("names").field("someFieldAnalyzed.name").order(TermsFacet.ComparatorType.TERM)).execute().actionGet();
    assertThat(searchResponse.getHits().totalHits(), is(1L));
    assertThat(searchResponse.getHits().getAt(0).id(), is(indexResponse.getId()));
    TermsFacet termsFacet = searchResponse.getFacets().facet(TermsFacet.class, "names");
    assertThat(termsFacet.getTotalCount(), is(2L));
    assertThat(termsFacet.getEntries().get(0).getTerm().string(), is("Jack Nicholson"));
    assertThat(termsFacet.getEntries().get(1).getTerm().string(), is("Kobe Bryant"));
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsFacetBuilder(org.elasticsearch.search.facet.terms.TermsFacetBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) TermsFacet(org.elasticsearch.search.facet.terms.TermsFacet) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 TermsFacet (org.elasticsearch.search.facet.terms.TermsFacet)1 TermsFacetBuilder (org.elasticsearch.search.facet.terms.TermsFacetBuilder)1 Test (org.junit.Test)1