我怎样才能同时搜索所有字段并使用 Elastic 搜索中指定的字段进行搜索?

How can i do both search across all field and search with field specified in Elastic search?

我对弹性搜索还很陌生,我该如何编写一个查询,在文档的所有字段中搜索一个关键字(即测试关键字),然后再编写一个在特定字段中搜索的关键字。

这可以使用 query_string 完成,但我们不能在指定嵌套字段的嵌套字段中进行搜索,所以我使用 LUQUM 转换 lucene 查询到 Elasticsearch DSL。

下面是示例用例:

我有一个映射:

"mappings": {
    "properties": {
      "grocery_name":{
        "type": "text"
       },
      "items": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "stock": {
            "type": "integer"
          },
          "category": {
            "type": "text"
          }
        }
      }
    }
  }
}

数据如下所示

{
  "grocery_name": "Elastic Eats",
  "items": [
    {
      "name": "Red banana",
      "stock": "12",
      "category": "fruit"
    },
    {
      "name": "Cavendish banana",
      "stock": "10",
      "category": "fruit"
    },
    {
      "name": "peach",
      "stock": "10",
      "category": "fruit"
    },
    {
      "name": "carrot",
      "stock": "9",
      "category": "vegetable"
    },
    {
      "name": "broccoli",
      "stock": "5",
      "category": "vegetable"
    }
  ]
}

如何从 grocery_name: Elastic Eats 查询以获取项目名称与 banana 匹配的所有项目?

尝试了 *_all,但没有用。

示例查询:

{
   "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                        "grocery_name": {
                            "query": "Elastic Eats"
                        }
                    }
                },
                {
                    "match": {
                        "*": {
                            "query": "banana",
                            "zero_terms_query": "all"
                        }
                    }
                }
            ]
        }
    }
}

我确定我遗漏了一些明显的东西,但我已经阅读了手册,但我一点也不高兴。

更新: 为嵌套对象启用 include_in_parent 适用于以下查询,但它会在内部复制肯定会影响内存的数据。

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "grocery_name": {
              "query": "Elastic Eats"
            }
          }
        },
        {
          "multi_match": {
              "query": "banana"
          }
        }
      ]
    }
  }
}

还有其他方法吗?

您需要使用 nested match query 和 inner_hits 导致内部嵌套查询自动匹配相关的嵌套级别,而不是根

搜索查询

 {
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "grocery_name": "elastic"
          }
        },
        {
          "nested": {
            "path": "items",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "items.name": "banana"
                    }
                  }
                ]
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

搜索结果:

 "inner_hits": {
          "items": {
            "hits": {
              "total": {
                "value": 2,
                "relation": "eq"
              },
              "max_score": 0.744874,
              "hits": [
                {
                  "_index": "stof_64273970",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "items",
                    "offset": 0
                  },
                  "_score": 0.744874,
                  "_source": {
                    "name": "Red banana",
                    "stock": "12",
                    "category": "fruit"
                  }
                },
                {
                  "_index": "stof_64273970",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "items",
                    "offset": 1
                  },
                  "_score": 0.744874,
                  "_source": {
                    "name": "Cavendish banana",
                    "stock": "10",
                    "category": "fruit"
                  }
                }
              ]
            }

更新 1:

根据您的评论,您可以针对您的用例使用多匹配查询

If no fields are provided, the multi_match query defaults to the index.query.default_field index settings, which in turn defaults to *.

(*) extracts all fields in the mapping that are eligible to term queries and filters the metadata fields. All extracted fields are then combined to build a query.

搜索查询:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "grocery_name": "elastic"
              }
            },
            {
              "nested": {
                "path": "items",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "multi_match": {
                          "query": "banana"    <-- note this
                        }
                      }
                    ]
                  }
                },
                "inner_hits": {}
              }
            }
          ]
        }
      }
    }

更新二:

您需要像这样组合使用多个 bool 查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "grocery_name": {
              "query": "Elastic Eats"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "multi_match": {
                        "query": "banana"
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "nested": {
                        "path": "items",
                        "query": {
                          "bool": {
                            "must": [
                              {
                                "multi_match": {
                                  "query": "banana"
                                }
                              }
                            ]
                          }
                        },
                        "inner_hits": {}
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}