elasticsearch 按多个地理位置排序文档

txu3uszq  于 2022-12-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(107)

我是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是第一。
最后,如果我的设置是不正确的我所需要的,如果有任何其他方法来实现不同的文档结构相同的结果,请让我知道。我仍然在项目的开始,它很容易适应什么是更合适的。

drnojrws

drnojrws1#

文档here指出"Geopoint表示为字符串,格式为:"lat,lon"你位置是"location":"39.615,19.8948",查询可能必须如下:

"branch.address.location": {
     "lat": 39.615,
     "lon": 19.8948
  }

我的测试:

PUT idx_test
{
  "mappings": {
    "properties": {
      "branch": {
        "properties": {
          "address": {
            "properties": {
              "location": {
                "type": "geo_point"
              }
            }
          }
        }
      }
    }
  }
}

POST idx_test/_doc/1
{
  "company_name": "Company #1",
  "branch": [
    {
      "address": {
        "location": "39.615,19.8948"
      }
    }
    ]
}

POST idx_test/_doc/2
{
    "company_name": "Company #2",
    "branch": [
        {
            "address": {
                "location": "39.586,19.9028"
            }
        },
        {
            "address": {
                "location": "39.612,19.9134"
            }
        },
        {
            "address": {
                "location": "39.607,19.8946"
            }
        }
    ]
}

按位置搜索"39.607,19.8946"公司#2

GET idx_test/_search?
{
  "fields": [
    "company_name",
    "branch.address.location"
  ],
  "_source": false,
  "sort": [
    {
      "_geo_distance": {
        "branch.address.location": {
          "lat": 39.607,
          "lon": 19.8946
        },
        "order": "asc", 
        "unit": "km"
      }
    }
  ]
}

回复:

"hits": [
  {
    "_index": "idx_test",
    "_id": "2",
    "_score": null,
    "fields": {
      "branch.address.location": [
        {
          "coordinates": [
            19.9028,
            39.586
          ],
          "type": "Point"
        },
        {
          "coordinates": [
            19.9134,
            39.612
          ],
          "type": "Point"
        },
        {
          "coordinates": [
            19.8946,
            39.607
          ],
          "type": "Point"
        }
      ],
      "company_name": [
        "Company #2"
      ]
    },
    "sort": [
      0
    ]
  },
  {
    "_index": "idx_test",
    "_id": "1",
    "_score": null,
    "fields": {
      "branch.address.location": [
        {
          "coordinates": [
            19.8948,
            39.615
          ],
          "type": "Point"
        }
      ],
      "company_name": [
        "Company #1"
      ]
    },
    "sort": [
      0.8897252783915647
    ]
  }
]

按位置搜索"39.615,19.8948"公司#1

GET idx_test/_search?
{
  "fields": [
    "company_name",
    "branch.address.location"
  ],
  "_source": false,
  "sort": [
    {
      "_geo_distance": {
        "branch.address.location": {
          "lat": 39.615,
          "lon": 19.8948
        },
        "order": "asc", 
        "unit": "km"
      }
    }
  ]
}

回应

"hits": [
  {
    "_index": "idx_test",
    "_id": "1",
    "_score": null,
    "fields": {
      "branch.address.location": [
        {
          "coordinates": [
            19.8948,
            39.615
          ],
          "type": "Point"
        }
      ],
      "company_name": [
        "Company #1"
      ]
    },
    "sort": [
      0
    ]
  },
  {
    "_index": "idx_test",
    "_id": "2",
    "_score": null,
    "fields": {
      "branch.address.location": [
        {
          "coordinates": [
            19.9028,
            39.586
          ],
          "type": "Point"
        },
        {
          "coordinates": [
            19.9134,
            39.612
          ],
          "type": "Point"
        },
        {
          "coordinates": [
            19.8946,
            39.607
          ],
          "type": "Point"
        }
      ],
      "company_name": [
        "Company #2"
      ]
    },
    "sort": [
      0.8897285575578558
    ]
  }
]

相关问题