Elasticsearch query shows wrong results

250 Views Asked by At

I am using below query to get the elastic search results in php.

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "q= (\"Agriculture\" OR \"Agribusiness\" OR \"Agriculture\") et_tax= \"Agriculture\" -et_tax:\"Agriculture\"" 
        }
      }
    }
  },
 "aggs": {
    "lawfirmcount": {
      "cardinality": {
        "field": "pd_lawfirmID",
        "precision_threshold": 1250 
      }
    }
  }  
}

When is use the above code, it will fetch unmatched results. Is there anything wrong in that query.

1

There are 1 best solutions below

0
BrookeB On

It looks like you are querying on the same field, et_tax, twice. In the first case, where the term "Agriculture" is present, and then again where "Agriculture" is not present (-et_tax:). I would expect this to return all documents, since the term "Agriculture" either exists or does not exist in the field et_tax, and the default operator for query_string is OR.

Take a look at the documentation for query_string. You can change the default_operator to be AND instead.

Also, I suggest removing the double quotes from your search values, assuming they are not part of the term you are searching on.

Finally, I don't see a q field in your mapping. I don't think the "q=" in your query is working as you are expecting. You probably need to remove that and specify a default_field to query against.

Hope this helps...