【问题标题】:wildcard query on not analysed field returns no results未分析字段的通配符查询不返回任何结果
【发布时间】:2017-02-17 08:12:26
【问题描述】:

Elastic 1.5,在 AWS 上运行。我正在尝试运行以下查询:

GET /pictures_testing/_search
{
   "query": {
      "filtered": {
         "query": {
            "wildcard": {
               "aircraft_type_search.search": {
                  "wildcard": "piper"
               }
            }
         }
      }
   }
}

但它没有返回任何结果。但是我可以从飞机类型搜索返回结果,而不是飞机类型搜索。搜索映射

这是字段映射:

{
   "pictures_testing": {
      "mappings": {
         "picture": {
            "aircraft_type_search": {
               "full_name": "aircraft_type_search",
               "mapping": {
                  "aircraft_type_search": {
                     "type": "string",
                     "fields": {
                        "lower_case_sort": {
                           "type": "string",
                           "analyzer": "case_insensitive_sort"
                        },
                        "search": {
                           "type": "string",
                           "index": "not_analyzed"
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

我以相同的方式设置了其他字段,.search 映射将返回结果(包括通配符查询,这是最终点),所以我有点不知道为什么这个字段没有。

GET /pictures_testing/ 返回:

    {
   "pictures_testing": {
      "aliases": {},
      "mappings": {
         "picture": {
            "properties": {
               "ab_archive": {
                  "type": "string"
               },
               "ab_publication": {
                  "type": "string"
               },
               "aircraft_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "aircraft_type": {
                  "type": "string"
               },
               "aircraft_type_search": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "aircraft_verified": {
                  "type": "string"
               },
               "airport": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "airshow": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "airshow_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "airshow_verified": {
                  "type": "string"
               },
               "approved": {
                  "type": "integer",
                  "ignore_malformed": true
               },
               "category": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "code_number": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "construction_number": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "country": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "created_by": {
                  "type": "string"
               },
               "created_on": {
                  "type": "string"
               },
               "date_added": {
                  "type": "date",
                  "ignore_malformed": true,
                  "format": "yyyy-MM-dd HH:mm:ss"
               },
               "date_approved": {
                  "type": "date",
                  "ignore_malformed": true,
                  "format": "yyyy-MM-dd HH:mm:ss"
               },
               "date_taken": {
                  "type": "string"
               },
               "date_taken_formatted": {
                  "type": "date",
                  "ignore_malformed": true,
                  "format": "yyyy-MM-dd HH:mm:ss"
               },
               "display_picture": {
                  "type": "string"
               },
               "displayed_info": {
                  "type": "string"
               },
               "displayteam": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "displayteam_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "displayteam_verified": {
                  "type": "string"
               },
               "editors_choice": {
                  "type": "string"
               },
               "email": {
                  "type": "string"
               },
               "filename": {
                  "type": "string"
               },
               "full_size_folder": {
                  "type": "string"
               },
               "group": {
                  "type": "string"
               },
               "hidden_email": {
                  "type": "string"
               },
               "hidden_photographer": {
                  "type": "string"
               },
               "id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "information": {
                  "type": "string"
               },
               "initial": {
                  "type": "string"
               },
               "ip": {
                  "type": "string"
               },
               "location_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "location_verified": {
                  "type": "string"
               },
               "manufacturer": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "military_unit": {
                  "type": "string"
               },
               "model": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "modified_by": {
                  "type": "string"
               },
               "modified_on": {
                  "type": "string"
               },
               "name": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "operator": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "operator_category_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "operator_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "operator_type": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "operator_verified": {
                  "type": "string"
               },
               "original_filename": {
                  "type": "string"
               },
               "photographer": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "index": "not_analyzed",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "picture_category_id": {
                  "type": "integer",
                  "ignore_malformed": true,
                  "null_value": -1
               },
               "picture_id": {
                  "type": "string"
               },
               "picturecategory_verified": {
                  "type": "string"
               },
               "registration": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "reject_reason": {
                  "type": "string"
               },
               "site_name": {
                  "type": "string"
               },
               "slug": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "subtype": {
                  "type": "string"
               },
               "subtype2": {
                  "type": "string"
               },
               "type": {
                  "type": "string",
                  "fields": {
                     "lower_case_sort": {
                        "type": "string",
                        "analyzer": "case_insensitive_sort"
                     },
                     "search": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               },
               "uploaded_file_size": {
                  "type": "string"
               },
               "verified": {
                  "type": "string"
               },
               "views": {
                  "type": "integer",
                  "ignore_malformed": true
               },
               "website": {
                  "type": "string"
               },
               "year": {
                  "type": "integer",
                  "ignore_malformed": true
               }
            }
         }
      },
      "settings": {
         "index": {
            "creation_date": "1487318302366",
            "analysis": {
               "analyzer": {
                  "case_insensitive_sort": {
                     "filter": [
                        "lowercase"
                     ],
                     "tokenizer": "keyword"
                  }
               }
            },
            "number_of_shards": "1",
            "number_of_replicas": "1",
            "version": {
               "created": "1050299"
            },
            "uuid": "KoaR8mksTReY_2mRu4wi-Q"
         }
      },
      "warmers": {}
   }
}   

应该匹配的示例文档:

        {
        "_index": "pictures_testing",
        "_type": "picture",
        "_id": "1328215",
        "_score": 1,
        "_source": {
           "id": "1328215",
           "aircraft_id": "8947",
           "operator_id": "3273",
           "operator_category_id": "3",
           "location_id": "2877",
           "displayteam_id": null,
           "airshow_id": null,
           "picture_category_id": "4",
           "military_unit": "",
           "registration": "9M-AQJ",
           "code_number": "",
           "construction_number": "28-7125042",
           "date_taken": "9th March 1975",
           "date_taken_formatted": "1975-03-09 00:00:00",
           "year": "1975",
           "information": "A rather hazy slide being a long shot in 34c.",
           "approved": null,
           "display_picture": "yes",
           "reject_reason": "",
           "editors_choice": "no",
           "ab_publication": "",
           "ab_archive": "no",
           "correction_id": null,
           "correction_reason": null,
           "correction_status": null,
           "correction_rejection_reason": null,
           "views": "159",
           "created_on": "0000-00-00 00:00:00",
           "created_by": "0",
           "modified_on": "0000-00-00 00:00:00",
           "modified_by": null,
           "date_approved": "2012-01-02 18:58:07",
           "initial": "P",
           "manufacturer": "Piper",
           "type": "Piper PA-28 / 28R Cherokee",
           "model": "Piper PA-28-140 Cherokee D",
           "subtype": "Fixed Wing - Propeller",
           "subtype2": "Light Aircraft",
           "verified": "1",
           "operator": "Private",
           "operator_type": "Company",
           "airport": "Kota Kinabalu International (BKI / WBKK)",
           "country": "Malaysia",
           "deleted_on": null,
           "deleted_by": null,
           "slug": "general",
           "name": "General",
           "displayteam": null,
           "airshow": null,
           "category": "Light Aircraft",
           "group": "A",
           "picture_id": "1328215",
           "aircraft_verified": "1",
           "aircraft_type": "Piper PA-28 / 28R Cherokee",
           "operator_verified": "1",
           "location_verified": "1",
           "displayteam_verified": null,
           "airshow_verified": null,
           "picturecategory_verified": "1",
           "aircraft_type_search": "Piper PA-28 / 28R Cherokee Piper PA-28-140 Cherokee D"
        }
     },
     {
        "_index": "pictures_testing",
        "_type": "picture",
        "_id": "1328213",
        "_score": 1,
        "_source": {
           "id": "1328213",
           "aircraft_id": "5302",
           "operator_id": "3273",
           "operator_category_id": "3",
           "location_id": "2877",
           "displayteam_id": null,
           "airshow_id": null,
           "picture_category_id": "4",
           "military_unit": "",
           "registration": "9M-ANZ",
           "code_number": "",
           "construction_number": "28-20740",
           "date_taken": "9th March 1975",
           "date_taken_formatted": "1975-03-09 00:00:00",
           "year": "1975",
           "information": "-",
           "approved": null,
           "display_picture": "yes",
           "reject_reason": "",
           "editors_choice": "no",
           "ab_publication": "yes",
           "ab_archive": "no",
           "correction_id": null,
           "correction_reason": null,
           "correction_status": null,
           "correction_rejection_reason": null,
           "views": "126",
           "created_on": "0000-00-00 00:00:00",
           "created_by": "0",
           "modified_on": "0000-00-00 00:00:00",
           "modified_by": null,
           "date_approved": "2012-01-02 18:56:27",
           "initial": "P",
           "manufacturer": "Piper",
           "type": "Piper PA-28 / 28R Cherokee",
           "model": "Piper PA-28-140 Cherokee",
           "subtype": "Fixed Wing - Propeller",
           "subtype2": "Light Aircraft",
           "verified": "1",
           "operator": "Private",
           "operator_type": "Company",
           "airport": "Kota Kinabalu International (BKI / WBKK)",
           "country": "Malaysia",
           "deleted_on": null,
           "deleted_by": null,
           "slug": "general",
           "name": "General",
           "displayteam": null,
           "airshow": null,
           "category": "Light Aircraft",
           "group": "A",
           "picture_id": "1328213",
           "aircraft_verified": "1",
           "aircraft_type": "Piper PA-28 / 28R Cherokee",
           "operator_verified": "1",
           "location_verified": "1",
           "displayteam_verified": null,
           "airshow_verified": null,
           "picturecategory_verified": "1",
           "aircraft_type_search": "Piper PA-28 / 28R Cherokee Piper PA-28-140 Cherokee"
        }
     },

有人知道这里可能出了什么问题吗?

【问题讨论】:

  • 能否展示一下你运行时得到的真实映射:curl -XGET localhost:9200/pictures_testing,你分享的那个好像没有使用官方语法。
  • aircraft_type_search.search 字段是“not_analyzed”,因此要匹配查询,aircraft_type_search.search 字段应该包含单词“piper”,并且只包含该单词(没有其他单词)。你知道吗? (只是检查)
  • @Val 将该输出添加到问题中。我正在为弹性 BTW 使用 sense JSON 接口,这可能会解释语法。
  • @PaulinTrognon 但这是用于通配符搜索的正确方法,对吧?当然适用于我以相同方式设置的其他字段,因此“piper*”将是一个更典型的查询。
  • 您能否也展示一个您认为应该匹配的示例文档?

标签: amazon-web-services elasticsearch


【解决方案1】:

您不应在“aircraft_type_search.search”字段的映射中使用“not_analyzed”。 “not_analyzed”仅在您想完全搜索字段时使用,大小写相同。

如果您在搜索“piper”时想要结果“Piper PA-28 / 28R Cherokee Piper PA-28-140 Cherokee”,请将映射更改为:

  "aircraft_type_search": {
    "type": "string",
    "fields": {
      "lower_case_sort": {
        "type": "string",
        "analyzer": "case_insensitive_sort"
      },
      "search": {
        "type": "string",
        "analyzer": "standard" // CHANGE HERE
      }
    }
  }

那么您不必使用通配符查询。只需使用 ma​​tch 查询即可。

GET /pictures_testing/_search
{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "aircraft_type_search.search": "piper"
            }
         }
      }
   }
}

这将在“aircraft_type_search”字段中查找单词“piper”,并匹配您的搜索。

【讨论】:

  • 这似乎适用于该字段,但是我不能进行通配符查询。这对于注册字段来说尤其是一个问题,其中典型的搜索可能是“G-BOA*”,期望返回“G-BOAC、G-BOAB、G-BOAR”等形式的结果。是否有一种分析器类型可以适合这种不使用通配符的查询吗?如果我确实使用了通配符,是否有特别适合的分析器类型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多