我正在嘗試查詢 ElasticSearch 以匹配串列串列中包含所有請求值的每個檔案,但我似乎找不到完美的查詢。
映射:
"id" : {
"type" : "keyword"
},
"mainlist" : {
"properties" : {
"format" : {
"type" : "keyword"
},
"tags" : {
"type" : "keyword"
}
}
},
...
檔案:
doc1 {
"id" : "abc",
"mainlist" : [
{
"type" : "big",
"tags" : [
"tag1",
"tag2"
]
},
{
"type" : "small",
"tags" : [
"tag1"
]
}
]
},
doc2 {
"id" : "abc",
"mainlist" : [
{
"type" : "big",
"tags" : [
"tag1"
]
},
{
"type" : "small",
"tags" : [
"tag2"
]
}
]
},
doc3 {
"id" : "abc",
"mainlist" : [
{
"type" : "big",
"tags" : [
"tag1"
]
}
]
}
我嘗試過的最接近結果的查詢是:
GET /index/_doc/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"mainlist.tags": "tag1"
}
},
{
"term": {
"mainlist.tags": "tag2"
}
}
]
}
}
}
雖然我得到了結果doc1,doc2而我只希望doc1在單個串列元素中包含 tag1 和 tag2,而不是分布在兩個子串列中。
我怎么能做到這一點?謝謝你的幫助。
uj5u.com熱心網友回復:
正如@caster 所提到的,您需要以正常方式使用嵌套資料型別和查詢,Elasticsearch 將它們視為物件并且元素之間的關系丟失,如官方檔案中所述。
您需要更改映射和查詢以實作所需的輸出,如下所示。
索引映射
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"mainlist" :{
"type" : "nested"
}
}
}
}
根據您的示例,示例索引檔案,沒有變化
詢問
{
"query": {
"nested": {
"path": "mainlist",
"query": {
"bool": {
"must": [
{
"term": {
"mainlist.tags": "tag1"
}
},
{
"match": {
"mainlist.tags": "tag2"
}
}
]
}
}
}
}
}
結果
hits": [
{
"_index": "71519931_new",
"_id": "1",
"_score": 0.9139043,
"_source": {
"id": "abc",
"mainlist": [
{
"type": "big",
"tags": [
"tag1",
"tag2"
]
},
{
"type": "small",
"tags": [
"tag1"
]
}
]
}
}
]
uj5u.com熱心網友回復:
使用嵌套欄位型別,這是它的作業 https://www.elastic.co/guide/en/elasticsearch/reference/8.1/nested.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/446595.html
下一篇:彈性:輸入不匹配'-'
