在Elasticsearch中使用不同文档类型创建索引

时间:2019-10-17 17:26:05

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

我需要以弹性索引“ CLIENT”实体。我的对象“ CLIENT”人由几个细分(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。并且我可以将所有细分作为“客户列表”返回搜索结果

1 个答案:

答案 0 :(得分:0)

我想说的是,文档可以像这样

{
  ...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。否则,将根据这些值分配默认的数据类型,例如字符串值的“文本”等。