如何只返回嵌套字段中匹配的对象,而不返回整个对象?

时间:2020-10-06 06:36:52

标签: elasticsearch

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

JVM版本java -version): openjdk版本“ 1.8.0_222” OpenJDK运行时环境(内部版本1.8.0_222-8u222-b10-1ubuntu1〜16.04.1-b10) OpenJDK 64位服务器VM(内部版本25.222-b10,混合模式)

操作系统版本(如果为类Unix系统,则为uname -a): 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
                }
              ]
            }
            ],
        }
      }
  }
}

谢谢!

1 个答案:

答案 0 :(得分: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对象,而不是{{1} }一。