【问题标题】:elasticsearch query nested array of objectselasticsearch查询嵌套的对象数组
【发布时间】:2019-02-04 00:37:57
【问题描述】:

您好,我正在尝试获取一个查询以根据对象数组中的值进行过滤,结构是这样的

{
  "_index": "test",
  "_type": "home",
  "_id": "1247816",
  "_score": 1,
  "_source": {
    "TranCust": {
      "CustId": 1247816,
      "sourceNodeName": "SRC"
    },
    "TranList": [
      {
        "TranId": 2431015,
        "batchNr": "211"
      },
      {
        "TranId": 2431016,
        "batchNr": "213"
      }
    ]
  }
}

例如,我想查找 TranId 为 2431015 的所有文档,我的查询如下所示

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "TranList",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "TranId": "2431015"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

它似乎没有返回任何结果,有没有更好的方法来尝试编写这个查询?

编辑, 这是放入的映射

{
 "mappings": {
    "home": {
        "properties": {
            "TranCust": {
                "type": "object"
                }
            },
            "TranList": {
                "type": "nested"
            }
        }
    }
 }
}

【问题讨论】:

  • 您确定您的对象存储为nested type,而不是object
  • 我已经添加了我在问题中输入的映射

标签: elasticsearch


【解决方案1】:

好的,经过多次尝试,这就是我的工作方式

{"query": {
"bool": {
  "must": [
    {
      "nested": {
        "path": "TranList", 
        "query": {
          "bool": {
            "must": [ 
              { "match": { "TranList.TranId ": "2431015" }}
            ]
    }}}}
  ]
}}
}

【讨论】:

    【解决方案2】:

    不确定您的 ES 版本是什么,但以下内容在理想情况下应该适用于 ES 6.x+ 版本。您实际上不需要用 bool:must 包装您的嵌套查询

    {
        "query": {
            "nested" : {
                "path" : "TranList",
                "query" : {
                    "bool" : {
                        "must" : [
                            { "match" : {"TranList.TranId" : "2431015"} }
                        ]    
                    }
                }
            }
        }
    }
    
    

    【讨论】:

      【解决方案3】:
      {
        "query": {
          "query_string": {
            "default_field": "TranList.TranId",
            "query": "2431015"
          }
        }
      }
      

      【讨论】:

      • 您能否详细说明您的答案是如何解决问题的?
      猜你喜欢
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      相关资源
      最近更新 更多