【问题标题】:Create index in elasticsearch with different document types在弹性搜索中使用不同的文档类型创建索引
【发布时间】:2020-02-14 16:23:32
【问题描述】:

我需要在弹性中索引“客户”实体。我的对象“客户”人由几个片段(JSON 文档)组成,例如

COMMON (firstname, lastname etc),
EDUCATION (fields...),
JOB (fields...) and so on. 

所以我的索引必须存储所有这些段(JSON 文档)。然后我必须通过字段和段的不同组合进行搜索,例如:search word "university" in COMMON.firstname, COMMON.lastname, EDUCATION.field1, EDUCATION.field2。并且我可以将搜索结果作为包含所有细分的客户列表返回

【问题讨论】:

    标签: elasticsearch indexing full-text-search elastic-stack


    【解决方案1】:

    我会说,一个文档可以是这样的

    {
      ...common properties,
      "education": {
        ...education properties
      },
      "job": {
        ...job properties
      }
    }
    

    为了索引这样的文档,你可以执行下一个查询(一个新的索引,如果不存在,将自动创建)

    PUT /client/doc/1
    {
      "firstName": "...",
      "lastName": "...",
      ...other common properties,
      "education": {
        ...education properties
      },
      "job": {
        ...job properties
      }
    }
    

    其中 client 是索引名称,doc 是类型,1 是新文档的 id。

    然后你可以通过执行得到一个客户端列表(默认10个)

    GET /client/doc/_search
    

    为了进行搜索,您可以执行(这也将返回最多 10 个文档,因为默认值为 10)

    GET /client/doc/_search
    {
      "query": {
        "query_string" : {
          "query" : "firstName:university OR lastName:university OR education.field1:university OR education.field1:university",
          "default_field" : "content"
        }
      }
    }
    

    如果您想明确指定所有或部分属性的数据类型,请查看dynamic mapping。否则将根据值分配默认数据类型,例如字符串值的“文本”等。

    【讨论】:

    • 非常感谢您的回复。但是你能澄清一下这只是一种解决方案吗?因为我有大约 50-100 个片段,其中一些很少更改,其中一些经常会更改很多次,所以我必须更新包含 100 个片段的整个文档,即使一个片段中的一个字段每小时更新一次,如果我有 500 000 个客户,我将每小时更新 500 000 条完整记录
    • Elastic 不会更新文档,它会删除现有文档,然后索引新文档。但是,从您的用户角度来看,Elastic 支持部分更新。这意味着您可以提交部分文档,该文档将与现有文档合并。是的,这将是对文档的完全重写,但您不需要提交整个文档。如果您担心数据一致性,请记住,Elastic 不提供“事务”功能,因此您可以考虑使用传统的 RDB。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2018-03-27
    • 2015-12-18
    • 2023-03-05
    • 1970-01-01
    • 2015-09-05
    • 2018-05-17
    相关资源
    最近更新 更多