Elasticsearch过滤器仅显示具有列表值的文档 [英] Elasticsearch filter to show only documents with values from list

查看:82
本文介绍了Elasticsearch过滤器仅显示具有列表值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下脚本,我设置elasticsearch来索引文章。每篇文章都可以有一个作者列表:

With the following script I setup elasticsearch to index articles. Each article can have a list of authors:

#!/usr/bin/env bash 
HOST='http://localhost:9200/articles'

curl -XPUT ${HOST} 
curl -XPUT "${HOST}/article/_mapping" -d '{
    "article" : {
        "properties" : {
            "authors" : {"type":"string", "index_name":"author", "index": "not_analyzed"}
        }
    } 
}' 
curl -XPOST  "${HOST}/article/1"  -d '{
    "authors" : ["Albert","Wolfgang","Richard","Murray"],
    "message" : "Blabla" }' 
curl -XPOST  "${HOST}/article/2"  -d '{
    "authors" : ["Albert","Richard"],
    "message" : "Blublu" }' 
curl -XPOST  "${HOST}/article/3"  -d '{
    "authors" : ["Albert"],
    "message" : "Bleble" }'

我想做的是过滤掉不在给定列表中的所有作者文章。我尝试了以下查询:

What I want to do is to filter out all the articles with authors which are not in a given list. I tried the following query:

curl -XGET  "${HOST}/_search?pretty=true" -d '{
    "query": {
        "constant_score": {
            "filter": {
                "terms": { 
                    "authors": ["Albert","Richard","Erwin"],
                    "execution": "or" 
                }            
            }
        }
    } 
}'

但是,这将返回所有三篇文章作为匹配。但是我确实想过滤掉第1条,因为它有一些作者 [ Wolfgang, Murray] ,但不在给定作者<$ c的列表中$ c> [ Albert, Richard, Erwin] 。

However this will return all three articles as hits. But I do want to filter out article 1, because it has some authors ["Wolfgang", "Murray"], which are not in the list of given authors ["Albert", "Richard","Erwin"]. Can this somehow be achieved with elasticsearch?

推荐答案

如Tom83所建议,并且在Elasticsearch权威指南中,可以通过添加以下内容来实现: authors_counts 属性在索引时间。像这样:

As suggested by Tom83 and in the elasticsearch definitive guide linked this can be done by adding a authors_counts property at index time. Like so:

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"
}'

然后您可以按照指南中的建议创建查询。它非常冗长,因此我决定生成它。

You can then create the query as suggested in the guide. It is pretty verbose, so I decided to generate it.

(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"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}'

返回结果我想您应该这样:

It returns the results I think you expect:

{
  "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"
        }
      }
    ]
  }
}

我不确定这是否是个好主意,但是很有趣。希望对您有所帮助!

I'm not sure if this is a good idea, but was quite fun. Hope it helps!

这篇关于Elasticsearch过滤器仅显示具有列表值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