Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch exclude results with value

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

like image 568
kyle k Avatar asked Aug 17 '14 20:08

kyle k


People also ask

How do I get more than 10 results in Elasticsearch?

If a search request results in more than ten hits, ElasticSearch will, by default, only return the first ten hits. To override that default value in order to retrieve more or fewer hits, we can add a size parameter to the search request body.

How do I search for null values in Elasticsearch?

But if you want to search for all documents containing a null value, you can tell Elasticsearch to replace the null value with a default value. And sometimes the login_status is sent as null by default. If the login_status field is null, the login_status field is skipped.

What is _score in Elasticsearch?

The _score in Elasticsearch is a way of determining how relevant a match is to the query. The default scoring function used by Elasticsearch is actually the default built in to Lucene which is what Elasticsearch runs under the hood.


1 Answers

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

like image 181
progrrammer Avatar answered Sep 21 '22 19:09

progrrammer