0
votes

Here is the structure of my index. An Event has many nested objects : documents, sites, and persons.

Some fields on some nested objects are copy_to to the root.

Events
|--- persons—names
|--- other fields
|
|———— Documents
          --------- other fields
|
|———— Sites
          ---------- other fields
|
|———— Persons
           -----------name (copied to the root)
           ------------- id

Now let's say I am doing a query string search.

If a document is returned BECAUSE it matched with persons_names I want to return only the inner_hit of the associated object, which is "persons" in that case. Note that there may be multiple persons in the document and I need to return exactly the one that matched, so this might be an issue because copy_to is an array that doesn't point to its associated nested object.

Of course I want the same thing to happen if the result matched because of a field copied from "sites" or "documents".

This way, I will be able to create a multi object search --> What I mean by that is that I will return either an event, document, site or person, depending what matched with the query.

How can I do that ? Feel free to redirect me to another approach to solve my multi-object search problem.

1

1 Answers

0
votes

Using the Explain API could be a good approach, as discussed here : https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/3

So the solution would be to :

  • Make the query_string query
  • for each results :
    • Use the explain API on them (using their ids)
    • Analyze the explain API's results and tries to find if the result matched because of a person, site or document field.
    • return only the corresponding nested object.

This seems really complex though.

An other option would be to add a new document in my index for each nested object. This would look like this :

Events
|
|--- other fields
|
|———— Documents
|         --------- other fields
|
|———— Sites
|          ---------- other fields
|
|———— Persons
           -----------name (NOT copied to the root)
           ------------- id

### And on the same index, you have other documents for each nested objects

Persons
-----------name (NOT copied to the root)
------------- id
Sites
---------- other fields

Documents
--------- other fields

Then the query_string is straight forward.

Last alternative is to use an index for each type of those objects and query all those indices at the same time.