How to do multiple filter query in Elasticsearch using Java?

谁说我不能喝 提交于 2020-01-02 06:10:09

问题


I have build a web app on top of elasticsearch. I would like to do a multiple filter using Java.

Elasticsearch Query:

{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "T": "TEXT"},
          "match": {
            "new_content": "TEXT"
          }
        },

      ],
      "filter": {
        "term": {
          "collection1": "xyz"
        },
        "term": {
          "collection2": "abc"
        }

I want to filter the query such that it should filter on the same field collection with two different values(for eg,"xyz" and "abc")

Right now, I have coded a Java program for the single filter.

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

How should I filter a query on a same field for multiple values?

Edit:

Actually in my web app, I have extracted the value of the collection as:

String[] Collection=request.getParameterValues("site");

Collection is the field in my elasticsearch documents. Suppose Collection[0] is germany_collection(value:true), Collection[1] is usa_collection(value:true). I want to write a query which filters my documents such that I will get results when germany_collection:true and usa_collection:true.

This is my original program:

public StringBuffer getJson(String query, String start, String [] Collection{
Client client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

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

But when I did applied this filter, I am getting this error The method filter(TermsQueryBuilder) is undefined for the type TermsQueryBuilder. It says add cast to method reciever.


回答1:


You can use the terms query instead of multiple term queries:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.simpleQueryStringQuery(query).field("newContent").field("T"))
                .filter(QueryBuilders.termsQuery(Collection, "abc", "xyz"));
                                          ^                    ^      ^
                                          |                    |      |
                                    use termsQuery      with multiple values

UPDATE

I'm surprised your code compiles at all because you're missing some closing parenthesis, just rewrite your query like this and it should work:

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


来源:https://stackoverflow.com/questions/38062523/how-to-do-multiple-filter-query-in-elasticsearch-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!