2

I have built a web app on top of elasticsearch (v2.3.3). To filter the query, I am using post filter of elasticsearch. But I came to know that, if I use post filter then the performance benefit of filtering will be lost since I am not using any aggregation or differential filtering. (Reference: https://www.elastic.co/guide/en/elasticsearch/guide/current/_post_filter.html)

This is how my elasticsearch client looks like:

Client client = TransportClient.builder().build().addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),
            9300));
    SearchResponse response = client.prepareSearch("index_name")
        .setTypes("index_type")
        .setQuery(QueryBuilders.simpleQueryStringQuery(query)
            .field("newContent").field("T"))
        .setPostFilter(QueryBuilders.termQuery(Collection, true))
        .setFetchSource(new String[] { "U", "UE", "UD", "T" }, null)
        .setVersion(true).addHighlightedField("newContent").setFrom(0)
        .setSize(10).execute().actionGet();

I have also read that filtered query is depreciated in elasticsearch 2.x versions. Is there any other way which will help me to apply a filter before the query is executed? I might be missing something obvious. I would appreciate your help.

Rose
  • 1,490
  • 5
  • 25
  • 56

1 Answers1

5

You simply need to bring the filter present in post filter inside a bool/filter query. Try to do hits instead:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
    .must(QueryBuilders.simpleQueryStringQuery(query)
              .field("newContent").field("T"))
    .filter(QueryBuilders.termQuery(Collection, true));

SearchResponse response = client.prepareSearch("index_name")
    .setTypes("index_type")
    .setQuery(boolQuery)
    .setFetchSource(new String[] { "U", "UE", "UD", "T" }, null)
    .setVersion(true).addHighlightedField("newContent").setFrom(0)
    .setSize(10).execute().actionGet();
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks. I will try this and let you know – Rose Jun 24 '16 at 04:38
  • Hi Val, I am trying to use your solution. For BoolQueryBuilder, I have imported "import org.elasticsearch.index.query.BoolQueryBuilder" package. For OueryBuilder, I have imported, import "org.elasticsearch.index.query.BoolQueryBuilder" . I have also tried some other imports. But I am getting an error which says "The method boolQuery() is undefined for the type MultiMatchQuery.QueryBuilder". Is there any other imports that I'm missing? – Rose Jun 24 '16 at 16:28
  • 2
    Sorry I had a typo `QueryBuilder` should be `QueryBuilders`. my bad, I fixed it – Val Jun 24 '16 at 16:29
  • Thanks. I am trying it now – Rose Jun 24 '16 at 16:31
  • Hi Val, Do you know how to do filters on the same field with multiple values? I have posted a question on this topic: "http://stackoverflow.com/questions/38062523/how-to-do-multiple-filter-query-in-elasticsearch-using-java". Can you refer me some links where I can find a good documentation on Elasticsearch using Java API? – Rose Jun 27 '16 at 21:03