# Text\_phrase\_prefix scoring and closest match

**URL:** <https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172>\
**Category:** Elasticsearch\
**Created:** [March 29, 2012, 3:07pm UTC](https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172 "2012-03-29T15:07:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamie\_Brough](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jamie\_Brough](https://discuss.elastic.co/u/Jamie_Brough)\
**Post date:** [March 29, 2012, 3:07pm UTC](https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172/1 "2012-03-29T15:07:49Z")

</div>

hi,

I'm implementing an autocomplete using text\_phrase\_prefix, but 'exact'  
matches score lower than substring matches, so the most relevant results  
don't appear first and in some cases are not in the first page fo results  
(I'd like to avoid returning too many results and sorting at the client).  
For example:

index:

{  
"settings": {  
"number\_of\_shards": 1,  
"number\_of\_replicas": 0  
},  
"mappings": {  
"place": {  
"dynamic": false,  
"type": "object",  
"properties": {  
...snip...  
"name": {  
"type": "string",  
"analyzer": "standard",  
"store": "yes",  
"term\_vector": "with\_positions\_offsets"  
}  
...snip...  
}

query:

{  
"query": {  
"text\_phrase\_prefix": {  
"name": "London"  
}  
}  
}

As you can see below, "Londonthorpe" has a score of 3.9, whereas "London"  
is 1.05 (I'm wondering why the score is 1.05 and not 1, since it is a  
perfect match?).

Is there a way to order results by closest match, so that the shortest  
complete match is returned first - if not with text\_phrase\_prefix, then  
perhaps a custom forward edgengram filter?

thanks for any pointers,

here are the results of the above query:

{  
"took": 0,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 34,  
"max\_score": 3.9374971,  
"hits": [  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "82Cxx5olSR29vIa0tnQB6w",  
"\_score": 3.9374971,  
"\_source": {  
"name": "Londonthorpe",  
"admin2": "Lincolnshire",  
"country": "GBR",  
"location": "v1mth1htztws",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "0lY8WlnxT1yvCysgCHL0NA",  
"\_score": 3.4017,  
"\_source": {  
"name": "Londonderry",  
"admin2": "North Yorkshire",  
"country": "GBR",  
"location": "v1wsdygy0zz7",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "JS2laACxQimS7A740qNMcA",  
"\_score": 3.4017,  
"\_source": {  
"name": "Londonderry",  
"admin2": "Sandwell",  
"country": "GBR",  
"location": "v1m6d631w63y",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "20IzrjRWSYSYaHMD24TxWg",  
"\_score": 1.0555032,  
"\_source": {  
"name": "London",  
"admin2": "City of London",  
"country": "GBR",  
"location": "v1hth63636dy",  
"rank": 1  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "WGd0GQSGS\_e6Y14QoU0zCw",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Bradford",  
"country": "GBR",  
"location": "v1w637h6gzw6",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "JVEMN2DdRZqHEVCyJynoWg",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Powys",  
"country": "GBR",  
"location": "v1m6d631w63y",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "j5uNub82RRqqr03EefpmtA",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Shropshire",  
"country": "GBR",  
"location": "v1m6d631w63y",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "6oXW6HiyQjakxShTcKhpWg",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Worcestershire",  
"country": "GBR",  
"location": "v1m1wygzz03t",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "7UOGKwu5RXGyGaLcVkAIAQ",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Gloucestershire",  
"country": "GBR",  
"location": "v1hygzh00sd1",  
"rank": 3  
},  
"\_grouped": false  
},  
{  
"\_index": "places",  
"\_type": "place",  
"\_id": "tnVYn2j2Q-6B6LBWi57g0w",  
"\_score": 0.65968955,  
"\_source": {  
"name": "Little London",  
"admin2": "Oxfordshire",  
"country": "GBR",  
"location": "v1hy07wy36d0",  
"rank": 3  
},  
"\_grouped": false  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Jamie\_Brough](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jamie\_Brough](https://discuss.elastic.co/u/Jamie_Brough)\
**Post date:** [March 29, 2012, 3:35pm UTC](https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172/2 "2012-03-29T15:35:58Z")

</div>

wow, having read the "Search and Ngram tokenizer[https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xK7UhGVF0E8](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xK7UhGVF0E8)"  
thread, this mapping and query was what I was looking for:

[http://elasticsearch-users.115913.n3.nabble.com/Question-about-multi-field-and-edge-ngram-td3800000.html](http://elasticsearch-users.115913.n3.nabble.com/Question-about-multi-field-and-edge-ngram-td3800000.html)

works perfectly.

On Thursday, 29 March 2012 16:07:49 UTC+1, Jamie Brough wrote:

> hi,
> 
> I'm implementing an autocomplete using text\_phrase\_prefix, but 'exact'  
> matches score lower than substring matches, so the most relevant results  
> don't appear first and in some cases are not in the first page fo results  
> (I'd like to avoid returning too many results and sorting at the client).  
> For example:
> 
> index:
> 
> {  
> "settings": {  
> "number\_of\_shards": 1,  
> "number\_of\_replicas": 0  
> },  
> "mappings": {  
> "place": {  
> "dynamic": false,  
> "type": "object",  
> "properties": {  
> ...snip...  
> "name": {  
> "type": "string",  
> "analyzer": "standard",  
> "store": "yes",  
> "term\_vector": "with\_positions\_offsets"  
> }  
> ...snip...  
> }
> 
> query:
> 
> {  
> "query": {  
> "text\_phrase\_prefix": {  
> "name": "London"  
> }  
> }  
> }
> 
> As you can see below, "Londonthorpe" has a score of 3.9, whereas "London"  
> is 1.05 (I'm wondering why the score is 1.05 and not 1, since it is a  
> perfect match?).
> 
> Is there a way to order results by closest match, so that the shortest  
> complete match is returned first - if not with text\_phrase\_prefix, then  
> perhaps a custom forward edgengram filter?
> 
> thanks for any pointers,
> 
> here are the results of the above query:
> 
> {  
> "took": 0,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 34,  
> "max\_score": 3.9374971,  
> "hits": [  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "82Cxx5olSR29vIa0tnQB6w",  
> "\_score": 3.9374971,  
> "\_source": {  
> "name": "Londonthorpe",  
> "admin2": "Lincolnshire",  
> "country": "GBR",  
> "location": "v1mth1htztws",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "0lY8WlnxT1yvCysgCHL0NA",  
> "\_score": 3.4017,  
> "\_source": {  
> "name": "Londonderry",  
> "admin2": "North Yorkshire",  
> "country": "GBR",  
> "location": "v1wsdygy0zz7",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "JS2laACxQimS7A740qNMcA",  
> "\_score": 3.4017,  
> "\_source": {  
> "name": "Londonderry",  
> "admin2": "Sandwell",  
> "country": "GBR",  
> "location": "v1m6d631w63y",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "20IzrjRWSYSYaHMD24TxWg",  
> "\_score": 1.0555032,  
> "\_source": {  
> "name": "London",  
> "admin2": "City of London",  
> "country": "GBR",  
> "location": "v1hth63636dy",  
> "rank": 1  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "WGd0GQSGS\_e6Y14QoU0zCw",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Bradford",  
> "country": "GBR",  
> "location": "v1w637h6gzw6",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "JVEMN2DdRZqHEVCyJynoWg",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Powys",  
> "country": "GBR",  
> "location": "v1m6d631w63y",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "j5uNub82RRqqr03EefpmtA",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Shropshire",  
> "country": "GBR",  
> "location": "v1m6d631w63y",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "6oXW6HiyQjakxShTcKhpWg",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Worcestershire",  
> "country": "GBR",  
> "location": "v1m1wygzz03t",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "7UOGKwu5RXGyGaLcVkAIAQ",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Gloucestershire",  
> "country": "GBR",  
> "location": "v1hygzh00sd1",  
> "rank": 3  
> },  
> "\_grouped": false  
> },  
> {  
> "\_index": "places",  
> "\_type": "place",  
> "\_id": "tnVYn2j2Q-6B6LBWi57g0w",  
> "\_score": 0.65968955,  
> "\_source": {  
> "name": "Little London",  
> "admin2": "Oxfordshire",  
> "country": "GBR",  
> "location": "v1hy07wy36d0",  
> "rank": 3  
> },  
> "\_grouped": false  
> }  
> ]  
> }  
> }

---

<div class="post-metadata">

**Author:** ![alheim](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alheim](https://discuss.elastic.co/u/alheim)\
**Post date:** [March 29, 2012, 4:11pm UTC](https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172/3 "2012-03-29T16:11:21Z")

</div>

🙂 Seems we are all facing the same issues.

Maybe a tutorial could be posted on the website ? I can do it.

Suggestion is key for real time web application.

On Thu, Mar 29, 2012 at 5:35 PM, Jamie Brough [jamieb@yourgolftravel.com](mailto:jamieb@yourgolftravel.com)wrote:

> wow, having read the "Search and Ngram tokenizer[https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xK7UhGVF0E8](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/xK7UhGVF0E8)"  
> thread, this mapping and query was what I was looking for:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Question-about-multi-field-and-edge-ngram-td3800000.html](http://elasticsearch-users.115913.n3.nabble.com/Question-about-multi-field-and-edge-ngram-td3800000.html)
> 
> works perfectly.
> 
> On Thursday, 29 March 2012 16:07:49 UTC+1, Jamie Brough wrote:
> 
> > hi,
> > 
> > I'm implementing an autocomplete using text\_phrase\_prefix, but 'exact'  
> > matches score lower than substring matches, so the most relevant results  
> > don't appear first and in some cases are not in the first page fo results  
> > (I'd like to avoid returning too many results and sorting at the client).  
> > For example:
> > 
> > index:
> > 
> > {  
> > "settings": {  
> > "number\_of\_shards": 1,  
> > "number\_of\_replicas": 0  
> > },  
> > "mappings": {  
> > "place": {  
> > "dynamic": false,  
> > "type": "object",  
> > "properties": {  
> > ...snip...  
> > "name": {  
> > "type": "string",  
> > "analyzer": "standard",  
> > "store": "yes",  
> > "term\_vector": "with\_positions\_offsets"  
> > }  
> > ...snip...  
> > }
> > 
> > query:
> > 
> > {  
> > "query": {  
> > "text\_phrase\_prefix": {  
> > "name": "London"  
> > }  
> > }  
> > }
> > 
> > As you can see below, "Londonthorpe" has a score of 3.9, whereas "London"  
> > is 1.05 (I'm wondering why the score is 1.05 and not 1, since it is a  
> > perfect match?).
> > 
> > Is there a way to order results by closest match, so that the shortest  
> > complete match is returned first - if not with text\_phrase\_prefix, then  
> > perhaps a custom forward edgengram filter?
> > 
> > thanks for any pointers,
> > 
> > here are the results of the above query:
> > 
> > {  
> > "took": 0,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 1,  
> > "successful": 1,  
> > "failed": 0  
> > },  
> > "hits": {  
> > "total": 34,  
> > "max\_score": 3.9374971,  
> > "hits": [  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "82Cxx5olSR29vIa0tnQB6w",  
> > "\_score": 3.9374971,  
> > "\_source": {  
> > "name": "Londonthorpe",  
> > "admin2": "Lincolnshire",  
> > "country": "GBR",  
> > "location": "v1mth1htztws",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "0lY8WlnxT1yvCysgCHL0NA",  
> > "\_score": 3.4017,  
> > "\_source": {  
> > "name": "Londonderry",  
> > "admin2": "North Yorkshire",  
> > "country": "GBR",  
> > "location": "v1wsdygy0zz7",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "JS2laACxQimS7A740qNMcA",  
> > "\_score": 3.4017,  
> > "\_source": {  
> > "name": "Londonderry",  
> > "admin2": "Sandwell",  
> > "country": "GBR",  
> > "location": "v1m6d631w63y",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "20IzrjRWSYSYaHMD24TxWg",  
> > "\_score": 1.0555032,  
> > "\_source": {  
> > "name": "London",  
> > "admin2": "City of London",  
> > "country": "GBR",  
> > "location": "v1hth63636dy",  
> > "rank": 1  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "WGd0GQSGS\_e6Y14QoU0zCw",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Bradford",  
> > "country": "GBR",  
> > "location": "v1w637h6gzw6",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "JVEMN2DdRZqHEVCyJynoWg",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Powys",  
> > "country": "GBR",  
> > "location": "v1m6d631w63y",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "j5uNub82RRqqr03EefpmtA",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Shropshire",  
> > "country": "GBR",  
> > "location": "v1m6d631w63y",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "6oXW6HiyQjakxShTcKhpWg",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Worcestershire",  
> > "country": "GBR",  
> > "location": "v1m1wygzz03t",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "7UOGKwu5RXGyGaLcVkAIAQ",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Gloucestershire",  
> > "country": "GBR",  
> > "location": "v1hygzh00sd1",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > },  
> > {  
> > "\_index": "places",  
> > "\_type": "place",  
> > "\_id": "tnVYn2j2Q-6B6LBWi57g0w",  
> > "\_score": 0.65968955,  
> > "\_source": {  
> > "name": "Little London",  
> > "admin2": "Oxfordshire",  
> > "country": "GBR",  
> > "location": "v1hy07wy36d0",  
> > "rank": 3  
> > },  
> > "\_grouped": false  
> > }  
> > ]  
> > }  
> > }

## -- Alexandre Heimburger VP Engineering blueKiwi Software tel : +33687880997 email : [ahb@bluekiwi-software.com](mailto:ahb@bluekiwi-software.com) adress : 93 rue Vieille du Temple, 75003 Paris

blueKiwi is the innovation leader in Enterprise Social Software. Our  
solutions enable enterprises worldwide to engage and interact with their  
internal and external social networks in multiple business domains.  
blueKiwi has been consistently recognized by Gartner Inc. as a visionary  
provider since 2007.

---

<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:34am UTC](https://discuss.elastic.co/t/text-phrase-prefix-scoring-and-closest-match/7172/4 "2017-07-06T03:34:11Z")

</div>


