将数组与 ELASTICSEARCH 中的数组字段匹配
Match array with array filed in ELASTICSEARCH
我在存储目录的 ES 中有一个索引。文档是字符串和数组类型的混合(示例如下所示):
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N1234",
"_score" : 1.0,
"_source" : {
"product_id" : "1234",
"name" : "Multifunctional Soapbox Hanging",
"model" : "HS-11",
"image" : "catalog/1.jpg",
"size" : [
"43",
"44",
"45"
],
"color" : [
"66",
"67"
],
"tag" : [
"Soap Dish Rack Bathroom Essentials",
"Plastic Bathroom Essentials",
"Adhesive Bathroom Essentials",
"Smart Accessories Bathroom Essentials",
"Any Bathroom Essentials",
"Brown Bathroom Essentials",
"Bathroom Accessories Bathroom Essentials",
"Bathroom Essentials"
],
"language_id" : "1",
"status" : "7",
"date_added" : "2021-02-06 14:29:37"
}
}
所有文档都是相同的结构。我想根据 Color
和 Tag
字段查询类似产品的索引。意思是,如果我通过 color = 66
和 Tag = [ARRAY_OF_VALUES]
,它应该 return 我最相似的产品按分数排序,可能包含相同的颜色或最大匹配的标签。
我尝试 BOOL
使用 Should
或 Must
进行查询,但没有更好的结果。
我不知道如何查询数组,因为颜色和标签都是数组。我应该将 TAGS 参数作为数组或单独的值传递并进行单独的 match
查询吗?
我试过的查询(@ESCoder):
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
}
],
"should": [
{
"terms": {
"tag": [
"Casual Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Blue Dresses"
]
}
}
]
}
}
预期结果:
包含相同 color
和最大相似度 tag
的所有产品,按得分排序
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624121",
"_score" : 39.43454,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62412",
"name" : "Girls Short Sleeve Fashion Short Dress - Blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624151",
"_score" : 39.046432,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62415",
"name" : "Girls Short Sleeve Fashion Short Dress - Sky Blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Sleeves Dresses",
"Slip Dress Dresses",
"Sky Blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624111",
"_score" : 38.569298,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62411",
"name" : "Girls Short Sleeve Fashion Short Dress - Navy blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Slip Dress Dresses",
"Black Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624131",
"_score" : 37.646904,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62413",
"name" : "Girls Short Sleeve Fashion Short Dress - light blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"light blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624141",
"_score" : 37.02933,
"_source" : {
"color" : [
"233"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62414",
"name" : "Girls Short Sleeve Fashion Short Dress - Wine",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Wine Red Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624161",
"_score" : 36.39569,
"_source" : {
"color" : [
"84"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62416",
"name" : "Girls Short Sleeve Fashion Short Dress - Rose",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
}
您可以使用 terms query 搜索多个值。
术语查询 returns 文档在提供的字段中具有确切的术语(此处 tag
)。
如果您没有明确定义任何映射,则需要将 .keyword 添加到 tag
字段。这使用关键字分析器而不是标准分析器(注意 tag
字段后的“.keyword”)。
试试下面的查询
{
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
},
{
"terms": {
"tag.keyword": [
"Casual Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Blue Dresses"
]
}
}
]
}
}
}
搜索结果将包含得分为 2.0 的所有文档
但是如果你想根据分数排序,那么你需要使用 bool/should
子句和 term
查询
{
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
}
],
"should": [
{
"term": {
"tag.keyword": "Casual Dresses"
}
},
{
"term": {
"tag.keyword": "Round Neck Dresses"
}
},
{
"term": {
"tag.keyword": "Short Sleeves Dresses"
}
},
{
"term": {
"tag.keyword": "Blue Dresses"
}
}
],
"minimum_should_match": 1
}
}
}
搜索结果将是
"hits": [
{
"_index": "67148091",
"_type": "_doc",
"_id": "1",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62412",
"name": "Girls Short Sleeve Fashion Short Dress - Blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "2",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62415",
"name": "Girls Short Sleeve Fashion Short Dress - Sky Blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Sleeves Dresses",
"Slip Dress Dresses",
"Sky Blue Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "3",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62411",
"name": "Girls Short Sleeve Fashion Short Dress - Navy blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Slip Dress Dresses",
"Black Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "4",
"_score": 1.1101605,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62413",
"name": "Girls Short Sleeve Fashion Short Dress - light blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"light blue Dresses"
]
}
}
]
我在存储目录的 ES 中有一个索引。文档是字符串和数组类型的混合(示例如下所示):
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N1234",
"_score" : 1.0,
"_source" : {
"product_id" : "1234",
"name" : "Multifunctional Soapbox Hanging",
"model" : "HS-11",
"image" : "catalog/1.jpg",
"size" : [
"43",
"44",
"45"
],
"color" : [
"66",
"67"
],
"tag" : [
"Soap Dish Rack Bathroom Essentials",
"Plastic Bathroom Essentials",
"Adhesive Bathroom Essentials",
"Smart Accessories Bathroom Essentials",
"Any Bathroom Essentials",
"Brown Bathroom Essentials",
"Bathroom Accessories Bathroom Essentials",
"Bathroom Essentials"
],
"language_id" : "1",
"status" : "7",
"date_added" : "2021-02-06 14:29:37"
}
}
所有文档都是相同的结构。我想根据 Color
和 Tag
字段查询类似产品的索引。意思是,如果我通过 color = 66
和 Tag = [ARRAY_OF_VALUES]
,它应该 return 我最相似的产品按分数排序,可能包含相同的颜色或最大匹配的标签。
我尝试 BOOL
使用 Should
或 Must
进行查询,但没有更好的结果。
我不知道如何查询数组,因为颜色和标签都是数组。我应该将 TAGS 参数作为数组或单独的值传递并进行单独的 match
查询吗?
我试过的查询(@ESCoder):
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
}
],
"should": [
{
"terms": {
"tag": [
"Casual Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Blue Dresses"
]
}
}
]
}
}
预期结果:
包含相同 color
和最大相似度 tag
的所有产品,按得分排序
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624121",
"_score" : 39.43454,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62412",
"name" : "Girls Short Sleeve Fashion Short Dress - Blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624151",
"_score" : 39.046432,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62415",
"name" : "Girls Short Sleeve Fashion Short Dress - Sky Blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Sleeves Dresses",
"Slip Dress Dresses",
"Sky Blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624111",
"_score" : 38.569298,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62411",
"name" : "Girls Short Sleeve Fashion Short Dress - Navy blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Slip Dress Dresses",
"Black Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624131",
"_score" : 37.646904,
"_source" : {
"color" : [
"40"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62413",
"name" : "Girls Short Sleeve Fashion Short Dress - light blue",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"light blue Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624141",
"_score" : 37.02933,
"_source" : {
"color" : [
"233"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62414",
"name" : "Girls Short Sleeve Fashion Short Dress - Wine",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Wine Red Dresses"
]
}
},
{
"_index" : "catalog",
"_type" : "_doc",
"_id" : "N624161",
"_score" : 36.39569,
"_source" : {
"color" : [
"84"
],
"size" : [
"47",
"48",
"50",
"51"
],
"product_id" : "62416",
"name" : "Girls Short Sleeve Fashion Short Dress - Rose",
"model" : "LYQ001",
"tag" : [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
}
您可以使用 terms query 搜索多个值。
术语查询 returns 文档在提供的字段中具有确切的术语(此处 tag
)。
如果您没有明确定义任何映射,则需要将 .keyword 添加到 tag
字段。这使用关键字分析器而不是标准分析器(注意 tag
字段后的“.keyword”)。
试试下面的查询
{
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
},
{
"terms": {
"tag.keyword": [
"Casual Dresses",
"Round Neck Dresses",
"Short Sleeves Dresses",
"Blue Dresses"
]
}
}
]
}
}
}
搜索结果将包含得分为 2.0 的所有文档
但是如果你想根据分数排序,那么你需要使用 bool/should
子句和 term
查询
{
"query": {
"bool": {
"must": [
{
"terms": {
"color": [
"40" //code of blue color
]
}
}
],
"should": [
{
"term": {
"tag.keyword": "Casual Dresses"
}
},
{
"term": {
"tag.keyword": "Round Neck Dresses"
}
},
{
"term": {
"tag.keyword": "Short Sleeves Dresses"
}
},
{
"term": {
"tag.keyword": "Blue Dresses"
}
}
],
"minimum_should_match": 1
}
}
}
搜索结果将是
"hits": [
{
"_index": "67148091",
"_type": "_doc",
"_id": "1",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62412",
"name": "Girls Short Sleeve Fashion Short Dress - Blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"Blue Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "2",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62415",
"name": "Girls Short Sleeve Fashion Short Dress - Sky Blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Sleeves Dresses",
"Slip Dress Dresses",
"Sky Blue Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "3",
"_score": 2.640676,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62411",
"name": "Girls Short Sleeve Fashion Short Dress - Navy blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Round Neck Dresses",
"Slip Dress Dresses",
"Black Dresses"
]
}
},
{
"_index": "67148091",
"_type": "_doc",
"_id": "4",
"_score": 1.1101605,
"_source": {
"color": [
"40"
],
"size": [
"47",
"48",
"50",
"51"
],
"product_id": "62413",
"name": "Girls Short Sleeve Fashion Short Dress - light blue",
"model": "LYQ001",
"tag": [
"Casual Dresses",
"Body Fitted Dresses",
"Short Gown Dresses",
"Slip Dress Dresses",
"light blue Dresses"
]
}
}
]