# Should highlighting work with all query types?

**URL:** <https://discuss.elastic.co/t/should-highlighting-work-with-all-query-types/5331>\
**Category:** Elasticsearch\
**Created:** [September 8, 2011, 9:33am UTC](https://discuss.elastic.co/t/should-highlighting-work-with-all-query-types/5331 "2011-09-08T09:33:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Njal\_Karevoll](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@Njal\_Karevoll](https://discuss.elastic.co/u/Njal_Karevoll)\
**Post date:** [September 8, 2011, 9:33am UTC](https://discuss.elastic.co/t/should-highlighting-work-with-all-query-types/5331/1 "2011-09-08T09:33:59Z")

</div>

Highlighting does not seem to work with custom\_score or boosting (and maybe  
others, but these were the one I just tested).

Example (from [https://gist.github.com/9f003179c02ebe9b69ae](https://gist.github.com/9f003179c02ebe9b69ae)):

curl -XDELETE [http://localhost:9200/test?pretty](http://localhost:9200/test?pretty)  
curl -XPOST [http://localhost:9200/test/my\_type/1?pretty](http://localhost:9200/test/my_type/1?pretty) -d '  
{  
"foo": {  
"bar": "this is bar"  
}  
}'

# give es some time:

sleep 1

# works as expected:

curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{  
"query": {"query\_string": {"query":"bar"}},  
"highlight": {"fields": {"foo.bar":{}}}  
}'

# does not highlight:

curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{  
"query": {  
"boosting": {  
"positive": {  
"query\_string": {  
"query":"bar"  
}  
},  
"negative": {"term": {"foo.bar": "baz"}},  
"negative\_boost": 0.9  
}  
},  
"highlight": {"fields": {"foo.bar":{}}}  
}'

# does not highlight either:

curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{  
"query": {  
"custom\_score": {  
"query": {  
"query\_string": {  
"query":"bar"  
}  
},  
"script": "\_score"  
}  
},  
"highlight": {"fields": {"foo.bar":{}}}  
}'

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [September 8, 2011, 9:41pm UTC](https://discuss.elastic.co/t/should-highlighting-work-with-all-query-types/5331/2 "2011-09-08T21:41:40Z")

</div>

Hi,

I created ticked for this

> <https://github.com/elastic/elasticsearch/issues/1314>
>
> reported by: https://github.com/nkvoll
> 
> Highlighting does not seem to work with …custom\_score or boosting (and maybe others, but these were the one I just tested).
> 
> Example (from https://gist.github.com/9f003179c02ebe9b69ae ):
> 
> \`\`\`
> curl -XDELETE http://localhost:9200/test?pretty
> curl -XPOST 'http://localhost:9200/test/my\_type/1?pretty=1&refresh=true' -d '
> {
> "foo": {
> "bar": "this is bar"
> }
> }'
> 
> \# works as expected:
> curl http://localhost:9200/test/\_search?pretty -XPOST -d '{
> "query": {"query\_string": {"query":"bar"}},
> "highlight": {"fields": {"foo.bar":{}}}
> }'
> 
> 
> \# does not highlight:
> curl http://localhost:9200/test/\_search?pretty -XPOST -d '{
> "query": {
> "boosting": {
> "positive": {
> "query\_string": {
> "query":"bar"
> }
> },
> "negative": {"term": {"foo.bar": "baz"}},
> "negative\_boost": 0.9
> }
> },
> "highlight": {"fields": {"foo.bar":{}}}
> }'
> 
> 
> \# does not highlight either:
> curl http://localhost:9200/test/\_search?pretty -XPOST -d '{
> "query": {
> "custom\_score": {
> "query": {
> "query\_string": {
> "query":"bar"
> }
> },
> "script": "\_score"
> }
> },    
> "highlight": {"fields": {"foo.bar":{}}}
> }'    
> \`\`\`

There is already a quick pull request, you can give it a try

> <https://github.com/elastic/elasticsearch/pull/1315>
>
> A bit messy but should be working fine. Needs to be reviewed.
> 
> Idea: Going forwa…rd it might be useful to add some interface that would be used by highlighting code. The interface would force ES specific Query implementations to return Query that can be understood by highlighting framework. For example it would be subQuery for some queries or the query that is returned by rewrite method. Then the messy logic based on \`instanceof\` could have been reduced.

Thanks,  
Lukas

On Thu, Sep 8, 2011 at 11:33 AM, Njal Karevoll [nkvoll@gmail.com](mailto:nkvoll@gmail.com) wrote:

> Highlighting does not seem to work with custom\_score or boosting (and maybe  
> others, but these were the one I just tested).
> 
> Example (from [gist:9f003179c02ebe9b69ae · GitHub](https://gist.github.com/9f003179c02ebe9b69ae)):
> 
> curl -XDELETE [http://localhost:9200/test?pretty](http://localhost:9200/test?pretty)
> 
> curl -XPOST [http://localhost:9200/test/my\_type/1?pretty](http://localhost:9200/test/my_type/1?pretty) -d '
> 
> {  
> "foo": {
> 
> ```
> "bar": "this is bar"
> 
> }
> 
> ```
> 
> }'
> 
> # give es some time:
> 
> sleep 1
> 
> # works as expected:
> 
> curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{  
> "query": {"query\_string": {"query":"bar"}},  
> "highlight": {"fields": {"foo.bar":{}}}  
> }'
> 
> # does not highlight:
> 
> curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{  
> "query": {
> 
> ```
> "boosting": {
> "positive": {
> "query_string": {
> 
> "query":"bar"
> 
> }
> },
> 
> "negative": {"term": {"foo.bar": "baz"}},
> 
> "negative_boost": 0.9
> }
> },
> 
> "highlight": {"fields": {"foo.bar":{}}}
> 
> ```
> 
> }'
> 
> # does not highlight either:
> 
> curl [http://localhost:9200/test/\_search?pretty](http://localhost:9200/test/_search?pretty) -XPOST -d '{
> 
> ```
> "query": {
> "custom_score": {
> "query": {
> 
> "query_string": {
> "query":"bar"
> }
> 
> },
> "script": "_score"
> }
> 
> },
> "highlight": {"fields": {"foo.bar":{}}}
> 
> ```
> 
> }'

---

<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, 3:55am UTC](https://discuss.elastic.co/t/should-highlighting-work-with-all-query-types/5331/3 "2017-07-06T03:55:00Z")

</div>


