【发布时间】:2017-09-15 14:30:33
【问题描述】:
我有一个带有产品的弹性搜索 5.3 服务器。 每个产品都有一个 14 位数的产品代码,必须通过以下规则进行搜索。完整的代码应与仅包含后 9 位、后 6 位、后 5 位或后 4 位的搜索词匹配。
为了实现这一点,我创建了一个自定义分析器,它使用模式捕获标记过滤器在索引时创建适当的标记。这似乎工作正常。 _analysis API 显示创建了正确的术语。
为了从弹性搜索中获取文档,我使用 multi_match cross_fields bool 查询同时搜索多个字段。
当我的查询字符串的一部分与产品代码匹配,另一部分与任何其他字段匹配时,不会返回任何结果,但是当我分别搜索每个部分时,会返回适当的结果。此外,当我有多个部分跨越除产品代码之外的任何字段时,将返回正确的结果。
我的映射和分析器:
PUT /store
{
"mappings": {
"products":{
"properties":{
"productCode":{
"analyzer": "ProductCode",
"search_analyzer": "standard",
"type": "text"
},
"description": {
"type": "text"
},
"remarks": {
"type": "text"
}
}
}
},
"settings": {
"analysis": {
"filter": {
"ProductCodeNGram": {
"type": "pattern_capture",
"preserve_original": "true",
"patterns": [
"\\d{5}(\\d{9})",
"\\d{8}(\\d{6})",
"\\d{9}(\\d{5})",
"\\d{10}(\\d{4})"
]
}
},
"analyzer": {
"ProductCode": {
"filter": ["ProductCodeNGram"],
"type": "custom",
"preserve_original": "true",
"tokenizer": "standard"
}
}
}
}
}
查询
GET /store/products/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "[query_string]",
"fields": ["productCode", "description", "remarks"],
"type": "cross_fields",
"operator": "and"
}
}
]
}
}
}
样本数据
POST /store/products
{
"productCode": "999999123456789",
"description": "Foo bar",
"remarks": "Foobar"
}
以下查询字符串均返回一个结果:
“456789”、“foo”、“foobar”、“foo foobar”。
但是 query_string "foo 456789" 没有返回任何结果。
我很好奇为什么最后一次搜索没有返回任何结果。我相信它应该这样做。
【问题讨论】:
标签: elasticsearch