Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在elasticsearch聚合器-嵌套分组之间建立关系_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Elastic Stack - Fatal编程技术网 elasticsearch,elastic-stack,Python,elasticsearch,Elastic Stack" /> elasticsearch,elastic-stack,Python,elasticsearch,Elastic Stack" />

Python 在elasticsearch聚合器-嵌套分组之间建立关系

Python 在elasticsearch聚合器-嵌套分组之间建立关系,python,elasticsearch,elastic-stack,Python,elasticsearch,Elastic Stack,我需要在字段之间创建嵌套分组 让我们考虑下面给出的例子, 文件: { "keyword": "abc", "country": "IN", "state": "TN", "city": "Chennai" }, { "keyword": "abc", "country": "IN", "state": "TN", "city": "Trichy" }, { "keyword": "

我需要在字段之间创建嵌套分组

让我们考虑下面给出的例子,

文件:

 {
     "keyword": "abc",
     "country": "IN",
     "state": "TN",
     "city": "Chennai"
   },
   {
     "keyword": "abc",
     "country": "IN",
     "state": "TN",
     "city": "Trichy"
   },
   {
     "keyword": "abc",
     "country": "IN",
     "state": "KL",
     "city": "TVM"
   },
   {
     "keyword": "abc",
     "country": "US",
     "state": "Cal",
     "city": "California"
   }
所需输出(类似以下内容):

使用的查询:

 {
     "from": 0,
     "size": 1,
     "aggs": {
         "country": {
             "terms": {
                 "field": "country.keyword",
                 "size": 50000
             }
         },
         "state": {
             "terms": {
                 "field": "state.keyword",
                 "size": 50000
             }
         },
         "city": {
             "terms": {
                 "field": "city.keyword",
                 "size": 50000
             }
         }
     },
     "query": {
         "query_string": {
             "query": "(keyword:abc) "
         }
     }
 }
对于这个查询,我得到了单独的bucket作为城市、州和国家的输出

但我需要的是,城市应该归入州,州应该归入国家


提前感谢。

以下带有聚合的查询应该适合您

{
  "query": {
    "query_string": {
      "query": "(keyword:abc)"
    }
  }, 
  "size": 0, 
  "aggs": {
    "country_agg": {
      "terms": {
        "field": "country.keyword",
        "size": 10
      },
      "aggs": {
        "state_agg": {
          "terms": {
            "field": "state.keyword",
            "size": 10
          },
          "aggs": {
            "city_agg": {
              "terms": {
                "field": "city.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}
{
  "query": {
    "query_string": {
      "query": "(keyword:abc)"
    }
  }, 
  "size": 0, 
  "aggs": {
    "country_agg": {
      "terms": {
        "field": "country.keyword",
        "size": 10
      },
      "aggs": {
        "state_agg": {
          "terms": {
            "field": "state.keyword",
            "size": 10
          },
          "aggs": {
            "city_agg": {
              "terms": {
                "field": "city.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}