18
votes

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

1
Whats wrong with the query? what you get? - progrrammer
@progrrammer I added the error. - kyle k
It is not complete, copy all the error. - progrrammer
@progrrammer The error was to long, here is the gist gist.github.com/kylelk/feca416156712eebad3e Thank You - kyle k
He probably got a 400 Bad request - Christophe Roussy

1 Answers

26
votes

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!!