弹性面5.5

时间:2017-07-09 14:23:47

标签: database elasticsearch facet

我正在尝试用弹性的5.5构建一个有问题的搜索(有弹性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
  }
]

谢谢!

2 个答案:

答案 0 :(得分:1)

你只需要使用aggs而不是facets。聚合比方面更强大。

例如:

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

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

答案 1 :(得分:0)

如果列是文本,则需要使用" .keyword"也可以处理有很多单词的国名的情况("萨尔瓦多","美利坚合众国"等)

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