我是ElasticSearch
的新手,我试图为在该市拥有多个分支机构的公司创建一个指数。
每个分支都有自己的地理定位点。
我的公司文档如下所示:
{
"company_name": "Company X",
"branch": [
{
"address": {
// ... other fields
"location": "0.0000,1.1111"
}
}
]
}
索引具有以下Map:
{
"companies": {
"mappings": {
"dynamic_templates": [
{
"ids": {
"match": "id",
"match_mapping_type": "long",
"mapping": {
"type": "long"
}
}
},
{
"company_locations": {
"match": "location",
"match_mapping_type": "string",
"mapping": {
"type": "geo_point"
}
}
}
],
"properties": {
"branch": {
"properties": {
"address": {
"properties": {
// ...
"location": {
"type": "geo_point"
},
// ...
}
},
}
}
}
}
}
}
现在,在ElasticSearch中,我索引了以下文档:
{
"company_name": "Company #1",
"branch": [
{
"address": {
"location": "39.615,19.8948"
}
}
]
}
以及
{
"company_name": "Company #2",
"branch": [
{
"address": {
"location": "39.586,19.9028"
}
},
{
"address": {
"location": "39.612,19.9134"
}
},
{
"address": {
"location": "39.607,19.8946"
}
}
]
}
现在我的问题是,如果我尝试运行下面的搜索查询,不幸的是,第一个显示的公司是Company #2
,尽管geodistance查询具有Company #1
的位置数据:
GET companies/_search
{
"fields": [
"company_name",
"branch.address.location"
],
"_source": false,
"sort": [
{
"_geo_distance": {
"branch.address.location": {
"lon": 39.615,
"lat": 19.8948
},
"order": "asc",
"unit": "km"
}
}
]
}
我做错了什么吗?有没有办法用这个方法对搜索结果进行排序?
请记住,如果例如搜索的地理位置是更接近一些地理位置的“公司#2”,在这种情况下,我需要的Company #2
是第一。
最后,如果我的设置是不正确的我所需要的,如果有任何其他方法来实现不同的文档结构相同的结果,请让我知道。我仍然在项目的开始,它很容易适应什么是更合适的。
1条答案
按热度按时间drnojrws1#
文档here指出"Geopoint表示为字符串,格式为:"lat,lon"你位置是"location":"39.615,19.8948",查询可能必须如下:
我的测试:
按位置搜索"39.607,19.8946"公司#2
回复:
按位置搜索"39.615,19.8948"公司#1
回应