# Elastic result search by text with nearest distance using latitude and longitude

**URL:** <https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718>\
**Category:** Elasticsearch\
**Created:** [February 11, 2014, 6:14am UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718 "2014-02-11T06:14:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vallabh](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vallabh](https://discuss.elastic.co/u/vallabh)\
**Post date:** [February 11, 2014, 6:14am UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718/1 "2014-02-11T06:14:19Z")

</div>

Dear All,

I am using elastic for venue search in that i do have title, latitude and  
longitude.  
I have some what 4 lac data.

_My concern is that i wanted to search the text which will produce the  
relevant result with nearest distance._

Right now i am using "geo\_distance"

you can see my below code so that you can come to know what i am using,

curl -X PUT '[http://localhost:9200/adminvenue/?pretty=true](http://localhost:9200/adminvenue/?pretty=true)' -d '  
{  
"settings" : {  
"analysis" : {  
"analyzer" : {  
"venue\_analyzer" : {  
"tokenizer" : "standard",  
"filter" : ["standard", "lowercase", "venue\_metaphone",  
"asciifolding"]  
}  
},  
"filter" : {  
"venue\_metaphone" : {  
"type" : "phonetic",  
"encoder" : "metaphone",  
"replace" : false  
}  
}  
}  
}  
}

curl -X PUT  
'[http://localhost:9200/adminvenue/jos\_content/\_mapping?pretty=true](http://localhost:9200/adminvenue/jos_content/_mapping?pretty=true)' -d '  
{  
"jos\_content" : {  
"properties" : {

```
  "title" : {
  "type": "string",
  "index_analyzer": "venue_analyzer",
  "search_analyzer": "venue_analyzer"
  },
  "location" : {

```

"type" : "geo\_point",  
"lat\_lon": true  
}

```
}

```

}  
}

$result = $es-\>search(array(  
"query" =\> array(  
"dis\_max" =\> array(  
"queries" =\> array(  
0 =\> array(  
"field" =\> array(  
"title" =\> "palexpo"  
)  
)  
)  
)  
),  
"from" =\> 0,  
"size" =\> 100000,  
"filter" =\> array(  
"geo\_distance" =\> array(  
"distance" =\> "10000mi",  
"jos\_content.location" =\> array(  
"lat" =\> "37.77519600",  
"lon" =\> "-122.41920400"  
)  
)  
),  
"sort" =\> array(  
//0 =\> "\_score",  
0 =\> array(  
"\_geo\_distance" =\> array(  
"jos\_content.location" =\> array(  
"lat" =\> "37.77519600",  
"lon" =\> "-122.41920400"  
),  
"order" =\> "asc",  
"unit" =\> "km"  
)  
)  
)  
)  
);

Sorry for big code,  
Above code gives me nearest distance venue but not palexpo which i actually  
search, palexpo comes after 20 25 results.

Please help if you have any suggestion,  
Thanks,  
Vallabh

--  
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/62b2b233-1818-4e4e-9aee-d42b0683d7c8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/62b2b233-1818-4e4e-9aee-d42b0683d7c8%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 11, 2014, 3:52pm UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718/2 "2014-02-11T15:52:34Z")

</div>

I'm curious, can you show the results (lon, lat values) of the first 25  
matches?

--  
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/b89a5d4b-1804-49b4-b558-8743c45a6191%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b89a5d4b-1804-49b4-b558-8743c45a6191%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![vallabh](https://avatars.discourse-cdn.com/v4/letter/v/e8c25b/32.png) [@vallabh](https://discuss.elastic.co/u/vallabh)\
**Post date:** [February 12, 2014, 6:29am UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718/3 "2014-02-12T06:29:41Z")

</div>

Thanks for replying Binh,

Yeap first 25 matches having closer distance (lat,lon values) but they are  
not relevant matches.

For Ex.  
When i search for "palexpo" from San Francisco (37.77519600,-122.41920400)  
it gives me,

1. PLUGZ -- 37.80664300 -122.41628300 -- 2.181 Miles from san francisco
2. Laemmle's Monica 4-plex -- 34.01522650 -118.49798840 -- 340.395 Miles
3. Riverside Cinema Six Plex -- 35.16709000 -114.57260000 -- 472.071 Miles
4. Paramount Tri-Plex -- 43.48080000 -111.99220000 -- 673.978 Miles  
.  
.  
.  
.
5. Chez les Ploucs 2 -- 44.84471380 -0.57991900 -- 5683.384 Miles
6. Palexpo -- 46.23106930 6.11826660 -- 5821.611 Miles from san francisco

I know it is sorting by lat,lon values but all 36 results are not relevant.  
I hope u will understand Binh.

Thanks,  
Vallabh

On Tuesday, February 11, 2014 9:22:34 PM UTC+5:30, Binh Ly wrote:

> I'm curious, can you show the results (lon, lat values) of the first 25  
> matches?

--  
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/e38f426f-8b1c-4df0-9307-3b07c7d40322%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e38f426f-8b1c-4df0-9307-3b07c7d40322%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [February 12, 2014, 2:33pm UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718/4 "2014-02-12T14:33:41Z")

</div>

Oh I see, sounds like you want to sort by relevance and then have the  
distance factored into the relevance score also. You might want to take a  
look at the function\_score query:

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#\_using\_function\_score](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_using_function_score)

In it, you can combine say a match query with a function where you set the  
origin of your geo\_point as well as some distance and decay parameters and  
that will influence your score based on distance from your origin point.

--  
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/e011e49f-00b3-46c5-9976-a7029313c00f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e011e49f-00b3-46c5-9976-a7029313c00f%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:50am UTC](https://discuss.elastic.co/t/elastic-result-search-by-text-with-nearest-distance-using-latitude-and-longitude/15718/5 "2017-07-06T01:50:47Z")

</div>


