本文介绍了过滤非字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用弹性搜索(2.4)

I am using elastic search (2.4)

为了简单起见,这里是我的例子。

For simplicity, here is my example.

想获得第2级所有玩家的列表

I want to get a list of all players on level 2

{player:X,level:2}

我想在级别使用 include ,但显示字段'name'

I want to use include on level, but display the field 'name`

{
  "_source" : {
    "game" : "XYZ",
    "stats" : [ {
      "level" : 2,
      "player" : "Bob"
    }, {
      "level" : 1,
      "player" : "Peter"
    }],
  }
}

我添加了我的映射来显示我的数据:

I added my mapping to show my data:

{
  "tasks" : {
    "aliases" : { },
    "mappings" : {
      "task_document" : {
        "properties" : {
          "game" : {
            "type" : "string"
          },
          "stats" : {
            "type" : "nested",
            "properties" : {
              "player" : {
                "type" : "string"
              },
              "level" : {
                "type" : "string"
              }
            }
          },
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1491798329041",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "Yxxx",
        "version" : {
          "created" : "2040299"
        }
      }
    },
    "warmers" : { }
  }
}

以下是我尝试的示例,没有显示任何结果:

Here is a sample of my attempt which rendered no results:

      "aggs": {
    "inner": {
      "aggs": {
        "stats.player_L2": {
          "terms": {
            "field": "player",
            "include": {"stats.level":2}
            "size": 50
          }
        },
        "stats.player_count": {
          "cardinality": {
            "field": "player"
          }
        }
      },
      "nested": {
        "path": "stats"
      }
    }
  }
},

我也厌倦了在路径上过滤:

I also tired to filter on the path by doing:

  "nested": {
    "path": "stats",
    "filter":{"term":
      {
      "stats.level":2
      }
    }

给出错误:嵌套:SearchParseException [意外的令牌在[内]] START_OBJECT] 为我的尝试

什么是正确的查询来获得JUST 2级的结果并返回字段PLAYER?

What is the correct query to get JUST results of level 2 AND return the field PLAYER?

编辑:添加我的映射

推荐答案

{
  "size": 0,
  "aggs": {
    "inner": {
      "aggs": {
        "stats.player_L2": {
          "terms": {
            "field": "stats.level",
            "include": "2",
            "size": 50
          },
          "aggs": {
            "names": {
              "top_hits": {
                "size": 10
              }
            }
          }
        },
        "stats.player_count": {
          "cardinality": {
            "field": "stats.player"
          }
        }
      },
      "nested": {
        "path": "stats"
      }
    }
  }
}

这篇关于过滤非字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:19