# Returning field names in highlight

**URL:** https://discuss.elastic.co/t/returning-field-names-in-highlight/173341
**Category:** Elasticsearch
**Created:** [March 21, 2019, 3:55pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341 "2019-03-21T15:55:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![neesha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neesha/32/50845_2.png) [@neesha](https://discuss.elastic.co/u/neesha)
#### Post date: [March 21, 2019, 3:55pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/1 "2019-03-21T15:55:13Z")

</div>

I have a query that runs against a field called `content`. Content contains many subfields, for example `message`, `name`, `description`, `story`, `caption`. When it ES finds matches, the highlight result looks like this:

`highlight: {content: ["[bold]Test[/bold] [bold]Test[/bold]"]}`

That result found two matches, one in `message` and one in `name`. However, it's difficult to connect that to the result returned.

Is there a way to get the highlighter to return a result that ideally would return the result with the field names, like:  
`highlight: {message: ["[bold]Test[/bold]"], name:["[bold]Test[/bold]"]}`

And if not that, at least split each match and return a list, like below?  
`content: ["[bold]Test[/bold]", "[bold]Test[/bold]"]`

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 21, 2019, 4:29pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/2 "2019-03-21T16:29:20Z")

</div>

I don't understand.

For example:

```auto
DELETE test 
PUT test/_doc/1
{
  "content": {
    "foo": "bar",
    "message": "bar baz"
  }
}
GET test/_search
{
  "query": {
    "multi_match": {
      "query": "bar",
      "fields": ["content.foo","content.message"]
    }
  },
  "highlight": {
    "fields": {
      "content.foo": {},
      "content.message": {}
    }
  }
}

```

is giving:

```auto
{
  "took" : 123,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "content" : {
            "foo" : "bar",
            "message" : "bar baz"
          }
        },
        "highlight" : {
          "content.foo" : [
            "<em>bar</em>"
          ],
          "content.message" : [
            "<em>bar</em> baz"
          ]
        }
      }
    ]
  }
}

```

So you know exactly from which field you are getting the response, no?

---

<div class="post-metadata">

### Author: ![neesha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neesha/32/50845_2.png) [@neesha](https://discuss.elastic.co/u/neesha)
#### Post date: [March 21, 2019, 5:27pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/3 "2019-03-21T17:27:58Z")

</div>

What I'm hoping is to not have to list all the possibilities (`content.foo`, `content.message`, etc).

What I'd like to be able to do is run it as follows:

```auto
{
  "query": {
    "multi_match": {
      "query": "bar",
      "fields": ["content"]
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}

```

However, I'm trying to do so, and actually finding the above doesn't return a hit. So, now I'm wondering about how the data I'm working with is set up (I wasn't involved in the original setup that I'm working with), such that we're able to get hits across all the fields.

The results look like separate fields, but that's being pulled from `_source`. I think maybe it's a case of it all being grouped into a single textfield for indexing/search, which would prevent the return that I want.

Going to dig deeper.

Thanks. This was helpful. I'm still very new to ES and trying to wrap my brain around how it works and how it's used at this company.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 25, 2019, 2:21pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/4 "2019-03-25T14:21:40Z")

</div>

But do we agree that your query does not work?

```auto
{
  "query": {
    "multi_match": {
      "query": "bar",
      "fields": ["content"]
    }
  }
}

```

Because `content` is not a field.  
You can use `copy_to` feature if you wish to flatten all the content you want within one single field at index time.

---

<div class="post-metadata">

### Author: ![neesha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neesha/32/50845_2.png) [@neesha](https://discuss.elastic.co/u/neesha)
#### Post date: [March 25, 2019, 3:48pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/5 "2019-03-25T15:48:26Z")

</div>

Yes, agreed it doesn't work as written. What I need to do is add the `.*`. The following works almost as I want it too.

```auto
curl -X GET "localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "multi_match": {
      "query": "bar",
      "fields": ["content.*"]
    }
  },
  "highlight": {
    "fields": {
      "content.*": {}
    }
  }
}'

```

Except, of course, that it includes all subfields. So would need to limit it.

But I think with a few other things we've encountered in our indexing setup, there's some bigger changes we need to make, and all of this is just helping to support the urgency of the changes.

I looked up `copy_to` and have a question. Let's say in the first example way above, you use `copy_to` to combine `message` and `foo` into `m_foo`. If you try to do highlighting, can you specify you want it on `message` and `foo`? Or do you have to highlight on `m_foo` and end up back with the same problem I started with, where you don't know the specific fields the highlight matched on?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [March 25, 2019, 4:24pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/6 "2019-03-25T16:24:55Z")

</div>

> [@neesha](#):
>
> Or do you have to highlight on `m_foo` and end up back with the same problem I started with, where you don't know the specific fields the highlight matched on?

Yes. This is correct.

> Except, of course, that it includes all subfields. So would need to limit it.

Yeah. Then you would need to be explicit for each single field I think.

---

<div class="post-metadata">

### Author: ![neesha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neesha/32/50845_2.png) [@neesha](https://discuss.elastic.co/u/neesha)
#### Post date: [March 25, 2019, 7:27pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/7 "2019-03-25T19:27:40Z")

</div>

> [@dadoonet](#):
>
> > Except, of course, that it includes all subfields. So would need to limit it.
> 
> Yeah. Then you would need to be explicit for each single field I think.

Okay. That's what I thought. Hmm... Going to have to think through this system carefully.

It feels like the fastest use of ES is when everything can be simply split into tokens and then only direct matches are searched for and only in one field at a time. Each addition (another field, partial match, AND's or OR's) just increases complexity and slows it down. Need to figure out how we can do the least of the additions, but still get what we want out of it. It's a fine balance. 🙂

---

<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: [April 22, 2019, 7:27pm UTC](https://discuss.elastic.co/t/returning-field-names-in-highlight/173341/8 "2019-04-22T19:27:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
