在弹性搜索中获取嵌套数据

时间:2018-02-19 06:19:13

标签: elasticsearch

我在弹性搜索中跟踪数据

  {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "<index>",
            "_type": "<type>",
            "_id": "75559264",
            "_score": 1,
            "_source": {
              "request": {
                "parts": [
                  {
                    "lineCode": "GAT",
                    "partId": 0,
                    "reQty": 1,
                    "description": "Serpentine Belt",
                    "partNumber": "K040398",
                    "lineNumber": 1,
                    "brand": "BBSC"
                  },
                  {
                    "lineCode": "GAT",
                    "partId": 0,
                    "reQty": 1,
                    "description": "Timing Belt Kit With Water Pump",
                    "partNumber": "TCKWP312",
                    "lineNumber": 30,
                    "brand": "BBSC"
                  }, <and so on>
                ]
              },
              "response": {
                "parts": [
                  {
                    "lineCode": "AC",
                    "partId": 0,
                    "reQty": 1,
                    "description": "Serpentine Belt",
                    "partNumber": "4K398",
                    "locations": [
                      {
                        "core": 0,
                        "cost": 10,
                        "called": "Store 4",
                        "availQty": 4,
                        "list": 12
                      },
                      {
                        "core": 0,
                        "cost": 10,
                        "called": "Store 5",
                        "availQty": 5,
                        "list": 12
                      }
                    ],
                    "lineNumber": 13,
                    "brand": "BCVC",
                    "status": "Original"
                  },<and so on>
                ]
              },
              "header": {
                "lookup": "EPE",
                "ymme": "2001 HONDA CIVIC 4-1668 1.7L SOHC",
                "transid": "1a97ebd4-514c-43be-be19-2121f2d2b452",
                "created": "2017-12-01T05:37:32",
                "channel": "Pb",
                "errFlg": 0,
                "action": "INQ",
                "id": 75559264
              },
              "documentTimeStamp": "2017-12-01T05:37:47.668+0000"
            }
          }
        ]
      }
    }

现在我想根据线路代码获取数据(相应的部分)(例如:“GAT”)。

我已经使用this文档并执行了以下查询

GET <myindex>/<type>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "request.parts",
            "score_mode": "max", 
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {"parts.lineCode": "GAT"}
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

但在这里,我没有得到任何数据,而是显示以下响应。

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

我是弹性搜索的新手。因此,根据我的要求获取数据的任何帮助都非常受欢迎。

提前致谢!! :)

2 个答案:

答案 0 :(得分:0)

根据documentations: 在path子句中,您必须放置第一级嵌套对象,在您的情况request和匹配子句中,您应该插入嵌套路径request.parts.lineCode。当must子句重复并且没有意义时,您已经编写了一个详细的查询。在源文档中,字段egg不是嵌套的,字段comments.name是嵌套的,因此查询采用该结构,但不是你的情况。试试这个:

{
    "query": {
        "nested" : {
            "path" : "parts",
            "score_mode" : "max",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"parts.lineCode" : "GAT"} }
                    ]
                }
            }
        }
    }
}

如果parts.lineCode有一个关键字数据类型,请试试这个:

{
    "query": {
        "nested" : {
            "path" : "parts",
            "score_mode" : "max",
            "query" : {
                "bool" : {
                    "must" : [
                    { "term" : {"parts.lineCode" : "GAT"} }
                    ]
                }
            }
        }
    }
}

答案 1 :(得分:0)

GET your_index/your_index_type/_search
{
  "query": {
    "nested": {
      "path": "request.parts",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "request.parts.lineCode": "GAT"
              }
            }
          ]
        }
      }
    }
  }
}

这应该工作Veswanth !!!!!!!!!!!!