# Highlighting within fragments inconsistent

**URL:** <https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334>\
**Category:** Elasticsearch\
**Created:** [February 14, 2019, 6:02am UTC](https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334 "2019-02-14T06:02:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tree1](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tree1](https://discuss.elastic.co/u/Tree1)\
**Post date:** [February 14, 2019, 6:02am UTC](https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334/1 "2019-02-14T06:02:58Z")

</div>

I'm trying to use ElasticSearch to locate important text which I then run a second process to extract information from the highlighted fragment.

The problem is that in these highlighted fragments, the text I need to extract is often after the word that I'm searching for. Eg. In the document - "Numbers: 1 2 3 Colours: red green blue", I'm searching for "colours". The result I see is "3 _Colours_"

With ElasticSearch it seems like the fragment often finishes after "colours".

I have tried modifying the fragment size and it only increases the text BEFORE my search term and none after it.  
Is there a way to make sure that "colour" would be in the middle of the fragment?

---

<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:** [February 14, 2019, 6:31am UTC](https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334/2 "2019-02-14T06:31:33Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Which version are you using?

---

<div class="post-metadata">

**Author:** ![Tree1](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tree1](https://discuss.elastic.co/u/Tree1)\
**Post date:** [February 15, 2019, 12:07am UTC](https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334/3 "2019-02-15T00:07:52Z")

</div>

We are using Elasticsearch v6.3.2 on python 3

Here is the query I'm running:

```
query = {
    "query": {
	    "bool": {
		    "must": {
			    "query_string" : {"default_field": "content", "query" : "\"paint colours\" OR \"pc\""}
		    },
		    "filter": {
			    "term": {"_id": doc_name}
		     }
        }
    },
    "highlight": {
        "pre_tags" : ["<start>"],
        "post_tags" : ["</end>"],
        "fields": {
            "content": {"number_of_fragments": 1000, "fragment_size" : 500}
        }
    }
}

```

The idea is to locate the relevant data labelled around "Paint Colours" using Elasticsearch and then process the returned fragment to extract data present after the the label.

For example, if there is a document like:

> ...some text...
> 
> Word1  
> Word2  
> Paint Colours: Red, Green
> 
> ...more text...

I would like to extract [Red, Green] out of this document.

The problem I am facing is Elasticsearch returns highlighted fragments which look like this:

> Word2  
> _Paint Colours_

And if I increase the fragment size, it looks something like this:

> Word1  
> Word2  
> _Paint Colours_

But what I'm really looking for is to have the search term somewhere in the middle of the fragment so that there is text AFTER the search term as well. Like this:

> Word2  
> _Paint Colours_: Red, Green

This would help extract required text that is next to the search term.

---

<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:** [March 15, 2019, 12:07am UTC](https://discuss.elastic.co/t/highlighting-within-fragments-inconsistent/168334/4 "2019-03-15T00:07:53Z")

</div>

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