我幾乎沒有包含陣列“items”的檔案,我只想選擇那些“items.name”等于“red”的檔案。如果有任何檔案具有一種紅色和另一種顏色,那么它不應該出現在結果中。
1.
{
"items": [
{
"id": "4",
"name": "red"
},
{
"id": "5",
"name": "blue"
}
]
}
{
"items": [
{
"id": "3",
"name": "red"
}
]
}
{
"items": [
{
"id": "2",
"name": "red"
},
{
"id": "1",
"name": "red"
}
]
}
現在,在這里我需要一個查詢,其中只有檔案 2 和 3 應該出現在結果中,因為所有“items.name”中都存在“紅色”。檔案 1 被忽略,因為它也包含藍色。
uj5u.com熱心網友回復:
這是我使用腳本的第一個解決方案:
GET test/_search
{
"runtime_mappings": {
"all_red_items": {
"type": "boolean",
"script": {
"source": "int count = 0; for (int i = 0; i < doc['items.name'].size(); i ) { if (doc['items.name'][i] != 'red') { count }} emit(count == 0);"
}
}
},
"query": {
"bool": {
"must": [
{
"term": {
"all_red_items": {
"value": true
}
}
}
]
}
}
}
這是我的正則運算式解決方案:
GET test/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"items.name": {
"value": "red"
}
}
}
],
"must_not": [
{
"regexp": {
"items.name": "@&~(red)"
}
}
]
}
}
}
在發送請求之前,您需要準備以下索引:
DELETE test
PUT test
{
"mappings": {
"properties": {
"items": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
}
POST test/_doc
{
"items": [
{
"id": 6,
"name": "red"
},
{
"id": 5,
"name": "blue"
}
]
}
POST test/_doc
{
"items": [
{
"id": 1,
"name": "red"
}
]
}
POST test/_doc
{
"items": [
{
"id": 3,
"name": "red"
},
{
"id": 4,
"name": "red"
}
]
}
GET test/_mapping
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476430.html
