user1398287
user1398287

Reputation: 5375

exact match query in elasticsearch

I'm trying to run an exact match query in ES

in MYSQL my query would be:

SELECT * WHERE `content_state`='active' AND `author`='bob' AND `title` != 'Beer';

I looked at the ES docs here:

https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html

and came up with this:

{
"from" : '.$offset.', "size" : '.$limit.',
"filter": {
"and": [
  {
    "and": [
      {
        "term": {
          "content_state": "active"
        }
      },
      {
        "term": {
          "author": "bob"
        }
      },
      {
        "not": {
          "filter": {
            "term": {
              "title": "Beer"
            }
          }
        }
      }
    ]
  }
    ]
  }
}

but my results are still coming back with the title = Beer, it doesn't seem to be excluding the titles that = Beer.

did I do something wrong?

I'm pretty new to ES

Upvotes: 0

Views: 162

Answers (3)

Avinash Kumar Pandey
Avinash Kumar Pandey

Reputation: 752

Filters are supposed to work on exact values, if you had defined your mapping in a manner where title was a non-analyzed field, your previous attempt ( with filters) would have worked as well.

{
    "mappings": {
        "test": {
            "_all": {
                "enabled": false
            },
            "properties": {
                "content_state": {
                    "type": "string"
                },
                "author": {
                    "type": "string"
                },
                "title": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

Upvotes: 0

Pandiyan Cool
Pandiyan Cool

Reputation: 6565

Query String Query is a pretty good concept to handle various relationship between search criteria. Have a quick look into Query string query syntax to understand in detail about this concept

{
  "query": {
    "query_string": {
      "query": "(content_state:active AND author:bob) AND NOT (title:Beer)"
    }
  }
}

Upvotes: 0

user1398287
user1398287

Reputation: 5375

I figured it out, I used this instead...

{
 "from" : '.$offset.', "size" : '.$limit.',
  "query": {
   "bool": {
   "must": [
    {
      "query_string": {
        "default_field": "content_state",
        "query": "active"
      }
    },
    {
      "query_string": {
        "default_field": "author",
        "query": "bob"
      }
    }
  ],

  "must_not": [
    {
      "query_string": {
        "default_field": "title",
        "query": "Beer"
      }
    }
  ]




}
}
}

Upvotes: 1

Related Questions