Bha*_* Yd 4 full-text-search elasticsearch elasticsearch-6
我正在做一个项目来索引elasticsearch(版本6)中的网站问题和答案以供搜索.
我首先考虑创建两个索引,如下所示,一个用于问题,一个用于答案.
问题映射:
{"mappings": {
"question": {
"properties": {
"title":{
"type":"text"
},
"question": {
"type": "text"
},
"questionId":{
"type":"keyword"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
答案映射:
{"mappings": {
"answer": {
"properties": {
"answer":{
"type":"text"
},
"answerId": {
"type": "keyword"
},
"questionId":{
"type":"keyword"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经使用multimatch查询以及term和top_hits聚合来搜索索引的Q&A(引用的问题).我使用此方法从搜索结果中删除重复项.由于同一问题的答案或问题本身可能会出现在结果中.我只想在结果中每个问题输入一个条目.我面临的问题是对结果进行分页.没有可能的方法在elasticsearch中分页聚合.它只能分页命中而不是聚合.
然后我想到将一个问题和答案保存在一个文档中,答案在Json数组中.这种方法的问题在于没有干净的方法来添加,删除,更新给定问题文档中的特定答案.我发现的唯一方法是使用groovy脚本(引用问题).在elasticsearch v6 AFAIK中已弃用.
有没有更好更干净的设计方法?谢谢.
亲子关系
使用父子关系.它类似于嵌套模型,并允许一个实体与另一个实体的关联.您可以将一种文档类型与另一种文档类型以一对多关系关联.有关此处的更多信息,请访问:https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html
可以添加,更改或删除子文档,而不会影响父级或其他子级.您可以使用Scroll API对父文档进行分页.可以使用has_parent连接检索子文档.
权衡:您不必处理重复和分页问题,但父子查询可能比等效的嵌套查询慢5到10倍.
您的映射可能如下所示:
PUT /my-index
{
"mappings": {
"question": {
"properties": {
"title": {
"type": "text"
},
"question": {
"type": "text"
},
"questionId": {
"type": "keyword"
}
}
},
"answer": {
"_parent": {
"type": "question"
},
"properties": {
"answer": {
"type": "text"
},
"answerId": {
"type": "keyword"
},
"questionId": {
"type": "keyword"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)