默认情况下,条款汇总为我提供了前10个最受欢迎的字词及其计数,然后是sum_other_doc_count字段,表示"其他"项目
我可以将这些显示给用户:
first (150)
second (122)
third(111)
...
other(19)
...用户可以通过选择其中一个术语来过滤他们的结果。我使用他们选择的术语应用TermFilter。工作正常。
...但是.......有没有办法可以创建一个代表"其他" (即除了前10名之外的所有术语)?
答案 0 :(得分:2)
我不这么认为。但是,您可以将与terms和not过滤器相关(但不完全相同)的内容混合在一起,这会返回所有未显示最高项的文档。为了简单起见,我将使用前5名。
所以我创建了一个索引并添加了一些随机的拉丁文:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc"}}
{"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec rhoncus dictum ligula, quis volutpat diam fringilla ut."}
{"index":{"_index":"test_index","_type":"doc"}}
{"text": "Nulla ac gravida ipsum. Pellentesque placerat mattis pharetra. Praesent sapien lorem, auctor in imperdiet vel, lacinia vel diam."}
{"index":{"_index":"test_index","_type":"doc"}}
{"text": "Mauris a risus ut eros posuere rutrum. Nunc scelerisque diam ex, consequat mollis sem facilisis in."}
{"index":{"_index":"test_index","_type":"doc"}}
{"text": "Maecenas lacinia sollicitudin ultricies. Aenean id eleifend sapien. In et justo accumsan, cursus mi vel, consectetur augue. Nullam in quam ac magna iaculis finibus quis ut risus."}
{"index":{"_index":"test_index","_type":"doc"}}
{"text": "Donec dolor eros, rhoncus ultricies quam et, dapibus egestas libero."}
然后得到前5个术语:
POST /test_index/_search?search_type=count
{
"aggs": {
"top_terms":{
"terms":{
"field": "text",
"size": 5
}
}
}
}
...
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"top_terms": {
"buckets": [
{
"key": "diam",
"doc_count": 3
},
{
"key": "in",
"doc_count": 3
},
{
"key": "ut",
"doc_count": 3
},
{
"key": "ac",
"doc_count": 2
},
{
"key": "consectetur",
"doc_count": 2
}
]
}
}
}
然后我可以构建一个过滤器,让我回到前五个术语不出现的文档,例如:
POST /test_index/_search
{
"query": {
"constant_score": {
"filter": {
"not": {
"filter": {
"terms": {
"text": [
"diam",
"in",
"ut",
"ac",
"consectetur"
]
}
}
}
},
"boost": 1.2
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "4uoLr70rRXulHHc7N3Ujmw",
"_score": 1,
"_source": {
"text": "Donec dolor eros, rhoncus ultricies quam et, dapibus egestas libero."
}
}
]
}
}
我知道这并没有真正回答你的问题,但也许会给你一些想法。
以下是我使用的代码(如果您使用ES 1.4,您必须打开CORS才能在浏览器中使用该代码):
http://sense.qbox.io/gist/93b69375c5491f1b0458e2053a08e65006f34a1c