rdiz
rdiz

Reputation: 6176

ElasticSearch query sub-objects

I wandered through the docs a lot today, but can't find the answer; probably because I'm new to Elastic and don't really know the entire ES-terminology yet.

Say I have a books type containing a bunch of, well - books. Each book has a nested author.

{
  "name": "Me and Jane",
  "rating": "10",
  "author": {
    "name": "John Doe",
    "alias":"Mark Twain"
  }
}

Now, I know we can query the authors fields like this:

"match": {
   "author.name": "Doe"
 }

But what if I want to search across all the author fields? I tried author._all, which doesn't work.

Upvotes: 2

Views: 1733

Answers (2)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Another approach is multi_match with wildcard field names: https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-match-query.html#_using_wildcards_in_field_names

Something like this, I think:

  "query": {
    "nested": {
      "path": "author",
      "query": {
        "multi_match": {
          "query": "doe",
          "fields": [
            "author.*"
          ]
        }
      }
    }
  }

UPDATE: full sample provided

PUT /books
{
  "mappings": {
    "paper": {
      "properties": {
        "author": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "string"
            },
            "alias": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

POST /books/paper/_bulk
{"index":{"_id":1}}
{"author":[{"name":"john doe","alias":"doe"},{"name":"mark twain","alias":"twain"}]}
{"index":{"_id":2}}
{"author":[{"name":"mark doe","alias":"john"}]}
{"index":{"_id":3}}
{"author":[{"name":"whatever","alias":"whatever"}]}

GET /books/paper/_search
{
  "query": {
    "nested": {
      "path": "author",
      "query": {
        "multi_match": {
          "query": "john",
          "fields": [
            "author.*"
          ]
        }
      }
    }
  }
}

Result is:

   "hits": {
      "total": 2,
      "max_score": 0.5906161,
      "hits": [
         {
            "_index": "books",
            "_type": "paper",
            "_id": "2",
            "_score": 0.5906161,
            "_source": {
               "author": [
                  {
                     "name": "mark doe",
                     "alias": "john"
                  }
               ]
            }
         },
         {
            "_index": "books",
            "_type": "paper",
            "_id": "1",
            "_score": 0.5882852,
            "_source": {
               "author": [
                  {
                     "name": "john doe",
                     "alias": "doe"
                  },
                  {
                     "name": "mark twain",
                     "alias": "twain"
                  }
               ]
            }
         }
      ]
   }

Upvotes: 1

chengpohi
chengpohi

Reputation: 14217

You can use Query String Query, The example:

 {
     "query": {
         "query_string": {
             "fields": ["author.*"],
             "query": "doe",
             "use_dis_max": true
         }
     }
 }

Upvotes: 0

Related Questions