簡體   English   中英

如何為ElasticSearch創建復雜的搜索查詢?

[英]How to Create complex search Query for ElasticSearch?

我已經使用以下映射創建了ElasticSearch索引。 我需要編寫復雜搜索查詢,例如在查詢結果中查找相關項。 例如,找到價格為“ 100”的商品。 從搜索結果中獲取ItemBrand,然后查找具有相同ItemBrand。(相似產品)的其他項目。 誰能幫我嗎?

PUT myindex-solvisoft2
{
"mappings": {
"ItemCategory":{
"properties": {Category":{"type": "text"},"IsActive":{"type": "boolean"},"Comment":{"type": "text" }}
},

"ItemBrand": {
"properties": {"Creator": {"type": "keyword"},"DateEffective": {"type": "date"},"DateTerminated": {"type": "date"},"Description": {"type": "text"},
"IsActive": {"type": "boolean"},"ItemBrand": {"type": "keyword"}}
},

"Item": {
"properties": {
"Comment": {"type": "text"},"Creator": {"type": "keyword"},"DateEffective": {"type": "date"},
"DateTerminated": {"type": "date"},"IsActive": {"type": "boolean"},"ItemBrand": {"type": "keyword"   },
"ItemDescription": { "type": "text"  },"ItemCategory": {"type": "keyword"},"ItemId": {"type": "integer" },
"Price": {"type": "double"  }    }
}
}
}

在不運行查詢的情況下編寫好查詢將非常具有挑戰性,但是我可以分解步驟。

使用布爾值must找出價格等於100的記錄。

之后,您需要過濾查詢並使用字段來搜索具有相同商品品牌的商品。

沒有那么復雜。

這是我針對各個問題的解決方案。如有需要,它可能對初學者有所幫助。

GET myindex-solvisoft_similar_products/_search
{
"query": {
"bool": {
"filter": {
"term": {
"brand": "Microsoft"
}
}
}
},
"aggs": {
"colors": {
"terms": {
"field": "color"
}
},
"color_red": {
"filter": {
"term": {
"color": "red"
}
},
"aggs": {
"models": {
"terms": {
"field": "model"
}
}
}
}
},
"post_filter": {
"term": {
"color": "red"
}
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM