Rubén Aguado
Rubén Aguado

Reputation: 366

Search exact words in elastichsearch

Using elasticseach, I want to search all documents with two words.

I use:

{ "query": { "match": { "text": "word1 word2" } } }

but instead of having documents with word1 and word2, i have documents with word1 or word2. ¿how can i search docs with word1 AND word2?

Upvotes: 0

Views: 29

Answers (1)

Musab Dogan
Musab Dogan

Reputation: 3680

You can use operator in the query.

The operator parameter can be set to or or and to control the boolean clauses (defaults to or).

POST test_index/_doc
{"text":"word1"}
POST test_index/_doc
{"text":"word2"}
POST test_index/_doc
{"text":"word1 word2"}


GET test_index/_search
{
  "query": {
    "match": {
      "text": {
        "query": "word1 word2",
        "operator": "and"
      }
    }
  }
}

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-boolean

enter image description here

Upvotes: 1

Related Questions