![](/img/trans.png)
[英]How to get all documents where all objects in an array are of certain values in Elastic search
[英]Elastic match documents containing all values in array
我在 Elastic 中有類似下面這些文件的文件。 每個文檔都有一些信息和訪問文檔的所有強制權限。
當我查詢時,我想通過所有用戶權限並接收匹配的文檔,但在查詢時遇到困難。
文件:
{
"id": 1,
"permissions": ["a", "d"]
}
{
"id": 2,
"permissions": ["a"]
}
{
"id": 3,
"permissions": ["a", "b"]
}
{
"id": 4,
"permissions": ["a", "c"]
}
這是我得到的最接近的:
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"tags.keyword": "a"
}
},
{
"match_phrase": {
"tags.keyword": "b"
}
},
],
"minimum_should_match": doc.tags.length
}
}
}
// Result are documents with id's 2 and 3.
我嘗試使用"script"
擴展"minimum_should_match"
但沒有成功(顯然它不支持它):
"script" : {
"script" : {
"inline": "doc['permissions'].length",
"lang": "painless"
}
}
在上面的示例中,通過傳遞權限數組["a", "b", "c"]
,輸出應該是 id 為 2、3 和 4 的文檔。 ["a"]
僅匹配 id 為 2 的文檔。
編輯:附加信息一個文檔最多有 5 個權限,以及用戶,但權限集相當大(500+),所以我正在搜索一個通用查詢。 數據也可以轉換。
我正在使用 Elastic 7.6 任何幫助表示贊賞!
沒有達到預期結果的有效方法,但除了以下搜索查詢之外,您甚至可以使用腳本獲取結果,請參閱此SO 答案以了解這一點。
您可以使用術語查詢來返回在提供的字段中包含一個或多個確切術語的文檔。
添加一個包含索引數據(與問題中給出的相同)、索引映射、搜索查詢和搜索結果的工作示例。
索引映射:
{
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"permissions": {
"type": "text"
}
}
}
}
搜索查詢:
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"terms": {
"permissions": [
"a",
"b",
"c"
]
}
}
],
"must_not": [
{
"terms": {
"permissions": [
"d"
]
}
}
]
}
}
}
}
}
搜索結果:
"hits": [
{
"_index": "stof_64081578",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"id": 2,
"permissions": [
"a"
]
}
},
{
"_index": "stof_64081578",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"id": 3,
"permissions": [
"a",
"b"
]
}
},
{
"_index": "stof_64081578",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"id": 4,
"permissions": [
"a",
"c"
]
}
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.