动态获取字段中的多个条目而不更改ElasticSearch中的映射?

2024-04-18 05:58:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON文件,如下所示:

{'name':'John Doe',
 'work':[{
 'company':'Google',
 'duration':'2 years',
 'desc':'some long text'},
 {
 'company':'Yahoo',
 'duration':'3 years',
 'desc':'some long text'}]}

工作中可能有从0到未知的多个条目。 如何定义elasticsearch映射以使其自动存储。你知道吗

我试过这样的方法:

from elasticsearch import elasticsearch
mapping:{    
 "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
},
"man":{
   "properties": {
       "name":{"type":"text"},
       "work":{
           "properties": {
               "company" : {"type":"text"},
               "duration": {"type":"text"},
               "desc": {"type":"text"}
               }
            }
        }
   }
}

es = Elasticsearch()

 if es.indices.exists("test"):
    es.indices.delete(index="test")

 es.indices.create("test")
 es.indices.put_mapping(index="test", doc_type="man", body=mapping)

请帮我解决这个问题。谢谢。你知道吗


Tags: textnametestestypesomeelasticsearchdesc
1条回答
网友
1楼 · 发布于 2024-04-18 05:58:46

在elasticsearch中,每个字段都可以是多值的。所以你不必做任何特别的事情来保存值列表。它将自动与其他字段合并。
唯一应该考虑的是对象数组在索引时被展平。所以你会失去任何关联。您的数据将作为工作公司=[“google”,“yahoo”]用于对象中的所有字段。所以你不能查询在谷歌工作了一段时间的人。如果您有那个用例,那么您应该使用嵌套对象。只要将类型嵌套在映射中,就可以了。不需要指定数组类型。你知道吗

相关问题 更多 >