【问题标题】:Elasticsearch search across multiple fields including nested跨多个字段的 Elasticsearch 搜索,包括嵌套
【发布时间】:2020-10-01 12:19:07
【问题描述】:

我的弹性搜索索引上有这个映射:

{
  "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", ......,其中......是上面的查询,但如果是这样,我无法搜索标题、描述、内容。所以请告诉我如何从多个嵌套级别进行查询。

【问题讨论】:

  • 请仔细阅读我的回答,如果这是您的问题,请告诉我?

标签: elasticsearch


【解决方案1】:

您需要结合嵌套和非嵌套搜索查询。参考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"
            }
          ]
        }
      }
    ]

【讨论】:

  • 好吧,它工作,但不是我想要的方式。你能帮助我吗?我需要在所有字段中搜索相同的内容。例如,我可以从所有字段中创建一个大字符串并在其中搜索。当我使用我的方法通过在内容字段中具有大字符串的实体进行搜索时,它可以正常工作,但是当我使用您的方法以相同的方式搜索时,它返回空数组。如何将字段中的所有值连接到一个大字符串中并在其中搜索?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多