在elasticsearch Elasticsearch中查询复杂的doc

时间:2017-07-27 15:10:45

标签: elasticsearch querydsl

我是弹性研究领域的新人。

我们正在使用我们存储在弹性搜索数据库中的复杂文档来处理我们的项目。

这是我们文档的一部分:

{
   "AAA":[
      {
         "SSS":{
            "Description":{
               "Detail":[
                  {
                     "Name":"A9",
                     "Infos":"...."
                  },
                  {
                     "Name":"A8",
                     "Infos":"...."
                  },
                  {
                     "Name":"A7",
                     "Infos":"...."
                  },
                  {
                     "Name":"A6",
                     "Infos":"...."
                  },
                  {
                     "Name":"A5",
                     "Infos":"...."
                  },
                  {
                     "Name":"A4",
                     "Infos":"...."
                  },
                  {
                     "Name":"A3",
                     "Infos":"...."
                  },
                  {
                     "Name":"A2",
                     "Infos":"...."
                  },
                  {
                     "Name":"A1",
                     "Infos":"...."
                  }
               ]
            }
         }
      }
   ]
}

我想要一个返回"的查询的相关信息" &安培; "名称"例如,"Name":"A1"特定的字段。我不想显示JSON对象详细信息的所有表,而只显示"Name":"A1"的对象。 换句话说,我想要一个返回文档部分的查询:

{
   "AAA":[
      {
         "SSS":{
            "Description":{
               "Detail":[
                  {
                     "Name":"A1",
                     "Infos":"...."
                  }
               ]
            }
         }
      }
   ]
}

我正在使用elasticsearch 5.4 这是我正在使用的映射:

{
   "mappings":{
      "-----":{
         "properties":{
            "AAA":{
               "type":"nested",
               "properties":{
                  "SSS":{
                     "properties":{
                        "Description":{
                           "properties":{
                              "Detail":{
                                 "type":"nested",
                                 "properties":{
                                    "Infos":{
                                       "type":"keyword"
                                    },
                                    "Name":{
                                       "type":"keyword"
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

这是我尝试的两个查询,但它没有工作:

  GET /******/******/_search
{
  "_source":["AAA.SSS.Description.Detail.Name","AAA.SSS.Description.Detail.Infos"],
    "query": {
        "query_string" : {
            "default_field" : "AAA.SSS.Description.Detail.Name",
            "query" : "A1"

        }
    }
}


GET /******/******/_search
{
  "_source":["AAA.SSS.Description.Detail.Name","AAA.SSS.Description.Detail.Infos"],

      "filter": [
        { "term":  { "AAA.SSS.Description.Detail.Name": "A1" }}

      ]
}

我整天都在努力,但对我来说并没有用。 谁能帮我。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

query_string查询无法使用nested数据。您需要在其中创建一个nested query term个查询。这应该让你去:

GET /******/******/_search
{
  "query": {
    "nested": {
      "path": "AAA",
      "query": {
        "nested": {
          "path": "AAA.SSS.Description.Detail",
          "query": {
            "term": {
              "AAA.SSS.Description.Detail.Name": "A1"
            }
          },
          "inner_hits": {
            "_source": {
              "includes": [
                "AAA.SSS.Description.Detail.Name",
                "AAA.SSS.Description.Detail.Infos"
              ]
            }
          }
        }
      }
    }
  }
}

答案 1 :(得分:0)

这是适用于我的示例的查询:

    POST index/type/_search
 {
  "query": {
    "nested": {
      "path": "AAA.SSS.Description.Detail",
        "query": {
          "match": {
            "AAA.SSS.Description.Detail.Name": "A1"
          }
        },
        "inner_hits" : {
          "_source": {
              "includes":["AAA.SSS.Description.Detail.Name","AAA.SSS.Description.Detail.Infos"]
            }
        }
    }
  }
}

这就是结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.89712,
    "hits": [
      {
        "_index": "index",
        "_type": "type",
        "_id": "AV2NxhX8nWBA4r3kkKAM",
        "_score": 1.89712,
        "_source": {
          "AAA": [
            {
              "SSS": {
                "Description": {
                  "Detail": [
                    {
                      "Name": "A9",
                      "Infos": "...."
                    },
                    {
                      "Name": "A8",
                      "Infos": "...."
                    },
                    {
                      "Name": "A7",
                      "Infos": "...."
                    },
                    {
                      "Name": "A6",
                      "Infos": "...."
                    },
                    {
                      "Name": "A5",
                      "Infos": "...."
                    },
                    {
                      "Name": "A4",
                      "Infos": "...."
                    },
                    {
                      "Name": "A3",
                      "Infos": "...."
                    },
                    {
                      "Name": "A2",
                      "Infos": "...."
                    },
                    {
                      "Name": "A1",
                      "Infos": "...."
                    }
                  ]
                }
              }
            }
          ]
        },
        "inner_hits": {
          "AAA.SSS.Description.Detail": {
            "hits": {
              "total": 1,
              "max_score": 1.89712,
              "hits": [
                {
                  "_nested": {
                    "field": "AAA",
                    "offset": 0,
                    "_nested": {
                      "field": "SSS.Description.Detail",
                      "offset": 8
                    }
                  },
                  "_score": 1.89712,
                  "_source": {
                    "AAA": {
                      "SSS.Description.Detail": {
                        "Infos": "....",
                        "Name": "A1"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}