我是Elasticsearch的新手,我想知道我的用例是否有任何好的实践。
我从API发送异构数据,我保存到数据库(作为JSON),然后保存在Elasticsearch中用于搜索目的。以这种格式发送的数据(因为它是异构的,用户可以发送任何类型的数据,一些元数据可以是多值的,其他单个值和JSON中的键的名称可能会有所不同:)
{
"indices":{
"MultipleIndices":[
{
"index":"editors",
"values":[
"The Editing House",
"Volcan Editing"
]
},
{
"index":"colors",
"values":[
"Red",
"Blue"
]
}
],
"SimpleIndices":[
{
"index":"AuthorName",
"value": "George R. R. Martin"
},
{
"index":"NumberOfPages",
"value":"2898"
},
{
"index":"BookType",
"value":"Fantasy"
}
]
}
}
一旦我们收到这个JSON,它就会在代码中格式化,并以这种格式作为JSON存储在数据库中:
{
"indices":{
"editors":[
"The Editing House",
"Volcan Editing"
],
"colors":[
"Red",
"Blue"
],
"AuthorName" : "George R. R. Martin"
"NumberOfPages" : "2898",
"BookType" : "Fantasy"
}
}
然后我想将这些数据保存到Elasticsearch中,映射它的最佳方式是什么?将其存储为一个字段中的JSON?如果我这样做,搜索会有效吗?
您必须单独映射每个字段。您可以查看字段类型以了解哪种类型最适合您的架构。另一个建议是研究文本分析,因为它负责构建文本以优化搜索的过程。
我的建议图:
PUT indices
{
"mappings": {
"properties": {
"editors": {
"type": "keyword"
},
"colors":{
"type": "keyword"
},
"author_name":{
"type": "text"
},
"number_pages":{
"type": "integer"
},
"book_type":{
"type": "keyword"
}
}
}
}
我认为在您的情况下,除了动态映射之外,您没有太多选择,一旦第一个文档是特定索引中的索引,Elasticsearch就会为您生成动态映射。
但是,您可以通过使用动态模板来改进流程,以便优化您的映射,在我提供的官方链接中有很好的示例。