正如 Tom83 和链接的 elasticsearch 权威指南所建议的,这可以通过在索引时添加 authors_counts 属性来完成。像这样:
curl -XPOST "${HOST}/article/1" -d '{
"authors": [
"Albert",
"Wolfgang",
"Richard",
"Murray"
],
"authors_counts": 4,
"message": "Blabla"
}'
curl -XPOST "${HOST}/article/2" -d '{
"authors": [
"Albert",
"Richard"
],
"authors_counts": 2,
"message": "Blublu"
}'
curl -XPOST "${HOST}/article/3" -d '{
"authors": [
"Albert"
],
"authors_counts": 1,
"message": "Bleble"
}'
然后,您可以按照指南中的建议创建查询。它非常冗长,所以我决定生成它。
(ns query-gen
(:require [clojure.data.json :as json]
[clojure.math.combinatorics :refer [subsets]]))
(defn gen-filter [items]
(let [terms (map (fn [term] { "term" { "authors" term } }) items)
terms_count { "term" { "authors_counts" (count items) }}]
{ "bool" { "must" (cons terms_count terms)}}))
(defn gen-query [names]
(let [subsets (rest (subsets names))
filters (map gen-filter subsets)]
{"query" { "filtered" { "filter" { "or" filters }}}}))
(defn -main [& args]
(let [ query (gen-query ["Albert" "Richard" "Erwin"])
json (json/write-str query)]
(println json)))
这会产生如下所示的查询:
{
"query": {
"filtered": {
"filter": {
"or": [
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Albert"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Richard"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Richard"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Richard"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 3
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Richard"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
}
]
}
}
}
}
如果像这样使用:
curl -XGET "${HOST}/_search?pretty=true" -d '{
"query": {
"filtered": {
"filter": {
"or": [
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Albert"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Richard"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 1
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Richard"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 2
}
},
{
"term": {
"authors": "Richard"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"authors_counts": 3
}
},
{
"term": {
"authors": "Albert"
}
},
{
"term": {
"authors": "Richard"
}
},
{
"term": {
"authors": "Erwin"
}
}
]
}
}
]
}
}
}
}'
它返回我认为你期望的结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [
{
"_index": "articles",
"_type": "article",
"_id": "3",
"_score": 1.0,
"_source": {
"authors": [
"Albert"
],
"authors_counts": 1,
"message": "Bleble"
}
},
{
"_index": "articles",
"_type": "article",
"_id": "2",
"_score": 1.0,
"_source": {
"authors": [
"Albert",
"Richard"
],
"authors_counts": 2,
"message": "Blublu"
}
}
]
}
}
我不确定这是否是个好主意,但很有趣。希望对您有所帮助!