# GeoNames, Autocomplete and boost

**URL:** <https://discuss.elastic.co/t/geonames-autocomplete-and-boost/23516>\
**Category:** Elasticsearch\
**Created:** [May 2, 2015, 12:23am UTC](https://discuss.elastic.co/t/geonames-autocomplete-and-boost/23516 "2015-05-02T00:23:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Info\_Cascade](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@Info\_Cascade](https://discuss.elastic.co/u/Info_Cascade)\
**Post date:** [May 2, 2015, 12:23am UTC](https://discuss.elastic.co/t/geonames-autocomplete-and-boost/23516/1 "2015-05-02T00:23:08Z")

</div>

Hi. I'm trying to improve autocomplete search results on a GeoNames Cities  
index.  
I have been using django-haystack, but have run into issues there. I may  
need to replace it, or bypass it. But my question here pertains to  
indexing and querying with autocomplete using multiple fields.

Users expect to be able to use two-letter abbreviations for states to  
narrow their city choices. For example,  
"San Francisco, CA" and "New York, NY" should have the cities you'd expect  
at the top of the list.  
However that is not the case, and I think for different reasons. You can  
see the results below.

It turns out that there are a lot of San Franciscos in the world!  
Searching for "San Francisco CA" retrieves

San Francisco, Caraga, 13, PH 5.5191193  
San Francisco, Caraga, 13, PH 5.5163627  
San Francisco, Calabarzon, 40, PH 5.4498897  
San Francisco, Calabarzon, 40, PH 5.281434  
San Francisco, Caraga, 13, PH 5.281434  
San Francisco, California, CA, US 5.2123656  
South San Francisco, California, CA, US 4.3138  
San Francisco (El Calvito), Chiapas, 05, MX 4.137272  
San Francisco, Baja California Sur, 03, MX 4.137272  
San Francisco (Baños de Agua Caliente), Guanajuato, 11, MX 3.3008962

I would like to boost the state (region\_code) value so that San Francisco  
and South San Francisco are at the top.

For "New York NY" I get

Nyack, New York, US 3.0575132  
West Nyack, New York, US 2.670291  
South Nyack, New York, US 2.5124028  
Upper Nyack, New York, US 2.5124028

Instead of what I want, which is "New York City, New York, US".

The autocomplete field is EdgeNGram called "content\_auto". It currently  
has the following format, which is what I want to return: "CityName,  
RegionName, CountryCode."

So I think what I want to do in both cases is boost results if there is a  
match on the region\_code field, but _not_ display the region\_code field in  
the results.

The type of the search is currently query\_string, which is what haystack  
uses. If there is some way to make that work, then that would be good.  
However, I'm afraid it is limiting what I'm able to do.

I did some experiments --  
If I query directly with curl for sf using

{  
"query":{  
"multi\_match":{  
"query": "San Francisco CA",  
"type": "cross\_fields",  
"fields": ["content\_auto", "region\_code^3"]  
}  
}  
}

I get a result I'm satisfied with. However the similar query using "New  
York NY" puts the city as the sixth result! I also tried putting the  
region\_code in the content\_auto string, and boosting the region\_code field.  
Also, the following works for SF, but I have no way of knowing in advance  
what the region\_code is going to be. It ranks New York City third, and I  
would have to pick out two-letter combinations.  
"default\_field": "text",  
"default\_operator": "OR",  
"query": "(content\_auto:(san) AND content\_auto:(francisco))  
CA^1.5"

It would really help if someone could help me limit my _own_ queries about  
how ElasticSearch works, so that I can focus on the best approach!

Thanks in advance for your help 🙂

curl 'localhost:9200/cities/\_mapping?&pretty'  
{  
"cities" : {  
"mappings" : {  
"modelresult" : {  
"\_boost" : {  
"name" : "boost",  
"null\_value" : 1.0  
},  
"properties" : {  
"content\_auto" : {  
"type" : "string",  
"analyzer" : "edgengram\_analyzer"  
},  
"django\_ct" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"include\_in\_all" : false  
},  
"django\_id" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"include\_in\_all" : false  
},  
"id" : {  
"type" : "string"  
},  
"location" : {  
"type" : "geo\_point"  
},  
"region\_code" : {  
"type" : "string",  
"analyzer" : "snowball"  
},  
"text" : {  
"type" : "string",  
"analyzer" : "snowball"  
}  
}  
}  
}  
}  
}

NY example:  
curl -XGET '[http://localhost:9200/cities/modelresult/\_search?pretty](http://localhost:9200/cities/modelresult/_search?pretty)' -d '{  
"from": 0,  
"query": {  
"filtered": {  
"filter": {  
"terms": {  
"django\_ct": [  
"cities.city"  
]  
}  
},  
"query": {  
"query\_string": {  
"analyze\_wildcard": true,  
"auto\_generate\_phrase\_queries": true,  
"default\_field": "text",  
"default\_operator": "AND",  
"query": "(content\_auto:(new) AND content\_auto:(york,) AND  
content\_auto:(ny))"  
}  
}  
}  
},  
"size": 10,  
"sort": [  
{  
"\_score": {  
"order": "desc"  
}  
}  
]  
}'  
{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 4,  
"max\_score" : 3.0575132,  
"hits" : [ {  
"\_index" : "cities",  
"\_type" : "modelresult",  
"\_id" : "cities.city.5129433",  
"\_score" : 3.0575132,  
"\_source":{"django\_id": "5129433", "region\_code": "NY", "text":  
"Nyack\nNew York\nNY\nUnited States\nUS\n", "django\_ct": "cities.city",  
"location": "41.09065,-73.91791", "content\_auto": "Nyack, New York, US",  
"id": "cities.city.5129433"}  
}, {  
"\_index" : "cities",  
"\_type" : "modelresult",  
"\_id" : "cities.city.5143946",  
"\_score" : 2.670291,  
"\_source":{"django\_id": "5143946", "region\_code": "NY", "text": "West  
Nyack\nNew York\nNY\nUnited States\nUS\n", "django\_ct": "cities.city",  
"location": "41.09648,-73.97292", "content\_auto": "West Nyack, New York,  
US", "id": "cities.city.5143946"}  
}, {  
"\_index" : "cities",  
"\_type" : "modelresult",  
"\_id" : "cities.city.5138940",  
"\_score" : 2.5124028,  
"\_source":{"django\_id": "5138940", "region\_code": "NY", "text":  
"South Nyack\nNew York\nNY\nUnited States\nUS\n", "django\_ct":  
"cities.city", "location": "41.08315,-73.92014", "content\_auto": "South  
Nyack, New York, US", "id": "cities.city.5138940"}  
}, {  
"\_index" : "cities",  
"\_type" : "modelresult",  
"\_id" : "cities.city.5142011",  
"\_score" : 2.5124028,  
"\_source":{"django\_id": "5142011", "region\_code": "NY", "text":  
"Upper Nyack\nNew York\nNY\nUnited States\nUS\n", "django\_ct":  
"cities.city", "location": "41.10704,-73.92014", "content\_auto": "Upper  
Nyack, New York, US", "id": "cities.city.5142011"}  
} ]  
}  
}

}'

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8722fa24-8172-4a89-b9c1-39bd70f60da3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8722fa24-8172-4a89-b9c1-39bd70f60da3%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:16am UTC](https://discuss.elastic.co/t/geonames-autocomplete-and-boost/23516/2 "2017-07-06T00:16:11Z")

</div>


