2015-05-15 63 views
1

您好,我正在尝试设置一个搜索框,它将对某些字段执行部分搜索并对其他字段执行标准搜索。我快到了,但我不能让过去的以下障碍:弹性不匹配初始字符的多匹配查询

这是我的索引:

PUT /my_index 
{ 
    "mappings": { 
     "blogpost": { 
      "properties": { 
       "firstname": { 
        "fields": { 
         "autocomplete": { 
          "index_analyzer": "autocomplete", 
          "type": "string" 
         }, 
         "firstname": { 
          "index_analyzer": "standard", 
          "type": "string" 
         } 
        }, 
        "type": "string" 
       } 
      } 
     } 
    }, 
    "settings": { 
     "index": { 
      "analysis": { 
       "analyzer": { 
        "autocomplete": { 
         "tokenizer": "ngram_tokenizer", 
         "type": "custom" 
        }, 
        "standard": { 
         "type": "standard" 
        } 
       }, 
       "tokenizer": { 
        "ngram_tokenizer": { 
         "max_gram": "20", 
         "min_gram": "2", 
         "type": "nGram" 
        } 
       } 
      }, 
      "creation_date": "1431690991641", 
      "number_of_replicas": "0", 
      "number_of_shards": "3", 
      "uuid": "W4Ug6IadS9mYuN5_Pqlhow", 
      "version": { 
       "created": "1040499" 
      } 
     } 
    } 
} 

指数1号文件:

PUT /my_index/blogpost/1 
{"firstname" : "Albert"} 

简单的查询:

/_search?q=Albert 

返回Albert。都好。

Multi_match查询:

{ 
    "query": { 
    "multi_match": { 
     "query": "Albert", 
     "fields": [ 
     "firstname", 
     "firstname.autocomplete" 
     ] 
    } 
    } 
} 

也返回伟业。都好。

如果我用伯特替代艾伯特,它会返回艾伯特。都好。

但是“Al”或“al”或“Alber”或“alber”不会!包含开头字母的任何搜索都会失败。

然而

/my_index/_search?firstname.autocomplete:Al 

都好。

请帮忙。

回答

4

field.autocomplete的搜索分析器是默认的,通常是standard

因此,当您搜索Al时,实际上您正在寻找“al”,即使是“Al”,您最终还是会搜索小写版本。

但是,使用自动完成分析器进行索引时,您不会将数据规范化为小写,因此它在索引中只有术语“A1”。

您可以使用API​​分析检查数据如何进行了分析

GET /my_index/_analyze?field=firstname.autocomplete&text=Albert" 

添加lowercase token filter为“自动完成”分析应该可以解决这个问题:

"autocomplete": { 
         "tokenizer": "ngram_tokenizer", 
         "type": "custom", 
         "filter" :[ 
          "lowercase" 
         ] 
        },