弹性搜索词聚合中的问题

时间:2018-09-11 08:28:15

标签: python amazon-web-services elasticsearch search aggregation

在弹性搜索聚合查询中,我需要获取观看电影“冻结”的用户观看的所有电影。这就是我的“结果”来源

{
  "_index": "user",
  "_type": "user",
  "_id": "ovUowmUBREWOv-CU-4RT",
  "_version": 4,
  "_score": 1,
  "_source": {
    "movies": [
      "Angry birds 1",
      "PINNOCCHIO",
      "Frozen",
      "Hotel Transylvania 3"
    ],
    "user_id": 86
  }
}

这是我正在使用的查询。

{
  "query": {
    "match": {
      "movies": "Frozen"
    }
  },
  "size": 0,
  "aggregations": {
    "movies_like_Frozen": {
      "terms": {
        "field": "movies",
        "min_doc_count": 1
      }
    }
  }
}

我在存储桶中得到的结果是正确的,但是电影名称被这样的空白分隔

"buckets": [
                {
                    "key": "3",
                    "doc_count": 2
                },
                {
                    "key": "hotel",
                    "doc_count": 2
                },
                {
                    "key": "transylvania",
                    "doc_count": 2
                },
                {
                    "key": "1",
                    "doc_count": 1
                },
                {
                    "key": "angry",
                    "doc_count": 1
                },
                {
                    "key": "birds",
                    "doc_count": 1
                }
            ]

如何获得带有“愤怒的小鸟1”,“ Hotel Transylvania 3”的水桶。

请帮助。

1 个答案:

答案 0 :(得分:1)

在elasticsearch 6.x中,每个文本字段都进行隐式分析。要覆盖此内容,您需要在索引中为文本类型字段创建映射,如not_analyzed,然后在其中插入文档。

就您而言,

{
  "mappings": {
    "user": {
      "properties": {
        "movies": {
          "type": "text",
          "index": "not_analyzed",
          "fields": {
            "keyword": {
              "type": "text",
              "index": "not_analyzed"
            }
          }
        },
        "user_id": {
          "type": "long"
        }
      }
    }
  }
}

希望它能起作用。