• Elasticsearch学习--查询(prefix、wildcard、regexp、fuzzy)


    一、前缀搜索 prefix

    • 不计算相关度评分
    • 性能较差
    • 前缀搜索匹配的是分词后的词项
    • 前缀搜索没有缓存
    • 前缀搜索尽可能把前缀长度设置的更长
    1. GET product/_search
    2. {
    3. "query": {
    4. "fuzzy": {
    5. "name": {
    6. "value": "product1"
    7. }
    8. }
    9. }
    10. }

    index_prefixes为词项创建倒排索引

    比如computer这个单词,本身是一个词项,index_prefixes可以为这个单词再创建倒排索引,min_chars=2,max_chars=5的话,创建的索引:co、com、comp、compu

    1. PUT prefixindex
    2. {
    3. "mappings": {
    4. "properties": {
    5. "name": {
    6. "type": "text",
    7. "analyzer": "ik_max_word",
    8. "index_prefixes": {
    9. "min_chars": 2,
    10. "max_chars": 3
    11. }
    12. }
    13. }
    14. }
    15. }

    优缺点:

    优点:加快前缀索引的搜索效率

    缺点:占用内存、空间

    二、通配符 wildcard

    匹配的也是分词后的词项term

    1. # 造数据
    2. PUT testindex/_doc/1
    3. {
    4. "name":"zhang san"
    5. }
    6. PUT testindex/_doc/2
    7. {
    8. "name":"zhang si"
    9. }
    10. PUT testindex/_doc/3
    11. {
    12. "name":"zhuang san"
    13. }
    14. PUT testindex/_doc/4
    15. {
    16. "name":"zhuang si"
    17. }
    1. # 用法
    2. GET testindex/_search
    3. {
    4. "query": {
    5. "wildcard": {
    6. "name": {
    7. "value": "*san"
    8. }
    9. }
    10. }
    11. }

    * 数组中的每个值都是精准值

     三、正则 regexp

    1. 用法

    1. GET testindex/_search
    2. {
    3. "query": {
    4. "regexp": {
    5. "name": ".*san"
    6. }
    7. }
    8. }

    2. flags参数含义

     四、模糊查询 fuzzy

    1. 情况

    1)混淆字符(box->fox)

    2) 缺少字符(black->lack)

    3) 多出字符(sic -> sick

    4) 颠倒次序(act-> cat)

    2. 用法

    1. GET testindex/_search
    2. {
    3. "query": {
    4. "fuzzy": {
    5. "name": "xiaolahu"
    6. }
    7. }
    8. }

     3. 参数

    编辑距离:把字符改成正确的,需要挪到的次数

    1. GET testindex/_search
    2. {
    3. "query": {
    4. "fuzzy": {
    5. "name": {
    6. "value": "xiaoloahu",
    7. "fuzziness": 1,
    8. "transpositions":false
    9. }
    10. }
    11. }
    12. }

     fuzziness, 默认是auto,根据字符串长度,从0,1,2取值

     4. match查询也支持fuzziness

    1. GET testindex/_search
    2. {
    3. "query": {
    4. "match": {
    5. "name": {
    6. "query":"xiaoloahu",
    7. "fuzziness": 1
    8. }
    9. }
    10. }
    11. }

     match是分词的,fuzzy是不分词的

    fuzzy不适合数据量大时使用

    五、match_phrase_prefix

    match_phrase

    match_phrase会分词

    被检索字段必须包含match_phrase中的所有词项并且顺序必须是相同的

    被检索字段包含的match_phrase中的词项之间不能有其他词项

    match_phrase_prefix概念

    match_phrase_prefix与match_phrase相似,但是它多了一个特性,就是它允许在文本的最后一个词项(term)上的前缀匹配

    如果是一个单词,比如a,它会匹配文档字段所有以a开头的文档

    如果是一个短语,比如 "this is ma" ,他会先在倒排索引中做以ma做前缀搜索,然后在匹配到的doc中做match_phrase查询

    参数

    max_expansions:限制匹配的最大词项,默认50;越大越消耗性能,可以通过减少这个值,提高性能

    analyzer:指定何种分析器来对该短语进行分词处理

    boost :用于设置该查询的权重

    slop :允许短语间的词项(term)间隔

    slop 参数告诉 match_phrase 查询词条相隔多远时仍然能将文档视为匹配

    什么是相隔多远? 意思是说为了让查询和文档匹配你需要移动词条多少次

    六、N-gram和edge ngram

    ngram可以用于切词器(比分词器粒度更小)

    1. GET _analyze
    2. {
    3. "tokenizer": "ngram",
    4. "text": "reba always loves me"
    5. }

    有两个主要参数

    min_gram:创建索引所拆分字符的最小阈值

    max_gram:创建索引所拆分字符的最大阈值

    ngram也可以用于词项过滤器token filter

    1. GET _analyze
    2. {
    3. "tokenizer": "ik_max_word",
    4. "filter": [ "ngram" ],
    5. "text": "reba always loves me"
    6. }

    优点:做模糊搜索时,粒度更细

    缺点:占用大量的磁盘空间

    Edge n-gream

    ngram适用于前缀、中缀、后缀搜索

    Edge n-gream只适用于前缀搜索,但也会更节省空间

    Edge n-gream的性能比match_phrase_prefix更高

  • 相关阅读:
    Spring Boot启动流程分析
    Linkers of Addition
    Unity技术手册 - 创建物体有几种方式?
    k8s--基础--21--Statefulset
    29 Python的pandas模块
    flink的Standalone-HA模式安装
    MySQL总结 (思维导图,常用)
    图论---最小生成树
    echarts折线图设置背景颜色
    根据轮廓创建旋转框和椭圆
  • 原文地址:https://blog.csdn.net/CelineT/article/details/127218054