gpt4 book ai didi

elasticsearch - Elasticsearch跨多个字段进行搜索,包括嵌套

转载 作者:行者123 更新时间:2023-12-03 02:13:16 28 4
gpt4 key购买 nike

我在我的elasticsearch索引上有此映射:

{
"mappings": {
"properties": {
"title": {
"type": "keyword"
},
"description": {
"type": "text"
},
"content": {
"type": "text"
},
"author": {
"type": "keyword"
},
"completionTimeMinutes": {
"type": "integer"
},
"views": {
"type": "integer"
},
"questions": {
"type": "nested",
"properties": {
"question": {
"type": "keyword"
},
"answers": {
"type": "keyword"
},
"rightAnswer": {
"type": "integer"
}
}
}
}
}
}
而且我在从多个字段进行查询时遇到了一些问题。就我而言,我想搜索标题,描述,内容,questions.question,questions.answer。
我的查询无效,如下所示:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "text to search i put here",
"type": "cross_fields",
"fields": [
"title",
"description",
"content",
"questions.question",
"questions.answers"
],
"slop": 1
}
}
}
}
}
我也尝试过 {"query": { "nested": { "path": "questions", ......,其中......是上面的查询,但是如果是这样,我就无法搜索标题,描述,内容。因此,请告诉我如何从多个嵌套级别进行查询。

最佳答案

您需要组合嵌套和非嵌套搜索查询。请参阅ES官方文档,以了解有关bool queries的更多信息。
添加带有索引数据,搜索查询和搜索结果的工作示例
索引数据:

{
"title": "a",
"description": "a",
"content": "b",
"questions": [
{
"question": "c",
"answers": "c"
}
]
}
搜索查询:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "a",
"type": "cross_fields",
"fields": [
"title",
"content"
],
"slop": 1
}
},
{
"nested": {
"path": "questions",
"query": {
"multi_match": {
"query": "c",
"type": "cross_fields",
"fields": [
"title",
"content",
"questions.question",
"questions.answers"
],
"slop": 1
}
}
}
}
],
"minimum_should_match":1
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_64155263",
"_type": "_doc",
"_id": "1",
"_score": 0.5753642,
"_source": {
"title": "a",
"description": "a",
"content": "b",
"questions": [
{
"question": "c",
"answers": "c"
}
]
}
}
]

关于elasticsearch - Elasticsearch跨多个字段进行搜索,包括嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64155263/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com