Dongxing's Wiki Dongxing's Wiki
首页
  • 剑指 Offer
  • LeetCode
  • 算法与数据结构
  • Python 语言
  • Web 开发
  • Hive
  • Elastic Search
  • 机器学习
  • NLP
  • 检索技术
  • 数据分析
  • 经验笔记
  • Linux 配置
  • 博客进化记
  • 杂谈
GitHub (opens new window)
首页
  • 剑指 Offer
  • LeetCode
  • 算法与数据结构
  • Python 语言
  • Web 开发
  • Hive
  • Elastic Search
  • 机器学习
  • NLP
  • 检索技术
  • 数据分析
  • 经验笔记
  • Linux 配置
  • 博客进化记
  • 杂谈
GitHub (opens new window)
  • Hive

    • hive常用操作笔记
  • ElasticSearch

    • Elastic Search快速上手(1):简介及安装配置
    • Elastic Search快速上手(2):将数据存入ES
    • Elastic Search快速上手(3):搜索
    • Elastic Search快速上手(4):细节补充
  • 大数据
  • ElasticSearch
anthony
2017-08-10

Elastic Search快速上手(4):细节补充

# 模糊搜索

可以进行模糊搜索:

GET job/type1/_search
{
  "query":{
    "fuzzy":{
      "title":{
        "value":"linx",
        "fuzziness": 2,
        "prefix_length": 1
      }
    }
  }
}

在上面的例子中,可模糊查询与linx相似的单词。 fuzziness是指“编辑距离”,就是说从一个字符串想要通过增删改变换到另一个字符串,需要操作的最少次数。比如,linux和linxu之间的编辑距离为1,因为交换ux就可以完成变换。 prefix_length是说多少位字符认为是前缀。前缀部分必须完全匹配。

# 搜索建议

搜索建议功能,需要配合程序,在向es中存入文档时,就需要通过分词等方式,指定搜索建议字段的内容。

指定之后,可通过suggest方式,根据用户的当前输入,获取搜索建议:

GET wechatsearch/passage/_search
{
  "suggest":{
    "my-suggest":{
      "text":"ppt",
      "completion":{
        "field":"suggest",
        "fuzzy":{
          "fuzziness":2
        }
      }
    }
  },
  "_source": "ptitle"
}

上面代码搜索出与ppt关键词相近的搜索建议。其中,fuzziness值为2,因此,像p2p、pdf之类的关键词也会被搜索出来。较小的fuzziness会有更精确的匹配。

返回结果示例: { "took": 20, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 0, "max_score": 0, "hits": [] }, "suggest": { "my-suggest": [ { "text": "ppt", "offset": 0, "length": 3, "options": [ { "text": "pdf", "_index": "wechatsearch", "_type": "passage", "_id": "16", "_score": 10, "_source": { "ptitle": "Smallpdf:无所不能的PDF在线处理站" } }, { "text": "ppt", "_index": "wechatsearch", "_type": "passage", "_id": "7", "_score": 10, "_source": { "ptitle": "这18个技巧都不知道,别说你会做PPT" } }, { "text": "pa", "_index": "wechatsearch", "_type": "passage", "_id": "10", "_score": 5, "_source": { "ptitle": "吐血整理了这20个堪称神器的网站,个个都能解你燃眉之急" } } ] } ] } }

# 搜索高亮

可以指定哪些字段搜索高亮,并且指定高亮字符两侧的包裹标签,从而实现查询的返回结果包含html高亮效果。

GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

注意,高亮的结果在返回时单独存放,并不是将_source数据做了改变。 单独有一个highlight部分存放高亮内容: { ... "hits": { "total": 1, "max_score": 0.23013961, "hits": [ { ... "_score": 0.23013961, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] }, "highlight": { "about": [ "I love to go rock climbing" ] } } ] } }


关于ES的入门使用暂时整理到这里。 ES的官方文档是最好的参考资料,介绍很全面。


完成这些文档的同时,做了一个简单的小项目,基于Python(Django)的web搜索界面,采用ES作为搜索引擎。 此项目地址: https://github.com/dox1994/WechatSearch_Python

搜索提示:

搜索结果:

上次更新: 2022/11/11, 2:11:00
Elastic Search快速上手(3):搜索

← Elastic Search快速上手(3):搜索

Theme by Vdoing | Copyright © 2017-2023 anthony 京ICP备17072417-3
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式