如何在 Elasticsearch 中存储 country/state/city 信息

How to store country/state/city information in Elasticsearch

如何在 Elasticsearch 中存储 country/state/city 信息

i.e there are many countries
each country has many states
each state has many cities

它更容易存储在关系数据库中,但如果我想存储所有可能的组合,我应该如何在 Elasticsearch 中执行此操作

我想在包含用户信息的某个索引中存储国家、州、城市位置

i.e users (first_name, last_name, country, state, city ...)

数据重复是任何 NoSQL database/service 的 trade-off,包括 ElasticSearch。这里的主要思想是,您不需要包含所有可能 city/state/country 组合的单独索引。

话虽如此,每个用户可能只居住在一个城市。另外,一个人的 city/state/country 不太可能经常改变。当它发生时,您只需更新该用户的文档。如此频繁的更新(NoSQL 的一个重要缺点)在这里不会有任何重大问题。

建议的是确保您设置 fielddata: true and/or make your city/state/country fields also of the keyword data type so you can swiftly answer questions such as 'In which state do most of my users reside?' etc by employing aggregations.

重要:一定要考虑一些 normalization/standarization,尤其是。当涉及到城市和州时。如果我们谈论 U.S.:

  • 有些人可能会输入他们的状态 Massachusetts,其他人可能会输入 Mass.,大多数人会输入 MA。你打算怎么处理?
  • 同样,NYC不同于New York,后者不同于New York City,也不同于new york city

通常 做的是一个地址 autocomplete/dropdown(开源和付费服务都可用),它将为您提供相当数量的标准化,以便您可以节省直接在您的 elasticsearch 索引中的用户信息。还有地理编码方面,但那是另一回事。

请不要将 Elasticsearch 与 RDBMS 混淆,因为您没有提到您的 use-case 是什么,即它的 full-text 搜索或聚合,我将向您展示如何实现 full-text 使用您的数据进行搜索,它很容易实现,不需要太多 configuration/complexity 即可实现。

因为一个用户一次只能停留在一个城市、州和国家,但如果您想为用户存储多个选项,这也是可以做到的,您只需要索引 , 分隔值。

如果您需要这些字段的聚合,请将这些字段索引为 keyword,以便您可以对其进行聚合。

full-text 搜索的完整示例

索引映射

{
  "mappings" :{
      "properties" :{
          "first_name" :{
              "type" : "text"
          },
          "last_name" :{
              "type" : "text"
          },
          "country" :{
              "type" : "text"
          },
          "state" :{
              "type" : "text"
          },
          "city" :{
              "type" : "text"
          }
      }
  }
}

索引示例文档

{
  "first_name" : "abc",
  "last_name" : "xyz",
  "country": "USA",
  "state" : "California",
  "city" : "SF"
}
{
  "first_name" : "opster",
  "last_name" : "ninja",
  "country": "Israel",
  "state" : "na",
  "city" : "tel aviv"
}
{
  "first_name" : "abc",
  "last_name" : "xyz",
  "country": "USA",
  "state" : "California, washintion", // not two state
  "city" : "SF"
}

现在搜索 California 将 return 第一个和第三个文档如下所示

{
    "query": {
        "match": {
            "state": "california"
        }
    }
}

和搜索结果

 "hits": [
            {
                "_index": "so_63601020",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.38845783,
                "_source": {
                    "first_name": "abc",
                    "last_name": "xyz",
                    "country": "USA",
                    "state": "California",
                    "city": "SF"
                }
            },
            {
                "_index": "so_63601020",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.2863813,
                "_source": {
                    "first_name": "foo",
                    "last_name": "bar",
                    "country": "USA",
                    "state": "California, washington",
                    "city": "SF"
                }
            }
        ]