Elasticsearch-高级搜索(拼音|首字母|简繁|二级搜索)

这篇具有很好参考价值的文章主要介绍了Elasticsearch-高级搜索(拼音|首字母|简繁|二级搜索)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需求:

  1. 中文搜索、英文搜索、中英混搜
  2. 全拼搜索、首字母搜索、中文+全拼、中文+首字母混搜
  3. 简繁搜索
  4. 二级搜索(对第一次搜索结果,再进行搜索)

一、ES相关插件

IK分词:

GitHub - medcl/elasticsearch-analysis-ik: The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary.

拼音:

https://github.com/medcl/elasticsearch-analysis-pinyin

简繁体:

ehttps://github.com/medcl/elasticsearch-analysis-stconvert

二、什么是 analysis

        analysis分析是 Elasticsearch 在文档发送之前对文档正文执行的过程,以添加到反向索引中(inverted index)。 在将文档添加到索引之前,Elasticsearch 会为每个分析的字段执行许多步骤:

  •     Character filtering (字符过滤器): 使用字符过滤器转换字符
  •     Breaking text into tokens (把文字转化为标记): 将文本分成一组一个或多个标记
  •     Token filtering:使用标记过滤器转换每个标记
  •     Token indexing:把这些标记存于索引中

详细介绍:Elasticsearch: analyzer_Elastic 中国社区官方博客的博客-CSDN博客_elasticsearch analyzer如果大家之前看过我写的文章“开始使用Elasticsearch (3)”,在文章的最后部分写了有关于analyzer的有关介绍。在今天的文章中,我们来进一步了解analyzer。 analyzer执行将输入字符流分解为token的过程,它一般发生在两个场合:在indexing的时候,也即在建立索引的时候在searching的时候,也即在搜索时,分析需要搜索的词语什么是analysis...https://blog.csdn.net/UbuntuTouch/article/details/100392478

三、索引模板

PUT /_template/test_template
{
  "index_patterns": [
    "test-*"
  ],
  "aliases": {
    "test_read": {}
  },
  "settings": {
    "index": {
      "max_result_window": "100000",
      "refresh_interval": "5s",
      "number_of_shards": "5",
      "translog": {
        "flush_threshold_size": "1024mb",
        "sync_interval": "30s",
        "durability": "async"
      },
      "number_of_replicas": "1"
    },
    "analysis": {
      "char_filter": {
        "tsconvert": {
          "type": "stconvert",
          "convert_type": "t2s"
        }
      },
      "analyzer": {
        "ik_t2s_pinyin_analyzer": {
          "type": "custom",
          "char_filter": [
            "tsconvert"
          ],
          "tokenizer": "ik_max_word",
          "filter": [
            "pinyin_filter",
            "lowercase"
          ]
        },
        "stand_t2s_pinyin_analyzer": {
          "type": "custom",
          "char_filter": [
            "tsconvert"
          ],
          "tokenizer": "standard",
          "filter": [
            "pinyin_filter",
            "lowercase"
          ]
        },
        "ik_t2s_analyzer": {
          "type": "custom",
          "char_filter": [
            "tsconvert"
          ],
          "tokenizer": "ik_max_word",
          "filter": [
            "lowercase"
          ]
        },
        "stand_t2s_analyzer": {
          "type": "custom",
          "char_filter": [
            "tsconvert"
          ],
          "tokenizer": "standard",
          "filter": [
            "lowercase"
          ]
        },
        "ik_pinyin_analyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "filter": [
            "pinyin_filter",
            "lowercase"
          ]
        },
        "stand_pinyin_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "pinyin_filter",
            "lowercase"
          ]
        },
        "keyword_t2s_pinyin_analyzer": {
          "filter": [
            "pinyin_filter",
            "lowercase"
          ],
          "char_filter": [
            "tsconvert"
          ],
          "type": "custom",
          "tokenizer": "keyword"
        },
        "keyword_pinyin_analyzer": {
          "filter": [
            "pinyin_filter",
            "lowercase"
          ],
          "type": "custom",
          "tokenizer": "keyword"
        }
      },
      "filter": {
        "pinyin_first_letter_and_full_pinyin_filter": {
          "type": "pinyin",
          "keep_first_letter": true,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": false,
          "keep_joined_full_pinyin": true,
          "keep_none_chinese": true,
          "none_chinese_pinyin_tokenize": false,
          "keep_none_chinese_in_joined_full_pinyin": true,
          "keep_original": false,
          "limit_first_letter_length": 1000,
          "lowercase": true,
          "trim_whitespace": true,
          "remove_duplicated_term": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "index_phrases": true,
        "analyzer": "ik_max_word",
        "index": true,
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "stand": {
            "analyzer": "standard",
            "type": "text"
          },
          "STPA": {
            "type": "text",
            "analyzer": "stand_t2s_pinyin_analyzer"
          },
          "ITPA": {
            "type": "text",
            "analyzer": "ik_t2s_pinyin_analyzer"
          }
        }
      },
      "desc": {
        "index_phrases": true,
        "analyzer": "ik_max_word",
        "index": true,
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "stand": {
            "analyzer": "standard",
            "type": "text"
          },
          "STPA": {
            "type": "text",
            "analyzer": "stand_t2s_pinyin_analyzer"
          },
          "ITPA": {
            "type": "text",
            "analyzer": "ik_t2s_pinyin_analyzer"
          }
        }
      },
      "abstr": {
        "index_phrases": true,
        "analyzer": "ik_max_word",
        "index": true,
        "type": "text",
        "fields": {
          "keyword": {
            "ignore_above": 256,
            "type": "keyword"
          },
          "stand": {
            "analyzer": "standard",
            "type": "text"
          },
          "STPA": {
            "type": "text",
            "analyzer": "stand_t2s_pinyin_analyzer"
          },
          "ITPA": {
            "type": "text",
            "analyzer": "ik_t2s_pinyin_analyzer"
          }
        }
      }
    }
  }
}

