我正在查询弹性(v6.7(,以了解与以下查询相匹配的" x射线"的项目:
POST item/_search
{
"query": {
"bool": {
"must": {
"multi_match": {
"type": "phrase_prefix",
"query": "X-Ray",
"fields": [
"mpn",
"product_description"
"manufacturer_name"
],
"operator": "and",
"analyzer": "standard"
}
}
}
}
}
结果集为空。
我有包含" X射线"短语的项目文档。例如,如果我查询:
GET items/_doc/3e4a2d80-9d5e-11e7-a6c5-6ddf18575461
它返回:
{
"_index": "items",
"_type": "_doc",
"_id": "3e4a2d80-9d5e-11e7-a6c5-6ddf18575461",
"_version": 1,
"_seq_no": 7605,
"_primary_term": 1,
"found": true,
"_source": {
"manufacturer_name": "GE",
"var_pricing": 0,
"on_hand": 1,
...
"product_description": "Portable X-Ray w/Fuji CR Reader", <----This should be a match!
"project_id": null,
"user_id": "12",
"quote_items": [],
"parentCategory": [
0
]
}
}
如果我在刚安装的弹性版本(v7.3(上运行查询,我添加了三个文档,例如:
POST product/_bulk
{"index":{"_id":1001}}
{"name":"x-ray Machine","price":152000,"in_stock":38,"sold":47,"tags":["Alcohol","Wine"],"description":"x-ray machine for x-rays","is_active":true,"created":"2004/05/13"}
{"index":{"_id":1002}}
{"name":"X-Ray film","price":99,"in_stock":10,"sold":430,"tags":[],"description":"just some x-ray film","is_active":true,"created":"2007/10/14"}
{"index":{"_id":1003}}
{"name":"Table","price":2500,"in_stock":24,"sold":215,"tags":[],"description":"could be used for an x-ray table","is_active":true,"created":"2000/11/17"}
然后查询:
POST product/_search
{
"query": {
"bool": {
"must": {
"multi_match": {
"type": "phrase_prefix",
"query": "X-Ray",
"fields": [
"name",
"description"
],
"operator": "and",
"analyzer": "standard"
}
}
}
}
}
返回所有三个项目:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 31.876595,
"hits" : [
{
"_index" : "product",
"_type" : "default",
"_id" : "1001",
"_score" : 31.876595,
"_source" : {
"name" : "x-ray Machine",
"price" : 152000,
"in_stock" : 38,
"sold" : 47,
"tags" : [
"Alcohol",
"Wine"
],
"description" : "x-ray machine for x-rays",
"is_active" : true,
"created" : "2004/05/13"
}
},
{
"_index" : "product",
"_type" : "default",
"_id" : "1002",
"_score" : 27.347116,
"_source" : {
"name" : "X-Ray film",
"price" : 99,
"in_stock" : 10,
"sold" : 430,
"tags" : [ ],
"description" : "just some x-ray film",
"is_active" : true,
"created" : "2007/10/14"
}
},
{
"_index" : "product",
"_type" : "default",
"_id" : "1003",
"_score" : 25.889376,
"_source" : {
"name" : "Table",
"price" : 2500,
"in_stock" : 24,
"sold" : 215,
"tags" : [ ],
"description" : "could be used for an x-ray table",
"is_active" : true,
"created" : "2000/11/17"
}
}
]
}
}
什么给了?
我使用解释API获得了更多的见解,但它说的只是没有匹配:
POST items/_doc/3e4a2d80-9d5e-11e7-a6c5-6ddf18575461/_explain
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"type": "phrase_prefix",
"query": "X-Ray",
"fields": [
"product_description",
"mpn",
"manufacturer_name"
],
"operator": "and",
"analyzer": "standard"
}}
]
}
}
}
}
返回:
{
"_index": "items",
"_type": "_doc",
"_id": "3e4a2d80-9d5e-11e7-a6c5-6ddf18575461",
"matched": false,
"explanation": {
"value": 0,
"description": "Failure to meet condition(s) of required/prohibited clause(s)",
"details": [
{
"value": 0,
"description": "no match on required clause (((+product_description:x +product_description:ray) | (+mpn:x +mpn:ray) | (+manufacturer_name:x +manufacturer_name:ray)))",
"details": [
{
"value": 0,
"description": "No matching clause",
"details": []
}
]
},
{
"value": 0,
"description": "no match on required clause (MatchNoDocsQuery("Type list does not contain the index type"))",
"details": [
{
"value": 0,
"description": "MatchNoDocsQuery("Type list does not contain the index type") doesn't match id 12556",
"details": []
}
]
}
]
}
}
当我将分析仪更改为Whitespace或关键字时,不会更改。
(这不是答案,但我无法在评论中输入所有内容(
如果您打算整体上匹配X-Ray
,我真的不确定您是否真的需要在查询中使用analyzer
。
看这个
POST _analyze
{
"analyzer": "standard",
"text":"X-Ray"
}
响应是
{
"tokens" : [
{
"token" : "x",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "ray",
"start_offset" : 2,
"end_offset" : 5,
"type" : "<ALPHANUM>",
"position" : 1
}
]
}
因此,您的搜索词X-Ray
变成了x
和ray
。这是您的意图吗?
,所以我确定我的问题是一直应用标准分析器,因为它是在映射中设置的,以使用自定义分析仪(使用标准分析仪(。
在此处显示:
GET items/_mapping
显示
...
"manufacturer_name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
}
},
"analyzer": "my_search_analyzer",
"search_analyzer": "standard"
},
...
这对于我要查询的其他两个索引字段是相同的。
这里的课程:
检查映射以确保如果您有搜索问题,请确保某些字段设置自定义分析仪。