6

I have a few ElasticSearch documents, which contain an Array field.

I want to be able to search the whole index
To find the documents which firstly contains the array field,
and if they do, find those documents which contain the value to be searched.

Following is my data -

{
  "took" : 633,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon",
          "nick_name" : "an",
          "hobbies" : [
            "reading",
            "writing"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon2",
          "nick_name" : "an2",
          "hobbies" : [
            "playing",
            "studying"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon3",
          "nick_name" : "an3",
          "hobbies" : [
            "playing",
            "writing"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon4",
          "nick_name" : "an4"
        }
      }
    ]
  }
}

I want to get those documents whose hobbies array contains "playing" or "studying" value
ie. documents 2 and 3

Following are my data creation queries -

POST /demo/_doc/1
{
  "name":"anon",
  "nick_name":"an",
  "hobbies":["reading","writing"]
}

POST /demo/_doc/2
{
  "name":"anon2",
  "nick_name":"an2",
  "hobbies":["playing","studying"]
}

POST /demo/_doc/3
{
  "name":"anon3",
  "nick_name":"an3",
  "hobbies":["playing","writing"]
}

POST /demo/_doc/4
{
  "name":"anon4",
  "nick_name":"an4"
}

GET demo/_search
{
  "query": {
    "match_all": {}
  }
}

The following query is able to get me single search, but how can I achieve Or condition?

GET demo/_search
{
  "query": {
    "match": {
      "hobbies": "playing"
    }
  }
}

3 Answers 3

9

I cannot test it right now, but it could be something like this:

GET demo/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "hobbies": [
              "playing",
              "studying"
            ]
          }
        }
      ]
    }
  }
}

If your hobbies field is of type keyword.

Edit: Following the example in the documentation, it could be even easier:

GET demo/_search
{
    "query" : {
        "terms" : {
            "hobbies" : ["playing", "studying"]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I was able to resolve this using the following es query -

GET demo/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "hobbies": "playing"
          }
        },
        {
          "match": {
            "hobbies": "studying"
          }
        }
      ]
    }
  }
}

8 Comments

I wonder if a document with hobby "play" would be matched by this query. If I remember correctly, match queries are analyzed, stemmed etc and not meant to be used for exact keyword matches?
Yep able to retrieve the correct resultsets of studying and playing hobby types
But would a document with "hobbies": ["play"] be matched by your query? Because I think the input to the match query is analyzed and stemmed. After stemming your query should effectively look for "play" or "study" and thus both documents would be returned.
Oh yea doesn’t work for “play” case, works for exact text match only I guess?
Just to clarify - do you want documents with "play" to be matched by your query when looking for "playing" - or not?
|
0

Here is another method with operator.

GET demo/_search
{
  "query": {
    "match": {
      "hobbies": {
        "query": "studying, playing",
        "operator": "OR"
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.