# Match phrase queries to highlighted values

**URL:** https://discuss.elastic.co/t/match-phrase-queries-to-highlighted-values/17721
**Category:** Elasticsearch
**Created:** [May 26, 2014, 8:35am UTC](https://discuss.elastic.co/t/match-phrase-queries-to-highlighted-values/17721 "2014-05-26T08:35:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Alexandra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexandra/32/1521_2.png) [@Alexandra](https://discuss.elastic.co/u/Alexandra)
#### Post date: [May 26, 2014, 8:35am UTC](https://discuss.elastic.co/t/match-phrase-queries-to-highlighted-values/17721/1 "2014-05-26T08:35:22Z")

</div>

Hi,

My use case is the following : I have a text segment and a list o terms and I want to find only the ones that match exactly the text.  
For e.g. if my segment text is : "This is a simple text." And my terms are : "texts", "this", "text", I will find and highlight only the terms "this" and "text".

I'm building the query with the Java Api like this ( the segment is indexed ):

BoolQueryBuilder query = QueryBuilders.boolQuery();  
for(TermDocument termCandidate : termCandidates) {  
query.should(QueryBuilders.matchPhraseQuery(ElasticsearchDocumentField.TEXT\_CONTENT.getName(), termCandidate.getTermText()).slop(0).queryName(termCandidate.getId()).analyzer(EN\_ANALYZER));  
}

If I also highlight the terms ( because in the end I need the offsets ), the will all be highlighted and I don't know which one is which. (e.g. _This_ is a simple _text_.)

So now, my questions :  
1.Is there a way to highlight the terms from the query separately ? And to associate some id to each of them in order to be able to match them back ?  
2. Is there a way to receive the token numbers for an indexed text without using the analyze api ? (this is unrelated to the first question).

---

<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, 1:26am UTC](https://discuss.elastic.co/t/match-phrase-queries-to-highlighted-values/17721/2 "2017-07-06T01:26:59Z")

</div>


