Elasticsearch - 加入两种不同类型的文档

oha*_*nho 3 elasticsearch

我有这些文件:

我的电影

{
    "_index": "mymovies",
    "_type": "mymovie",
    "_id": "1",
    "_score": 1,
    "_source": {
     "title" : "Funny title is here"
     "genre" : "Comedy"
     "movieViews" : 901142
    }
}


 {
        "_index": "mymovies",
        "_type": "mymovie",
        "_id": "2",
        "_score": 1,
        "_source": {
         "title" : "Sad title is here"
         "genre" : "Drama"
         "movieViews" : 90
        }
 }

 {
        "_index": "mymovies",
        "_type": "mymovie",
        "_id": "3",
        "_score": 1,
        "_source": {
         "title" : "Sad Second title is here"
         "genre" : "Drama"
         "movieViews" : 9022
        }
 }

 {
        "_index": "mymovies",
        "_type": "mymovie",
        "_id": "4",
        "_score": 1,
        "_source": {
         "title" : "Horror title is here"
         "genre" : "Horror"
         "movieViews" : 9022
        }
 }
Run Code Online (Sandbox Code Playgroud)

用户

{
  "Name" : "Doni de brun",
  "moviesLiked": [2,1,3]      
}
Run Code Online (Sandbox Code Playgroud)
  1. 如何仅获取特定用户喜欢的类型?

  2. 有没有更好的方法来组织此查询的数据?

pkh*_*lop 5

以下查询将完成这项工作:

POST movies/_search
{
  "size": 0

  , "query": {
    "terms": {
      "_id": [2,1,3]
    }
  }

  , "aggs": {
    "genres_for_user": {
      "terms": {
        "field": "genre",
        "size": 50
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您的文档结构看起来不错。

确保您没有为流派字段分析索引。如果您为此字段保留默认分析器,则“经典喜剧”之类的值将被索引为两个关键字,并且在聚合查询中将生成两个计数器,分别为经典喜剧