如何获取一条记录中多个匹配字段的匹配数?

时间:2015-08-08 15:27:48

标签: elasticsearch

我的记录类似于

{
    "who": "John",
    "hobby": [
        {"name": "gardening",
         "skills": 2
         },
        {"name": "sleeping",
         "skills": 3
         },
        {"name": "darts",
         "skills": 2
         }
    ]
}
,
{
    "who": "Mary",
    "hobby": [
        {"name": "gardening",
         "skills": 2
         },
        {"name": "volleyball",
         "skills": 3
         },
        {"name": "kung-fu",
         "skills": 2
         }
    ]
}

我正在寻找能够回答这个问题的查询:“我们有多少技能爱好= 2?”

以上示例的答案为3"gardening"对两者都是通用的,并且每个都有另一个唯一的答案。)

"query""query" + "aggs"我尝试在['hits']['hits']或['aggregations']['sources']['buckets']匹配的文档数中返回,在上面的情况中是两个(一个用于"John",一个用于"Mary",每个都满足查询。)

有没有办法构建一个查询,以便它返回与该查询匹配的字段总数(在上面的示例中:列表"hobby"的元素?)(字段,而不是文件)

注意:如果我的文件是扁平的:

{"who": "John", "name": "gardening", "skills": 2},
{"who": "John", "name": "sleeping", "skills": 3},
(...)
{"who": "Mary", "name": "kung-fu", "skills": 2}

然后一个简单的"query"匹配“skills": 2 + "name"上的聚合就可以完成这项工作

1 个答案:

答案 0 :(得分:1)

是的,您可以使用nested type并使用inner_hits和/或nested aggregations来实现这一目标。

所以这是您应该使用的映射:

curl -XPUT localhost:9200/hobbies -d '{
  "mappings": {
    "hob": {
      "properties": {
        "who": {
          "type": "string"
        },
        "hobby": {
          "type": "nested",      <--- the hobby list is of type nested
          "properties": {
            "name": {
              "type": "string",
              "index": "not_analyzed"
            },
            "skills": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}

然后我们可以使用_bulk端点插入两个示例文档,如下所示:

curl -XPOST localhost:9200/hobbies/hob/_bulk -d '
{"index":{}}
{"who":"John", "hobby":[{"name": "gardening","skills": 2},{"name": "sleeping","skills": 3},{"name": "darts","skills": 2}]}
{"index":{}}
{"who":"Mary", "hobby":[{"name": "gardening","skills": 2},{"name": "volley-ball","skills": 3},{"name": "kung-fu","skills": 2}]}
'

最后,我们可以查询您的索引有多少爱好skills: 2这样:

curl -XPOST localhost:9200/hobbies/hob/_search -d '{
  "_source": false,
  "query": {
    "nested": {
      "path": "hobby",
      "query": {
        "term": {
          "hobby.skills": 2
        }
      },
      "inner_hits": {}         <---- this will return only the matching nested fields with skills=2
    }
  },
  "aggs": {
    "hobbies": {
      "nested": {
        "path": "hobby"
      },
      "aggs": {
        "skills": {
          "filter": {
            "term": {
              "hobby.skills": 2
            }
          },
          "aggs": {
            "by_field": {         <--- this will return a breakdown of the fields with skills=2
              "terms": {
                "field": "name"
              }
            }
          }
        }
      }
    }
  }
}'

此查询将返回给您的是

  1. hits部分中,skills: 2
  2. 的四个字段
  3. aggs部分中,分析了skills: 2
  4. 的3个不同字段