[英]Elasticsearch search with nested properties
我有一個實例,其中存儲了具有各種屬性的文章。 但可能有些項目根本沒有屬性。 有無數的屬性和分配的值,所有這些都是隨機的。
現在的問題是,不幸的是它並沒有按照我想要的方式工作。 屬性受到尊重,但屬性的順序似乎很重要。 但可能是實例中的條目中有很多屬性,而在搜索查詢中只查詢了 1-2 個,並且這些值可能會有微小的偏差。
目標是找到盡可能相似的條目,無論屬性的順序如何。
誰能幫我這個?
彈性實例信息:
"_index" : "articles",
"_type" : "_doc",
"_id" : "fYjaQXkBBdCju4scstN_",
"_score" : 1.0,
"_source" : {
"position" : "400.000",
"beschreibung" : "asc",
"menge" : 24.0,
"einheit" : "St",
"properties" : [
{
"desc" : "Farbe",
"val" : "rot"
},
{
"desc" : "Material",
"val" : "Holz"
},
{
"desc" : "Länge",
"val" : "20 cm"
},
{
"desc" : "Breite",
"val" : "100 km"
}
]
}
}
我當前查詢的嵌套部分:
[nested] => Array
(
[path] => properties
[query] => Array
(
[0] => Array
(
[0] => Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[match] => Array
(
[properties.desc] => Farbe
)
)
[1] => Array
(
[match] => Array
(
[properties.val] => rot
)
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[match] => Array
(
[properties.desc] => Länge
)
)
[1] => Array
(
[match] => Array
(
[properties.val] => 22 cm
)
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[match] => Array
(
[properties.desc] => Material
)
)
[1] => Array
(
[match] => Array
(
[properties.val] => Holz
)
)
)
)
)
)
)
)
您的查詢中有兩個問題會導致奇怪的結果:
您正在對具有多個術語的文本字段使用匹配查詢。 所以當做一個
"match": { "properties.val": "22 cm", }
, Elasticsearch 在 properties.val 字段中搜索“22”或“cm”。 我假設你想匹配整個短語,所以你可以在這里使用match_phrase 。 或者,您可以將該單元放入自己的字段中。 另一種選擇是使用 operator 參數:
"match": { "properties.val": { "query": "20 cm", "operator": "and" } }
但請注意,這不是在尋找確切的短語。 例如“20 30 cm”也會匹配,但也許這可能適合您的情況。
您在屬性級別使用 should 子句。 因此,您基本上是在要求具有屬性的文檔,“應該在其描述中包含Farbe並在其價值中腐爛”,但這將與以下所有示例匹配:
"properties": [ { "desc": "Farbe", "val": "blau" } ]
"properties" : [
{
"desc" : "Material",
"val" : "rot"
}
]
"properties" : [
{
"desc" : "Farbe",
"val" : "blau"
},
{
"desc" : "Material",
"val" : "rot"
}
]
因此,您需要為您想要匹配的每個屬性提供一個bool查詢(具有must或filter子句),並為每個屬性提供一個should子句的bool查詢。 您對該問題的查詢可能是這樣的:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "properties",
"query": {
"bool": {
"filter": [
{
"match": {
"properties.desc": "Farbe"
}
}
],
"must": [
{
"match_phrase": {
"properties.val": "rot"
}
}
]
}
}
}
},
{
"nested": {
"path": "properties",
"query": {
"bool": {
"filter": [
{
"match": {
"properties.desc": "Länge"
}
}
],
"must": [
{
"match_phrase": {
"properties.val": "22 cm"
}
}
]
}
}
}
},
{
"nested": {
"path": "properties",
"query": {
"bool": {
"filter": [
{
"match": {
"properties.desc": "Material"
}
}
],
"must": [
{
"match_phrase": {
"properties.val": "Holz"
}
}
]
}
}
}
}
]
}
}
}
請嘗試,如果這給出了預期的結果。 您仍然可以調整查詢,例如使用minimum_should_match或定義每個匹配屬性給出的分數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.