# Highlight when query contains fields and text - ES 1.6.0

**URL:** https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311
**Category:** Elasticsearch
**Created:** [June 25, 2015, 7:43am UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311 "2015-06-25T07:43:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![effoeffi](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@effoeffi](https://discuss.elastic.co/u/effoeffi)
#### Post date: [June 25, 2015, 7:43am UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/1 "2015-06-25T07:43:30Z")

</div>

Hello,

I'm using elasticsearch 1.6.0.  
Assume having the following document indexed in elasticsearch:

```json
{
    "id" : 2,
    "user" : "jack",
    "message" : "I'm jack",
	"comment" : "my first tweet"
}

```

When query:

```json
{
  "from" : 0,
  "size" : 5,
  "query" : {
    "query_string" : {
      "query" : "user:jack first",
      "use_dis_max" : true
    }
  },
  "highlight" : {
    "pre_tags" : ["<span class=\"mark\">"],
    "post_tags" : ["</span>"],
    "order" : "score",
    "encoder" : "html",
    "require_field_match" : false,
    "fields" : {    
      "*" : {}
    }
  }
}

```

I'm using "query string query".  
As you can see, my query combines field match (user:jack) and text (first).  
As a result, elasticsearch will highlight the word jack in both user field and message.  
But I query for user:jack, mean I want to highlight only user name jack and not jack in other fields.  
I can't set require\_field\_match to true, because than elasticsearch will not highlight 'first'.

What should I do?

Thanks in advance,  
Effi

---

<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: [June 25, 2015, 12:40pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/2 "2015-06-25T12:40:37Z")

</div>

The simplest thing is to set require field match to true and to set the  
default fields parameter on the query\_string to "message, comment".

---

<div class="post-metadata">

### Author: ![effoeffi](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@effoeffi](https://discuss.elastic.co/u/effoeffi)
#### Post date: [June 25, 2015, 1:06pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/3 "2015-06-25T13:06:14Z")

</div>

Unfortunately it wouldn't work ☹  
This will not highlight the word ' **first**' (like I wrote in the question:  
I can't set require\_field\_match to true, because than elasticsearch will not highlight 'first').

---

<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: [June 25, 2015, 4:21pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/4 "2015-06-25T16:21:27Z")

</div>

Sure it will - but only if you explicitly search that field instead of the  
\_all field. Do query\_string: {fields: "comment, message", query: "your  
query"} and it should work. I've totally done it before but I have to admit  
I haven't tested it this morning.

---

<div class="post-metadata">

### Author: ![effoeffi](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@effoeffi](https://discuss.elastic.co/u/effoeffi)
#### Post date: [June 28, 2015, 2:43pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/5 "2015-06-28T14:43:56Z")

</div>

Wooow, it's working. THANKS! 😃

I don't understand why should I determine that I want highlight on text in the search level (and not at highlight section)?

Another question, let's say my document looks like this:

```json
{ 
  "type": "tweet",
  "fields": {
         "user" : "jack",
         "message" : "I'm jack",
         "comment" : "my first tweet"
         }
}

```

Can I search on **_fields.\*_** instead of specific fields, like: _{fields: "comment, message", query: "your query"}_?  
I understand that on \_all it's not working, but I see in [elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) that I can use wildcard, they use the follow example:

```json
{
    "query_string" : {
        "fields" : ["city.*"],
        "query" : "this AND that OR thus",
        "use_dis_max" : true
    }
}

```

For me it failed with SearchPhaseExecutionException 😟  
Any suggestion?

Thanks,  
Effi

---

<div class="post-metadata">

### Author: ![effoeffi](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@effoeffi](https://discuss.elastic.co/u/effoeffi)
#### Post date: [June 30, 2015, 1:51pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/6 "2015-06-30T13:51:05Z")

</div>

Another issue is when adding to query-string fields a field from type that is not string, for example a field name **_times_** from type _long_:

```json
{
  "from" : 0,
  "size" : 5,
  "query" : {
    "query_string" : {
     "fields" : ["user","message","comment","times"],
      "query" : "first times:>1",
      "use_dis_max" : true
    }
  },
  "highlight" : {
    "pre_tags" : ["<span class=\"mark\">"],
    "post_tags" : ["</span>"],
    "order" : "score",
    "encoder" : "html",
    "require_field_match" : false,
    "fields" : {    
      "*" : {}
    }
  }
}

```

On example data, that looks like this:

```json
{ 
  "type": "tweet",
  "fields": {
         "user" : "jack",
         "message" : "I'm jack",
         "comment" : "my first tweet",
         "times" : "7"
         }
}

```

This will raise a NumberFormatException.  
I understand from [elasticsearch doc on "Multi Field"](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_multi_field_2), that the idea of running the query\_string query against multiple fields is to expand each query term to an OR clause like this:  
field1:query\_term OR field2:query\_term | ...

In this example, _times:first_ will failed, because _times_ configured as _long_.  
Is that mean that I can't highlight fields from type _long_ (other than _string_)?

Thanks,  
Effi

---

<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: [June 30, 2015, 4:25pm UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/7 "2015-06-30T16:25:38Z")

</div>

> [@effoeffi](#):
>
> Is that mean that I can't highlight fields from type long (other than string)?

For the most part that is true, yes. Some of the highlighters make an effort to stringify the non-string data first and then do string highlighting against it but that is lame and haphazard. Its not something that's particularly well thought out frankly.

---

<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 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/highlight-when-query-contains-fields-and-text-es-1-6-0/24311/8 "2017-07-06T00:04:30Z")

</div>


