我正在尝试使用Elasticsearch在我的网站上创建一个包含建议的搜索。 我有一个“标题”字段,以这种方式设置映射。
"mappings" : {
"products" : {
"properties" : {
"title": {
"type" : "completion",
"analyzer": "simple",
"search_analyzer": "standard"
}
}
}
}
这是我要使用的搜索查询。
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [ "title^2", "description"],
"query": "cuboard",
"fuzziness": "AUTO"
}
}]
}
}
当我运行它时会返回错误。 我使用分析器来获取建议和它运作良好。我不能在Elasticsearch中用于搜索的分析字段中使用模糊性吗?
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"multi_match\" : {\n \"query\" : \"cuboard\",\n \"fields\" : [\n \"discrip^1.0\",\n \"title^2.0\"\n ],\n \"type\" : \"best_fields\",\n \"operator\" : \"OR\",\n \"slop\" : 0,\n \"fuzziness\" : \"AUTO\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n }\n ],\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}",
"index_uuid": "VIpm20b0Rra0PI1fyKCsKQ",
"index": "offers"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "products",
"node": "gTaPvDb2QXyR3VdK0gX3qg",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"bool\" : {\n \"must\" : [\n {\n \"multi_match\" : {\n \"query\" : \"cuboard\",\n \"fields\" : [\n \"discrip^1.0\",\n \"title^2.0\"\n ],\n \"type\" : \"best_fields\",\n \"operator\" : \"OR\",\n \"slop\" : 0,\n \"fuzziness\" : \"AUTO\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"fuzzy_transpositions\" : true,\n \"boost\" : 1.0\n }\n }\n ],\n \"adjust_pure_negative\" : true,\n \"boost\" : 1.0\n }\n}",
"index_uuid": "VIpm20b0Rra0PI1fyKCsKQ",
"index": "offers",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Can only use fuzzy queries on keyword and text fields - not on [title] which is of type [completion]"
}
}
}
]
},
"status": 400
}
答案 0 :(得分:1)
你去了,你可以找到错误的原因:
Can only use fuzzy queries on keyword and text fields - not on [title] which is of type [completion]
有fuzzy queries on completion fields的方法,但你需要采取不同的方式,例如:
POST products/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [
"description"
],
"query": "cuboard",
"fuzziness": "AUTO"
}
}
]
}
},
"suggest": {
"offer-suggest": {
"prefix": "cuboard",
"completion": {
"field": "title",
"fuzzy": {
"fuzziness": "AUTO"
}
}
}
}
}
<强>更新强>:
如果您确实希望将title
和description
字段合并到同一个查询中,则需要将映射更改为:
{
"mappings": {
"products": {
"properties": {
"title": {
"type": "completion",
"analyzer": "simple",
"search_analyzer": "standard",
"fields": {
"title_analyzed": {
"type": "text",
"analyzer": "simple",
"search_analyzer": "standard"
}
}
}
}
}
}
然后您可以将查询修改为:
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [ "title.title_analyzed^2", "description"],
"query": "cuboard",
"fuzziness": "AUTO"
}
}]
}
}