# Text Phrase Query

**URL:** <https://discuss.elastic.co/t/text-phrase-query/5552>\
**Category:** Elasticsearch\
**Created:** [October 10, 2011, 2:16pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552 "2011-10-10T14:16:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Nemsadze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_nemsadze/32/3114_2.png) [@Alexander\_Nemsadze](https://discuss.elastic.co/u/Alexander_Nemsadze)\
**Post date:** [October 10, 2011, 2:16pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/1 "2011-10-10T14:16:24Z")

</div>

Hello

We want to get the functionality of text\_phrase\_prefix but with  
highlights. To search exact matching of phrase without ending last  
word.

Suppose we have indexed following text:  
Elasticsearch works perfectly for us.

Scenario 1:  
Search String: ElasticSearch works  
Result: Entry was found

Scenario 2:  
Search String: ElasticSearch perfectly  
Result: No entry was found

Scenario 3:  
Search String: ElasticSearch wor  
Result: Entry was found

Scenario 4:  
Search String: ElasticS  
Result: Entry was found

Scenario 5:  
Search String: ElasticS wor  
Result: No entry was found

Any suggestions implementing this?  
Thanks in advnace

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 10, 2011, 2:25pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/2 "2011-10-10T14:25:10Z")

</div>

On Mon, 2011-10-10 at 07:16 -0700, Alexander Nemsadze wrote:

> Hello
> 
> We want to get the functionality of text\_phrase\_prefix but with  
> highlights. To search exact matching of phrase without ending last  
> word.

> Any suggestions implementing this?

A simple regex on the the highlighted phrase would work, eg, in Perl  
(assuming your highlighting uses _tags):_

 

_s{_([^\<\s]+)}{\1}g

clint

---

<div class="post-metadata">

