18

How can i perform a search excluding results where a field has a specific value?

I have a database of Reddit comments and i want to find Bitcoin mentions, but exclude the bitcoin subreddit.

curl -s -XGET 'http://localhost:9200/_search?pretty=true&size=100' -d '
{
    "filtered": {
        "query" : {
            "match": {
                "body": "bitcoin"
            }
        },
        "filter": {
            "not": {
                "term": {
                    "subreddit": "bitcoin"
                }
            }
        }

    }
}'

The error is to long to post here. https://gist.github.com/kylelk/feca416156712eebad3e

kyle k
  • 5,134
  • 10
  • 31
  • 45

1 Answers1

26

It is silly error,

You have to include filtered query inside query . Here is modification

POST _search
{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "body": "bitcoin"
            }
         },
         "filter": {
            "not": {
               "term": {
                  "subreddit": "bitcoin"
               }
            }
         }
      }
   }
}

Hope this helps!!

progrrammer
  • 4,475
  • 2
  • 30
  • 38
  • 6
    Not valid for Elasticsearch 5.x - https://stackoverflow.com/questions/40519806/no-query-registered-for-filtered – hipokito May 30 '17 at 13:33