2

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.

2 Answers 2

1

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"
                  }
               ]
            }
         }
      ]
   }
Sign up to request clarification or add additional context in comments.

6 Comments

I actually had a mistake in my solution. I updated it with the correct query.
Hm, it shouldn't. I tested it with a nested mapping just one minute ago.
{ "index" : "test", "type" : "books", "body" : { "query": { "multi_match": { "query" : "Streich", "fields": ["author.*"] } } } } works for me.. Weird
And are you sure author is nested? The best way to check this is GET /test/books/_mapping. If it is nested and you have include_in_parent: true it might work like that (without nested query).
That's what I meant when I said that I'm not really into the understanding of the terminology yet. I'm just touching the surface of ES, I guess. To my understanding, a nested object (document?) is merely a JSON-object inside another object. Is that what is meant by "nested" in ES as well? I don't see either nested nor include_in_parent when curl'ing that.
|
0

You can use Query String Query, The example:

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

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.