簡體   English   中英

Elasticsearch 跨多個字段搜索,包括嵌套

[英]Elasticsearch search across multiple fields including nested

我在我的彈性搜索索引上有這個映射:

{
  "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 查詢的信息

添加包含索引數據、搜索查詢和搜索結果的工作示例

指數數據:

{
    "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"
            }
          ]
        }
      }
    ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM