0

I've been working to change my query to a filter in Elasticsearch using the java api. I've made sure that the fields I am running searches on are set to "not_analzed".

Here is the java code for the filter:

FilterBuilder andFilter = FilterBuilders.andFilter(FilterBuilders.termFilter("thread_name", keyword), FilterBuilders.termFilter("site_name", "test_site"));

    QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), andFilter);

    SearchResponse threadResponse = client.prepareSearch("thread_and_messages").setQuery(filteredQuery).setSize(20).setFrom(firstRowOffset).execute().actionGet();

This then gets translated to this in JSON:

"filtered" : {
"query" : {
  "match_all" : { }
},
"filter" : {
  "and" : {
    "filters" : [ {
      "term" : {
        "thread_name" : "apple"
      }
    }, {
      "term" : {
        "site_name" : "test_site"
      }
    } ]
  }
}
}

When I try to do any searches it won't return the document I'm looking for which is thread_name: Apple and site_name: test_site.

Also when I run this using a curl command it won't find that document either.

Can anyone see what I'm doing wrong here?

8
  • Are you searching for apple or Apple. Please note the case. Commented Mar 17, 2016 at 11:26
  • Either, the thread_name field isn't analyzed so I assumed case wouldn't be an issue Commented Mar 17, 2016 at 11:35
  • That is the issue.. If it is not-analyzed. Case matters in term query. It looks for exact match. Commented Mar 17, 2016 at 11:37
  • So if I set it be analyzed this would sort out the issue? Commented Mar 17, 2016 at 11:39
  • On analyzed you will not be able to search for exact term. i.e . if you have red apple. you won't be able to search for red apple using term query. Commented Mar 17, 2016 at 11:41

1 Answer 1

1

You can use must clause.That will perform and operation

 "query": {
    "bool": {
        "must": [
           {
               "query_string": {
                  "default_field": "thread_name",
                  "query": "apple"
               }
           },
           {
               "query_string": {
                  "default_field": "site_name",
                  "query": "test_site"
               }

           }
        ]
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.