ElasticSearch:范围以上字段的搜索计数

siotufzp  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(183)

我正在使用ElasticSearch作为我的搜索引擎,我需要搜索特定数组字段中images计数大于2的文档。我想知道具有此条件的图书计数,并使用另一个查询返回符合此条件的所有图书ID。对于下面的示例,我希望计数为2,图书ID为:1234和1235我尝试查看文档,但没有成功。

{
   "_index":"index_book_1",
   "_type":"_doc",
   "_id":"1234",
   "_score":9.987,
   "_source":{
      "title":"Book Name 1",
      "readers":18,
      "images":[
         "s-l640.png",
         "s-l641.png",
         "s-l642.png"
      ],
     "category":"Baby"
   }
 }

 {
   "_index":"index_book_1",
   "_type":"_doc",
   "_id":"1235",
   "_score":9.987,
   "_source":{
      "title":"Book Name 2",
      "readers":20,
      "images":[
         "s-l640.png",
         "s-l641.png"
      ],
     "category":"Baby"
   }
 }

 {
   "_index":"index_book_1",
   "_type":"_doc",
   "_id":"1232",
   "_score":9.987,
   "_source":{
      "title":"Book Name 1",
      "readers":18,
      "images":[
         "s-l640.png"
      ],
     "category":"Baby"
   }
}
aoyhnmkz

aoyhnmkz1#

如果您希望文档在“images”数组中包含2个以上的项,则可以使用下面的查询(image必须是关键字类型)

{
  "query": {
    "script": {
      "script": "doc['images.keyword'].size() >= 2"
    }
  }
}

相关问题