我有一个看起来像这样的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)
请帮我。谢谢。
最佳答案
在Elasticsearch中,每个字段都可以是多值的。因此,您不必专门做任何事情来保存值列表。它将自动与其他字段合并。
您唯一应该考虑的是在索引时间将对象数组展平。因此,您将失去任何关联。您对象中所有字段的数据都将被索引为work.company = [“google”,“yahoo”]。因此,您无法查询在Google工作了一段时间的人。如果有该用例,则应使用嵌套对象。只需将类型嵌套在映射中即可。无需指定数组类型。
关于python - 在字段中动态获取多个条目而无需更改ElasticSearch中的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844603/