四、DSL语句

GET /test_read/_search
{
  "from": 0,
  "size": 10,
  "terminate_after": 100000,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "bj天安门 OR 测试",
            "fields": [
              "name.ITPA"
            ],
            "type": "phrase",
            "default_operator": "and"
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "天安门"
          }
        }
      ]
    }
  },
  "highlight": {
    "fragment_size": 1000,
    "pre_tags": [
      "<span style=\"color:red;background:yellow;\">"
    ],
    "post_tags": [
      "</span>"
    ],
    "fields": {
      "name.stand": {},
      "desc.stand": {},
      "abstr.stand": {},
      "name.IPA": {},
      "desc.IPA": {},
      "abstr.IPA": {},
      "name.ITPA": {},
      "desc.ITPA": {},
      "abstr.ITPA": {}
    }
  }
}

post_filter:后过滤器 | Elasticsearch: 权威指南 | Elastic

PS:post_filter实现二次搜索功能,post_filter无法使用es高亮功能,需要自己通过代码进行手动标记高亮;根据上面的DSL语句,可写出对应的代码啦~

拼音插件配置:文章来源地址https://www.toymoban.com/news/detail-728105.html

  • keep_first_letter:这个参数会将词的第一个字母全部拼起来.例如:刘德华->ldh.默认为:true
  • keep_separate_first_letter:这个会将第一个字母一个个分开.例如:刘德华->l,d,h.默认为:flase.如果开启,可能导致查询结果太过于模糊,准确率太低.
  • limit_first_letter_length:设置最大keep_first_letter结果的长度,默认为:16
  • keep_full_pinyin:如果打开,它将保存词的全拼,并按字分开保存.例如:刘德华> [liu,de,hua],默认为:true
  • keep_joined_full_pinyin:如果打开将保存词的全拼.例如:刘德华> [liudehua],默认为:false
  • keep_none_chinese:将非中文字母或数字保留在结果中.默认为:true
  • keep_none_chinese_together:保证非中文在一起.默认为: true, 例如: DJ音乐家 -> DJ,yin,yue,jia, 如果设置为:false, 例如: DJ音乐家 -> D,J,yin,yue,jia, 注意: keep_none_chinese应该先开启.
  • keep_none_chinese_in_first_letter:将非中文字母保留在首字母中.例如: 刘德华AT2016->ldhat2016, 默认为:true
  • keep_none_chinese_in_joined_full_pinyin:将非中文字母保留为完整拼音. 例如: 刘德华2016->liudehua2016, 默认为: false
  • none_chinese_pinyin_tokenize:如果他们是拼音,切分非中文成单独的拼音项. 默认为:true,例如: liudehuaalibaba13zhuanghan -> liu,de,hua,a,li,ba,ba,13,zhuang,han, 注意: keep_none_chinese和keep_none_chinese_together需要先开启.
  • keep_original:是否保持原词.默认为:false
  • lowercase:小写非中文字母.默认为:true
  • trim_whitespace:去掉空格.默认为:true
  • remove_duplicated_term:保存索引时删除重复的词语.例如: de的>de, 默认为: false, 注意:开启可能会影响位置相关的查询.
  • ignore_pinyin_offset:在6.0之后,严格限制偏移量,不允许使用重叠的标记.使用此参数时,忽略偏移量将允许使用重叠的标记.请注意,所有与位置相关的查询或突出显示都将变为错误,您应使用多个字段并为不同的字段指定不同的设置查询目的.如果需要偏移量,请将其设置为false。默认值:true

