# Detect exact field that matched the text

**URL:** <https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525>\
**Category:** Elasticsearch\
**Created:** [March 4, 2016, 3:28pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525 "2016-03-04T15:28:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![diego.bernardes](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@diego.bernardes](https://discuss.elastic.co/u/diego.bernardes)\
**Post date:** [March 4, 2016, 3:28pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/1 "2016-03-04T15:28:36Z")

</div>

Given this document:

```json
{  
   "id":123,
   "name":"amazing dress",
   "variations":[  
      {  
         "sku":456,
         "name":"amazing dress red",
         "price":12.42
      },
      {  
         "sku":789,
         "name":"amazing dress blue",
         "price":13
      }
   ]
}

```

How could i detect what SKU the query hit? This is soooo dammm important for me.  
Usually my query is something like this:

```json
{  
   "query":{  
      "multi_match":{  
         "fields":[  
            "name",
            "variations.name"
         ],
         "query":"red dress"
      }
   }
}

```

So i'm gonna hit that document, but, how to discover the SKU that really hit the document?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 4, 2016, 4:04pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/2 "2016-03-04T16:04:01Z")

</div>

Each SKU needs its own document. You can use [nested](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html) object if you want to but it is often simpler to just build documents like

```auto
{  
    "product": 123,
    "name": "amazing dress",
    "sku":789,
    "name":"amazing dress blue",
    "price":13
 }

```

You can think of nested is a fancy way to automatically build all those documents.

---

<div class="post-metadata">

**Author:** ![diego.bernardes](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@diego.bernardes](https://discuss.elastic.co/u/diego.bernardes)\
**Post date:** [March 4, 2016, 4:51pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/3 "2016-03-04T16:51:27Z")

</div>

But this gonna lead to other problems. If a given product have multiple SKU and they are indexed as multiple documents, when a query is executed, the same item could return multiple times, but, in fact, its just a single product that must return, the sku separation is just a abstraction.

What i'm trying to say is, in the application, only the base product should be displayed, if the user click that product, inside the product page it show the skus, like in almost all ecommerces.

This thing i'm trying to do is to solve a problem when users ask for 'black dress' and the image displayed is a 'white dress', but there is a inner sku of that product that is white. With this the app can show the correct sku without the user need to enter the product to see if has the property it's searching.

Another thing that multiple sku documents generate is the incorrectness of count, aggregations, etc...

I'm not familiar with nested objects, but don't seems that if gonna help to identify whatis the sku the query hited, it may help in accuracy.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [March 4, 2016, 5:07pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/4 "2016-03-04T17:07:28Z")

</div>

This sounds more like the traditional use case for parent/child documents actually. Have a look at those as well.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [March 4, 2016, 5:14pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/5 "2016-03-04T17:14:46Z")

</div>

You might be looking for inner hits, which will only return the nested  
objects that are matched by a nested query:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html)

Ivan

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:11pm UTC](https://discuss.elastic.co/t/detect-exact-field-that-matched-the-text/43525/6 "2017-07-05T23:11:09Z")

</div>


