弹性 5.5 中的刻面

facets in elastic 5.5

我正在尝试使用 elastic 5.5 构建一个完美的搜索(使用 elastic 2。* 我找到了有关如何执行此操作的信息,但在 5.5 中我不能)

如果用户选择使用一些 属性 进行过滤,我想显示数量提示的不是异常的、类似商店的物品。 Image for visualisation

数据集示例:

[
  {
    "id" : "978-0641723445",
    "cat" : ["book","hardcover"],
    "name" : "The Lightning Thief",
    "author" : "Rick Riordan",
    "series_t" : "Percy Jackson and the Olympians",
    "sequence_i" : 1,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 12.50,
    "pages_i" : 384
  }
,
  {
    "id" : "978-1423103349",
    "cat" : ["book","paperback"],
    "name" : "The Sea of Monsters",
    "author" : "Rick Riordan",
    "series_t" : "Percy Jackson and the Olympians",
    "sequence_i" : 2,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 6.49,
    "pages_i" : 304
  }
,
  {
    "id" : "978-1857995879",
    "cat" : ["book","paperback"],
    "name" : "Sophie's World : The Greek Philosophers",
    "author" : "Jostein Gaarder",
    "sequence_i" : 1,
    "genre_s" : "fantasy",
    "inStock" : true,
    "price" : 3.07,
    "pages_i" : 64
  }
,
  {
    "id" : "978-1933988177",
    "cat" : ["book","paperback"],
    "name" : "Lucene in Action, Second Edition",
    "author" : "Michael McCandless",
    "sequence_i" : 1,
    "genre_s" : "IT",
    "inStock" : true,
    "price" : 30.50,
    "pages_i" : 475
  }
]

谢谢!

您只需要使用聚合而不是分面。聚合比分面更强大。

例如:

GET index/_search
{
    "aggs" : {
        "countries" : {
            "terms" : { "field" : "country" }
        },
        "types" : {
            "terms" : { "field" : "type" }
        }
    }
}

然后,如果用户单击 "facet",只需在 bool 查询中添加一个筛选子句(这将更新所有方面计数)或添加 post_filter 以仅筛选结果(不不影响聚合)。

如果列是文本,您也需要使用“.keyword”来处理国家名称中有很多词的情况("El Salvador"、"United States of America" 等)

GET index/_search
{
    "aggs" : {
        "countries" : {
            "terms" : { "field" : "country.keyword" }
        }
    }
}