【问题标题】:how can I fetch only inner fields from source in ElasticSearch?如何从 ElasticSearch 的源中仅获取内部字段?
【发布时间】:2021-03-22 00:54:14
【问题描述】:

我有这样的索引结构:

{
          "id" : 42,
          "Person" : {
            "contracts" : [
              {
                "contractID" : "000000000000102"
              }
            ],
            "Ids" : [
              3,
              387,
              100,
              500,
              274,
              283,
              328,
              400,
              600
            ]
          },
          "dateUpdate" : "2020-12-07T13:15:00.408Z"
        }
      },
      ...
}

我需要一个搜索查询,它只会从源中获取内部“Ids”字段,仅此而已。我该怎么做?

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-dsl


    【解决方案1】:

    你可以在inner_hits中使用_source,方式如下

    索引映射:

    {
      "mappings": {
        "properties": {
          "Person": {
            "type": "nested"
          }
        }
      }
    }
    

    搜索查询:

    {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "Person",
                "query": {
                  "match_all": {}
                },
                "inner_hits": {
                  "_source": {
                    "includes": [
                      "Person.Ids"
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "inner_hits": {
              "Person": {
                "hits": {
                  "total": {
                    "value": 1,
                    "relation": "eq"
                  },
                  "max_score": 1.0,
                  "hits": [
                    {
                      "_index": "65237264",
                      "_type": "_doc",
                      "_id": "1",
                      "_nested": {
                        "field": "Person",
                        "offset": 0
                      },
                      "_score": 1.0,
                      "_source": {
                        "Ids": [
                          3,
                          387,
                          100,
                          500,
                          274,
                          283,
                          328,
                          400,
                          600
                        ]
                      }
                    }
                  ]
                }
              }
            }
    

    你也可以使用nested inner_hits and _souce,方式如下

    {
      "query": {
        "nested": {
          "path": "Person",
          "query": {
            "match_all": {}
          },
          "inner_hits": {
            "_source" : false,
            "docvalue_fields" : [
              {
                "field": "Person.Ids",
                "format": "use_field_mapping"
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

    • @Abyrbalg 已经很久了。我希望你做得很好?你有机会浏览我的答案吗,期待得到你的反馈?
    • @Abyrbalg 如果我的回答帮助您解决了您的问题,那么请不要忘记投票并接受答案?
    • @Abyrbalg 感谢您接受我的回答,您能否通过单击答案旁边的向上箭头来投票赞成答案?
    猜你喜欢
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2016-12-03
    相关资源
    最近更新 更多