Ganesh
Ganesh

Reputation: 867

how to use not query in index field in elasticsearch

This is the mapping.

curl -XPUT 'localhost:9200/products/' -d '{
    "settings" : {
        "index" : {
            "number_of_shards" : 6, 
            "number_of_replicas" : 1 
        }
    },
    "mappings" : {
        "product" : {
          "_all":{ "enabled": true  },
          "properties":{
            "id" : { "type" : "string", "index" : "not_analyzed", "include_in_all": true },
            "description" : { "type" : "string" },
            "title" : { "type" : "string", "boost" : 2 },
      }
        }
    }
}'

I don't want to get the ads which have no description. but as you can see in mapping "description" have an index. So how do I use not query in description? please help me out.

I seen the doc of elasticsearch and I use this query.

**query => {
    filtered => {
        filter => {
            not => {
                filter => {
                    term  => {description  => ''}
                }
            }
        },
        query => {
            match => { _all => $q }
        }
    }
}**

But it's not working, I think because description have index right?

Upvotes: 0

Views: 33

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

For 2.4 this would be the correct syntax and query approach:

{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ],
      "filter": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "description"
              }
            },
            {
              "wildcard": {
                "description": "*"
              }
            }
          ]
        }
      }
    }
  }
}

Instead of filtered you have a bool with must as query and filter as filter. What's inside ofmustis what you have as query and what's inside offilteris what you have as filter. The approach you used withfiltered` is deprecated in ES 2.x.

Upvotes: 2

Related Questions