1

I have some problems with NOT operator in Elastic search, Here is my query

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "-et_tax:\"Agriculture\" -et_tax:\"Airports/Ports\"" 
        }
      }
    }
  },
 "aggs": {
    "lawfirmcount": {
      "cardinality": {
        "field": "pd_lawfirmID",
        "precision_threshold": 1250 
      }
    }
  }  
}

I am framing the query using PHP & MySQL. When i use the above it was showing wrong results, I need to exclude these results in my total results, I am expecting result is showing all the records but not in et_tax Column as Agriculture AND Airport/Ports. So how can i use the multiple NOT operator in my query.

UPDATE :

When i Pass the same query AND/OR both will show same results,

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "-et_tax:\"Agriculture\" AND  -et_tax:\"Airports/Ports\""  
        }
      }
    }
  },
 "aggs": {
    "lawfirmcount": {
      "cardinality": {
        "field": "pd_lawfirmID",
        "precision_threshold": 1250 
      }
    }
  }  
}

The above with AND operator it will show 1564 records, and below query with OR operator it also shows the same count.

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "-et_tax:\"Agriculture\" OR  -et_tax:\"Airports/Ports\""  
        }
      }
    }
  },
 "aggs": {
    "lawfirmcount": {
      "cardinality": {
        "field": "pd_lawfirmID",
        "precision_threshold": 1250 
      }
    }
  }  
}

Thanks in Advance,

skr07
  • 707
  • 1
  • 10
  • 36
  • Can you share the mapping of the `et_tax` field? – Val Jan 25 '16 at 10:48
  • Default operator for query_string is OR. You need to change default operator or add AND between conditions. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html – slawek Jan 25 '16 at 10:53
  • @val `et_tax` is mapped as string – skr07 Jan 25 '16 at 10:56
  • @satheesh Do you want et_tax with value containing both Airports/Ports and Agriculture keywords to be excluded? – Richa Jan 25 '16 at 11:03
  • Yes @Richa I need to exclude the results with above keywords. – skr07 Jan 25 '16 at 11:05
  • I don't understand your problem exactly but if you need to find out how your query_string is understood by ES you can check this question http://stackoverflow.com/q/18400441/808271 . – slawek Jan 25 '16 at 15:41

1 Answers1

2

try out this.

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "* AND NOT (et_tax:\"Agriculture\" OR  et_tax:\"Airports/Ports\")"  
        }
      }
    }
  },
 "aggs": {
    "lawfirmcount": {
      "cardinality": {
        "field": "pd_lawfirmID",
        "precision_threshold": 1250 
      }
    }
  }  
}
  • if i want to add some additional query before that `AND NOT`, can i include this way `(\"BBC OR SKY\") AND et_tx:\"corporate\" AND NOT (et_tax:\"Agriculture\" OR et_tax:\"Airports/Ports\")"` is this correct? – skr07 Jan 25 '16 at 11:20