为什么使用term query查询elasticsearch时,搜索结果会有差异?
Why is there a difference in the search results when querying elasticsearch using a term query?
我最近开始学习 elasticsearch,我的查询的搜索结果有所不同。下面提供了名为“products”的索引的映射(我正在粘贴来自我的 Kibana 控制台工具的响应):
{
"products" : {
"mappings" : {
"properties" : {
"in_stock" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"price" : {
"type" : "long"
},
"tags" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
索引中的数据如下(我粘贴的是我的Kibana控制台工具的响应):
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "products",
"_type" : "_doc",
"_id" : "202",
"_score" : 1.0,
"_source" : {
"name" : "Vegetable Chopper",
"price" : 10,
"in_stock" : 250,
"tags" : [
"kitchen appliances",
"vegetable slicer",
"chopper"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "203",
"_score" : 1.0,
"_source" : {
"name" : "Dish Washer",
"price" : 90,
"in_stock" : 60,
"tags" : [
"kitchen appliances",
"electrical",
"electric washer"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "205",
"_score" : 1.0,
"_source" : {
"name" : "Microwave Oven",
"price" : 100,
"in_stock" : 50,
"tags" : [
"kitchen appliances",
"electricals",
"oven",
"oven toaster",
"microwave"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "206",
"_score" : 1.0,
"_source" : {
"name" : "Mixer Grinder",
"price" : 55,
"in_stock" : 130,
"tags" : [
"kitchen appliances",
"electricals",
"mixer",
"grinder",
"juicer",
"food processor"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "207",
"_score" : 1.0,
"_source" : {
"name" : "Fruit Juicer",
"price" : 40,
"in_stock" : 100,
"tags" : [
"kitchen appliances",
"electicals",
"juicer",
"mixer",
"electric juicer",
"juice maker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "208",
"_score" : 1.0,
"_source" : {
"name" : "Knife Set",
"price" : 15,
"in_stock" : 250,
"tags" : [
"kitchen knife",
"steel knives",
"cutlery"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "209",
"_score" : 1.0,
"_source" : {
"name" : "Rice Maker",
"price" : 85,
"in_stock" : 60,
"tags" : [
"kitchen appliances",
"electricals",
"electric rice cooker",
"electric pressure cooker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "210",
"_score" : 1.0,
"_source" : {
"name" : "Induction Cooktop",
"price" : 30,
"in_stock" : 150,
"tags" : [
"kitchen appliances",
"electricals",
"hot plate heater",
"electric hot place",
"induction cooker",
"induction stove"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "211",
"_score" : 1.0,
"_source" : {
"name" : "Coffee Maker",
"price" : 50,
"in_stock" : 100,
"tags" : [
"kitchen appliances",
"electricals"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "212",
"_score" : 1.0,
"_source" : {
"name" : "Wine Glasses Set",
"price" : 50,
"in_stock" : 70,
"tags" : [
"kitchen and dining",
"glassware",
"stemware"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "213",
"_score" : 1.0,
"_source" : {
"name" : "Dinner Set",
"price" : 100,
"in_stock" : 40,
"tags" : [
"kitchen and dining",
"crockery",
"full dinner set"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "214",
"_score" : 1.0,
"_source" : {
"name" : "Whiskey Glasses Set",
"price" : 60,
"in_stock" : 50,
"tags" : [
"kitchen and dining",
"glassware",
"whiskey glasses",
"old fashioned glass",
"rocks glass",
"short tumbler"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "215",
"_score" : 1.0,
"_source" : {
"name" : "Mug And Saucer Set",
"price" : 35,
"in_stock" : 60,
"tags" : [
"kitchen and dining",
"mug set",
"mugs and saucer",
"crockery set"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "201",
"_score" : 1.0,
"_source" : {
"name" : "Milk Frother",
"price" : 25,
"in_stock" : 15,
"tags" : [
"kitchen appliances",
"electricals",
"milk"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "200",
"_score" : 1.0,
"_source" : {
"name" : "Espresso Maker",
"price" : 180,
"in_stock" : 5,
"tags" : [
"kitchen appliances",
"electrical",
"coffee maker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "204",
"_score" : 1.0,
"_source" : {
"name" : "Pressure Fryer",
"price" : 120,
"in_stock" : 50,
"tags" : [
"air fryer",
"kitchen appliances",
"electric fryer",
"fryer",
"health fryer"
]
}
}
]
}
}
使用下面的查询查询数据后,我只匹配了六个记录:
查询 - 1
GET /products/_search
{
"query": {"terms" : {"tags": ["kitchen appliances","electricals"]}}
}
匹配的文档 ID 是 (201,205,206,209,210,211)
当我执行下面的查询时,我匹配了 11 条记录:
查询 2
GET /products/_search
{
"query": {"terms" : {"tags.keyword": ["kitchen appliances","electricals"]}}
}
与第二个查询匹配的文档 ID 是:(200,201,202,203,204,205,206,207,209,210,211)
有人可以解释这两个查询之间的区别是什么吗?为什么 Query-1 是 Query-2 的子集,即使两个查询都在同一字段上执行?
如果您有 text
类型字段,最好使用 match
查询。
term query 不对字词进行任何分析。它 returns 包含精确术语匹配文档的文档。
terms query 以精确的方式工作。它 returns 具有 1 个或多个确切术语的文档。
查询 1:
{
"query": {
"terms": {
"tags": [
"kitchen appliances",
"electricals"
]
}
}
}
搜索结果是
"hits": [
{
"_index": "67155973",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "Microwave Oven",
"price": 100,
"in_stock": 50,
"tags": [
"kitchen appliances",
"electricals",
"oven",
"oven toaster",
"microwave"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"name": "Mixer Grinder",
"price": 55,
"in_stock": 130,
"tags": [
"kitchen appliances",
"electricals",
"mixer",
"grinder",
"juicer",
"food processor"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "7",
"_score": 1.0,
"_source": {
"name": "Rice Maker",
"price": 85,
"in_stock": 60,
"tags": [
"kitchen appliances",
"electricals",
"electric rice cooker",
"electric pressure cooker"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "8",
"_score": 1.0,
"_source": {
"name": "Induction Cooktop",
"price": 30,
"in_stock": 150,
"tags": [
"kitchen appliances",
"electricals",
"hot plate heater",
"electric hot place",
"induction cooker",
"induction stove"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "9",
"_score": 1.0,
"_source": {
"name": "Coffee Maker",
"price": 50,
"in_stock": 100,
"tags": [
"kitchen appliances",
"electricals"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "14",
"_score": 1.0,
"_source": {
"name": "Milk Frother",
"price": 25,
"in_stock": 15,
"tags": [
"kitchen appliances",
"electricals",
"milk"
]
}
}
]
中所述
The term query does not analyze the search term. The term query only
searches for the exact term you provide. This means the term query may
return poor or no results when searching text fields.
查询 2:
{
"query": {
"terms": {
"tags.keyword": [
"kitchen appliances",
"electricals"
]
}
}
}
在上面的查询中,您使用的 tags.keyword
字段使用关键字分析器而不是标准分析器。此处查询搜索确切的术语,即 "kitchen appliances"
OR "electricals"
,因此 returns 11 个文档。
我最近开始学习 elasticsearch,我的查询的搜索结果有所不同。下面提供了名为“products”的索引的映射(我正在粘贴来自我的 Kibana 控制台工具的响应):
{
"products" : {
"mappings" : {
"properties" : {
"in_stock" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"price" : {
"type" : "long"
},
"tags" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
索引中的数据如下(我粘贴的是我的Kibana控制台工具的响应):
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 16,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "products",
"_type" : "_doc",
"_id" : "202",
"_score" : 1.0,
"_source" : {
"name" : "Vegetable Chopper",
"price" : 10,
"in_stock" : 250,
"tags" : [
"kitchen appliances",
"vegetable slicer",
"chopper"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "203",
"_score" : 1.0,
"_source" : {
"name" : "Dish Washer",
"price" : 90,
"in_stock" : 60,
"tags" : [
"kitchen appliances",
"electrical",
"electric washer"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "205",
"_score" : 1.0,
"_source" : {
"name" : "Microwave Oven",
"price" : 100,
"in_stock" : 50,
"tags" : [
"kitchen appliances",
"electricals",
"oven",
"oven toaster",
"microwave"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "206",
"_score" : 1.0,
"_source" : {
"name" : "Mixer Grinder",
"price" : 55,
"in_stock" : 130,
"tags" : [
"kitchen appliances",
"electricals",
"mixer",
"grinder",
"juicer",
"food processor"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "207",
"_score" : 1.0,
"_source" : {
"name" : "Fruit Juicer",
"price" : 40,
"in_stock" : 100,
"tags" : [
"kitchen appliances",
"electicals",
"juicer",
"mixer",
"electric juicer",
"juice maker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "208",
"_score" : 1.0,
"_source" : {
"name" : "Knife Set",
"price" : 15,
"in_stock" : 250,
"tags" : [
"kitchen knife",
"steel knives",
"cutlery"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "209",
"_score" : 1.0,
"_source" : {
"name" : "Rice Maker",
"price" : 85,
"in_stock" : 60,
"tags" : [
"kitchen appliances",
"electricals",
"electric rice cooker",
"electric pressure cooker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "210",
"_score" : 1.0,
"_source" : {
"name" : "Induction Cooktop",
"price" : 30,
"in_stock" : 150,
"tags" : [
"kitchen appliances",
"electricals",
"hot plate heater",
"electric hot place",
"induction cooker",
"induction stove"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "211",
"_score" : 1.0,
"_source" : {
"name" : "Coffee Maker",
"price" : 50,
"in_stock" : 100,
"tags" : [
"kitchen appliances",
"electricals"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "212",
"_score" : 1.0,
"_source" : {
"name" : "Wine Glasses Set",
"price" : 50,
"in_stock" : 70,
"tags" : [
"kitchen and dining",
"glassware",
"stemware"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "213",
"_score" : 1.0,
"_source" : {
"name" : "Dinner Set",
"price" : 100,
"in_stock" : 40,
"tags" : [
"kitchen and dining",
"crockery",
"full dinner set"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "214",
"_score" : 1.0,
"_source" : {
"name" : "Whiskey Glasses Set",
"price" : 60,
"in_stock" : 50,
"tags" : [
"kitchen and dining",
"glassware",
"whiskey glasses",
"old fashioned glass",
"rocks glass",
"short tumbler"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "215",
"_score" : 1.0,
"_source" : {
"name" : "Mug And Saucer Set",
"price" : 35,
"in_stock" : 60,
"tags" : [
"kitchen and dining",
"mug set",
"mugs and saucer",
"crockery set"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "201",
"_score" : 1.0,
"_source" : {
"name" : "Milk Frother",
"price" : 25,
"in_stock" : 15,
"tags" : [
"kitchen appliances",
"electricals",
"milk"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "200",
"_score" : 1.0,
"_source" : {
"name" : "Espresso Maker",
"price" : 180,
"in_stock" : 5,
"tags" : [
"kitchen appliances",
"electrical",
"coffee maker"
]
}
},
{
"_index" : "products",
"_type" : "_doc",
"_id" : "204",
"_score" : 1.0,
"_source" : {
"name" : "Pressure Fryer",
"price" : 120,
"in_stock" : 50,
"tags" : [
"air fryer",
"kitchen appliances",
"electric fryer",
"fryer",
"health fryer"
]
}
}
]
}
}
使用下面的查询查询数据后,我只匹配了六个记录: 查询 - 1
GET /products/_search
{
"query": {"terms" : {"tags": ["kitchen appliances","electricals"]}}
}
匹配的文档 ID 是 (201,205,206,209,210,211)
当我执行下面的查询时,我匹配了 11 条记录: 查询 2
GET /products/_search
{
"query": {"terms" : {"tags.keyword": ["kitchen appliances","electricals"]}}
}
与第二个查询匹配的文档 ID 是:(200,201,202,203,204,205,206,207,209,210,211)
有人可以解释这两个查询之间的区别是什么吗?为什么 Query-1 是 Query-2 的子集,即使两个查询都在同一字段上执行?
如果您有 text
类型字段,最好使用 match
查询。
term query 不对字词进行任何分析。它 returns 包含精确术语匹配文档的文档。
terms query 以精确的方式工作。它 returns 具有 1 个或多个确切术语的文档。
查询 1:
{
"query": {
"terms": {
"tags": [
"kitchen appliances",
"electricals"
]
}
}
}
搜索结果是
"hits": [
{
"_index": "67155973",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "Microwave Oven",
"price": 100,
"in_stock": 50,
"tags": [
"kitchen appliances",
"electricals",
"oven",
"oven toaster",
"microwave"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"name": "Mixer Grinder",
"price": 55,
"in_stock": 130,
"tags": [
"kitchen appliances",
"electricals",
"mixer",
"grinder",
"juicer",
"food processor"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "7",
"_score": 1.0,
"_source": {
"name": "Rice Maker",
"price": 85,
"in_stock": 60,
"tags": [
"kitchen appliances",
"electricals",
"electric rice cooker",
"electric pressure cooker"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "8",
"_score": 1.0,
"_source": {
"name": "Induction Cooktop",
"price": 30,
"in_stock": 150,
"tags": [
"kitchen appliances",
"electricals",
"hot plate heater",
"electric hot place",
"induction cooker",
"induction stove"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "9",
"_score": 1.0,
"_source": {
"name": "Coffee Maker",
"price": 50,
"in_stock": 100,
"tags": [
"kitchen appliances",
"electricals"
]
}
},
{
"_index": "67155973",
"_type": "_doc",
"_id": "14",
"_score": 1.0,
"_source": {
"name": "Milk Frother",
"price": 25,
"in_stock": 15,
"tags": [
"kitchen appliances",
"electricals",
"milk"
]
}
}
]
中所述
The term query does not analyze the search term. The term query only searches for the exact term you provide. This means the term query may return poor or no results when searching text fields.
查询 2:
{
"query": {
"terms": {
"tags.keyword": [
"kitchen appliances",
"electricals"
]
}
}
}
在上面的查询中,您使用的 tags.keyword
字段使用关键字分析器而不是标准分析器。此处查询搜索确切的术语,即 "kitchen appliances"
OR "electricals"
,因此 returns 11 个文档。