到了这里,关于Elasticsearch-高级搜索(拼音|首字母|简繁|二级搜索)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Elasticsearch 高级搜索技巧和最佳实践

    Elasticsearch 高级搜索技巧和最佳实践          Elasticsearch 是一个开源的分布式搜索和分析引擎,它支持实时地存储、搜索和分析大规模数据。它被广泛应用于各行各业,用于构建高性能的搜索引擎、日志分析系统、电子商务推荐系统等。          本文将介绍 Elasticsearch 的

    2024年02月10日
    浏览(35)
  • 架构师系列-搜索引擎ElasticSearch(四)- 高级查询

    ES查询 该方式可以通过kabana、curl、elasticsearch-head(纯前端)去操作 term查询和字段类型有关系,首先回顾一下ElasticSearch两个数据类型 ElasticSearch两个数据类型 1、text:会分词,不支持聚合 2、keyword:不会分词,将全部内容作为一个词条,支持聚合 term查询:不会对查询条件进

    2024年04月15日
    浏览(55)
  • 自然语言处理学习笔记(十一)————简繁转换与拼音转换

    目录 1.简繁转换 2.拼音转换 1.简繁转换 简繁转换指的是简体中文和繁体中文之间的相互转换。可能有的人觉得,这很简单, 按字转换 就好了。HanLP提供了这样的朴素实现 CharTable, 用来执行字符正规化(繁体-简体,全角-半角,大写-小写) 事实上,汉字历史悠久,地域复杂,

    2024年02月07日
    浏览(36)
  • Elastic 发布 Elasticsearch Relevance Engine™ — 为 AI 革命提供高级搜索能力

    作者:Matt Riley 今天我们将向大家介绍 Elasticsearch Relevance Engine™(ESRE™) ,这是一种创建高度相关的 AI 搜索应用程序的新功能。ESRE 建立在 Elastic 在搜索领域的领导地位以及超过两年的机器学习研究和开发基础之上。Elasticsearch Relevance Engine 结合了 AI 的最佳实践和 Elastic 的文

    2024年02月06日
    浏览(29)
  • elasticsearch的拼音分词器安装

    安装拼音分词器 第一步:下载 要实现根据字母做补全,就必须对文档按照拼音分词。在 GitHub 上恰好有 elasticsearch 的拼音分词插件。地址: 仓管的主页: https://github.com/infinilabs/analysis-pinyin 仓管的版本页 https://github.com/infinilabs/analysis-pinyin/releases 百度仓库: 链接:百度网盘

    2024年01月22日
    浏览(30)
  • elasticsearch 拼音分词器 & 自动补全。

    2. 自动补全。 当用户在搜索框输入字符时,我们应该提示出与该字符有关的搜索项,如图。 这种根据用户输入的字母,提示完整词条的功能,就是自动补全了。 因为需要根据拼音字母来推断,因此要用到拼音分词功能。 2.1. 拼音分词器。 要实现根据字母做补全,就必须对文

    2024年02月06日
    浏览(38)
  • Elasticsearch教程(35) ik中文分词器+pinyin拼音分词器+同义词

    闲来无事,发现上一篇ES博客还是 去年9月份 写的中文ik分词器 pinyin 首字母 search_as_you_type 组合使用,该篇文章还挖了一个 大坑 没有填,快一年了,是时候填下坑了。 针对股票查询这个特点场景,再结合一般使用者的搜索习惯,暂时确定如下7种期望效果。 上一篇博客Elast

    2023年04月09日
    浏览(33)
  • 服务器安装配置elasticsearch,kibana,IK分词器和拼音分词器,集群搭建教程

    elasticsearch安装教程大全 elasticsearch学习笔记(一) elasticsearch学习笔记(二) elasticsearch学习笔记(三) 可参考:Debian安装docker Centos安装docker (docker-compose可以直接互连) 因为我们还需要部署kibana容器,因此需要让es和kibana容器互联。这里先创建一个网络: Elasticsearch 和 kibana 版

    2023年04月08日
    浏览(30)
  • Elasticsearch实现检索词自动补全(检索词补全,自动纠错,拼音补全,繁简转换) 包含demo

    下面的请求定义了一个名为 “book” 的 Elasticsearch 索引,其中包含一个 具有 “text” 数据类型和 “standard” 分析器且名为 “title” 的字段。此字段用于处理书籍标题的文本数据。定义了名为 “suggest” 的 “completion” 子字段,用于支持实时搜索建议的自动补全功能。 增加测

    2024年02月07日
    浏览(29)
  • ElasticSearch关于自定义分词器模糊检索数字+字母

    之前的系统由于一些表的数据已经达到1~2百万行的数据了,而且还在与日俱增,一些条件比较复杂的检索,已经明显感觉到比较慢,影响用户的使用体验。所以引入的ElasiticSearch进行检索优化,效果还是比较好的。因为之前都没有怎么涉及到模糊检索这一块需求,只是为了速

    2024年02月16日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包