【问题标题】:How to return only the matched object in a nested field, instead of the whole object?如何仅返回嵌套字段中的匹配对象,而不是整个对象?
【发布时间】:2020-10-06 14:38:42
【问题描述】:

Elasticsearch 版本(bin/elasticsearch --version): 5.6.11 已安装插件:

JVM 版本(java -version): openjdk 版本“1.8.0_222” OpenJDK 运行环境 (build 1.8.0_222-8u222-b10-1ubuntu1~16.04.1-b10) OpenJDK 64-Bit Server VM(build 25.222-b10,混合模式)

操作系统版本(uname -a 如果在类 Unix 系统上): Linux myserver 4.4.0-131-generic #157-Ubuntu SMP Thu Jul 12 15:51:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

我目前正在尝试尽可能避免创建另一个索引,结果我遇到了以下问题: 如何仅检索嵌套对象中的匹配元素而不是整个对象?在我的情况下是:my_nested_field.my_object.name 符合我的条件?

我的映射:

{
  "my_super_special_index": {
    "aliases": {
      "my_super_special_index_alias": {}
    },
    "mappings": {
      "my_super_special_index": {
        "properties": {
          "my_nested_field": {
            "type": "nested",
            "properties": {
              "my_object": {
                "properties": {
                  "id": {
                    "type": "integer"
                  },
                  "last_known_party": {
                    "type": "boolean"
                  },
                  "name": {
                    "type": "text",
                    "store": true,
                    "analyzer": "translation_index_analyzer",
                    "search_analyzer": "translation_search_analyzer"
                  },
                  "name_raw": {
                    "type": "keyword"
                  },
                }
              }
            }
          }
        }
      }
    }
  }
}

我的查询:

GET my_super_special_index/_search
{
  "_source": "my_nested_field",
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "my_nested_field",
            "query": {
              "bool": {
                "should": [
                  {
                    "match_phrase": {
                      "my_nested_field.my_object.name": {
                        "query": "my name"
                      }
                    }
                  },
                  {
                    "match": {
                      "my_nested_field.my_object.name": {
                        "query": "my name",
                        "boost": 100
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "size": 50
}

我得到了什么结果:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1337,
    "hits": [
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2433.0676,
        "_source": {
          "id": "4712182",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "22222",
                  "name": "name i want",
                  "name_raw": "name i want",
                  "last_known_party": true
                },
                {
                  "id": "13333",
                  "name": "hiyoo definitely doesnt match",
                  "name_raw": "hiyoo definitely doesnt match",
                  "last_known_party": true
                }
              ],
              "my_other_object": [
                {
                  "id": "26672",
                  "name": "dont really like this",
                  "name_raw": "dont really like this",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      },
      
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2422.111,
        "_source": {
          "id": "357878",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "661111",
                  "name": "ratatoille",
                  "name_raw": "ratatoille",
                  "last_known_party": true
                },
                {
                  "id": "2334",
                  "name": "name i want or close match",
                  "name_raw": "name i want or close match",
                  "last_known_party": true
                }
              ],
              "my_other_object": [
                {
                  "id": "63111",
                  "name": "ttttt ok 1337",
                  "name_raw": "ttttt ok 1337",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      }
  }
}

我希望从 ES 得到什么:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1337,
    "hits": [
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2433.0676,
        "_source": {
          "id": "4712182",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "22222",
                  "name": "name i want",
                  "name_raw": "name i want",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      },
      
      {
        "_index": "my_super_special_index",
        "_type": "my_super_special_index",
        "_score": 2422.111,
        "_source": {
          "id": "357878",
          "my_nested_fields": [
            {
              ...
              "my_object": [
                {
                  "id": "2334",
                  "name": "name i want or close match",
                  "name_raw": "name i want or close match",
                  "last_known_party": true
                }
              ]
            }
            ],
        }
      }
  }
}

提前致谢!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    Nested inner_hist 救援:

    GET my_super_special_index/_search
    {
      "_source": "my_nested_field",
      "query": {
        "bool": {
          "should": [
            {
              "nested": {
                "path": "my_nested_field",
                "inner_hits": {},                        <--- add this
                "query": {
                  "bool": {
                    "should": [
                      {
                        "match_phrase": {
                          "my_nested_field.my_object.name": {
                            "query": "my name"
                          }
                        }
                      },
                      {
                        "match": {
                          "my_nested_field.my_object.name": {
                            "query": "my name",
                            "boost": 100
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      "size": 50
    }
    

    注意:由于my_object 也不是nested,因此您仍然会获得完整的数组,但您只会获得my_nested_fields 对象而不是my_other_object 一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多