**Author:** ![Alexander\_Nemsadze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_nemsadze/32/3114_2.png) [@Alexander\_Nemsadze](https://discuss.elastic.co/u/Alexander_Nemsadze)\
**Post date:** [October 10, 2011, 2:35pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/3 "2011-10-10T14:35:25Z")

</div>

Hello, thanks for your response, but in fact we are trying to pass the  
query string to Elasticsearch, not processing local strings.  
Elastic search makes highlight for you adding start and end tags to  
it.

On Oct 10, 6:25 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Mon, 2011-10-10 at 07:16 -0700, Alexander Nemsadze wrote:
> 
> > Hello
> 
> > We want to get the functionality of text\_phrase\_prefix but with  
> > highlights. To search exact matching of phrase without ending last  
> > word.  
> > Any suggestions implementing this?
> 
> A simple regex on the the highlighted phrase would work, eg, in Perl  
> (assuming your highlighting uses _tags):_
> 
>  
> 
> _s{_([^\<\s]+)}{\1}g
> 
> clint

---

<div class="post-metadata">

**Author:** ![Alexander\_Nemsadze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_nemsadze/32/3114_2.png) [@Alexander\_Nemsadze](https://discuss.elastic.co/u/Alexander_Nemsadze)\
**Post date:** [October 10, 2011, 2:41pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/4 "2011-10-10T14:41:38Z")

</div>

We want to get exact functionality of text\_phrase query, except it  
must be allowed not finishing last word.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 10, 2011, 3:20pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/5 "2011-10-10T15:20:40Z")

</div>

On Mon, 2011-10-10 at 07:35 -0700, Alexander Nemsadze wrote:

> Hello, thanks for your response, but in fact we are trying to pass the  
> query string to Elasticsearch, not processing local strings.  
> Elastic search makes highlight for you adding start and end tags to  
> it.

Hi Alexander

My regex was for post-processing results from elasticsearch to achieve  
what you want, ie instead of a search for "lang" returning

"a _lang_uage in common use"

my regex would convert that to

"a _language_ in common use"

which is what you want

clint

---

<div class="post-metadata">

**Author:** ![Alexander\_Nemsadze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_nemsadze/32/3114_2.png) [@Alexander\_Nemsadze](https://discuss.elastic.co/u/Alexander_Nemsadze)\
**Post date:** [October 10, 2011, 3:46pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/6 "2011-10-10T15:46:20Z")

</div>

Thanks for that solution, but my problem is that I have no "a  
_lang_uage in common use" highlighted text returned from  
elasticsearch, as text\_phrase does not search anything when entering  
part of the word.

text\_phrase\_prefix works perfectly for me, except highlight, no  
highlight is returned when I enter one word and half part of the  
second word, for example - Steve Jo - will return entry, but no  
highlight part exists in response JSON. If I enter - Ste - everything  
is OK.

On Oct 10, 7:20 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Mon, 2011-10-10 at 07:35 -0700, Alexander Nemsadze wrote:
> 
> > Hello, thanks for your response, but in fact we are trying to pass the  
> > query string to Elasticsearch, not processing local strings.  
> > Elastic search makes highlight for you adding start and end tags to  
> > it.
> 
> Hi Alexander
> 
> My regex was for post-processing results from elasticsearch to achieve  
> what you want, ie instead of a search for "lang" returning
> 
> "a _lang_uage in common use"
> 
> my regex would convert that to
> 
> "a _language_ in common use"
> 
> which is what you want
> 
> clint

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [October 10, 2011, 4:02pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/7 "2011-10-10T16:02:45Z")

</div>

> text\_phrase\_prefix works perfectly for me, except highlight, no  
> highlight is returned when I enter one word and half part of the  
> second word, for example - Steve Jo - will return entry, but no  
> highlight part exists in response JSON. If I enter - Ste - everything  
> is OK.

Ah right - apologies. I missed the fact that highlighting doesn't work  
at all with text\_phrase\_prefix.

I don't know why this is the case. I suggest opening an issue

clint

---

<div class="post-metadata">

**Author:** ![Alexander\_Nemsadze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexander_nemsadze/32/3114_2.png) [@Alexander\_Nemsadze](https://discuss.elastic.co/u/Alexander_Nemsadze)\
**Post date:** [October 11, 2011, 6:13am UTC](https://discuss.elastic.co/t/text-phrase-query/5552/8 "2011-10-11T06:13:49Z")

</div>

To explain in one more way, we want to get result elasticsearch to  
search for us exactly as database LIKE 'something mor%' query does and  
with highlights

On Oct 10, 8:02 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> > text\_phrase\_prefix works perfectly for me, except highlight, no  
> > highlight is returned when I enter one word and half part of the  
> > second word, for example - Steve Jo - will return entry, but no  
> > highlight part exists in response JSON. If I enter - Ste - everything  
> > is OK.
> 
> Ah right - apologies. I missed the fact that highlighting doesn't work  
> at all with text\_phrase\_prefix.
> 
> I don't know why this is the case. I suggest opening an issue
> 
> clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 12, 2011, 7:17pm UTC](https://discuss.elastic.co/t/text-phrase-query/5552/9 "2011-10-12T19:17:49Z")

</div>

Can you have term vectors stored for the field you highlight on, and see if  
it helps? You will need to reindex the data. Storing term vectors will cause  
a different highlighting implementation to be used (see here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/highlighting.html).

On Tue, Oct 11, 2011 at 8:13 AM, Alexander Nemsadze \<  
[alexander.nemsadze@azry.com](mailto:alexander.nemsadze@azry.com)\> wrote:

> To explain in one more way, we want to get result elasticsearch to  
> search for us exactly as database LIKE 'something mor%' query does and  
> with highlights
> 
> On Oct 10, 8:02 pm, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > > text\_phrase\_prefix works perfectly for me, except highlight, no  
> > > highlight is returned when I enter one word and half part of the  
> > > second word, for example - Steve Jo - will return entry, but no  
> > > highlight part exists in response JSON. If I enter - Ste - everything  
> > > is OK.
> > 
> > Ah right - apologies. I missed the fact that highlighting doesn't work  
> > at all with text\_phrase\_prefix.
> > 
> > I don't know why this is the case. I suggest opening an issue
> > 
> > clint

---

<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:52am UTC](https://discuss.elastic.co/t/text-phrase-query/5552/10 "2017-07-06T03:52:16Z")

</div>


