25

So my problem is basically the same as described here, however it still remains unanswered on the group.

My mapping:

{
    "abstract": {
        "properties": {
            "summary": {
                "type": "string"
            }
        }
    },
    "authors": {
        "type": "nested",
        "properties": {
            "first_name": {
                "type": "string"
            },
            "last_name": {
                 "type": "string"
            }
        }
    }
}

And I would like to perform a full-text search on both of these fields, probably unequally weighted. The query that comes to my mind, but unfortunately doesn't work, would be this:

{
    "query": {
        "bool": {
            "should": [{
                "multi_match": {
                    "query": "higgs boson",
                    "fields": ["abstract.summary^5", "author.last_name^2"]
                }
            }]
        }
    }
}

I don't get any results from the authors field, because of its nested mapping. I also can't get rid of the nested property - I use it for aggregations. Any elegant idea how to solve it?

stpk
  • 2,015
  • 1
  • 16
  • 23
  • In the document mapping, I don't see the links between the 2 objects. I believe if you are using `nested` object, you need to map it as a child, inside of `properties` and also specify a `nested query`. https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html – Jaider Sep 26 '16 at 19:11

2 Answers2

14

The only solution that I managed to work out, which is not handy nor elegant but somehow works is such query:

"query": {
    "bool": {
        "should": [
            {
                "nested": {
                    "path": "authors",
                    "query": {
                        "multi_match": {
                            "query": "higgs",
                            "fields": ["last_name^2"]
                        }
                    }
                } 
            },
            {
                "multi_match": {
                    "query": "higgs",
                    "fields": ["abstract.summary^5"]
                }
            }
        ]
    }
}

I'm also not sure if the boosting will work as expected, providing it's set in different queries. Any suggestions appreciated.

stpk
  • 2,015
  • 1
  • 16
  • 23
  • according to https://www.elastic.co/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html you will have to rewrite the nested multimatch query into several simple nested queries, and add the `boost` parameter. so in your simple case with just one field per nested multimatch, simply add the `boost` param at the same level with the `query` as folows: – ulkas Jan 21 '16 at 08:26
  • `"nested": { "path": "authors", "query": { "multi_match": { "query": "higgs", "fields": ["last_name"] } }, "boost": 2}` – ulkas Jan 21 '16 at 08:26
14

Changing your mapping to the following one which uses include_in_root: true will allow you to use the query you original wrote:

{
    "abstract": {
        "properties": {
            "summary": {
                "type": "string"
            }
        }
    },
    "authors": {
        "type": "nested",
        "include_in_root": true,
        "properties": {
            "first_name": {
                "type": "string"
            },
            "last_name": {
                 "type": "string"
            }
        }
    }
}

You may want to index inner objects both as nested fields and as flattened object fields. This can be achieved by setting include_in_parent to true. - Link

Note: include_in_root may be deprecated in future versions of elasticsearch in favor of copy_to.

Aymeric
  • 906
  • 9
  • 20