ArcherWong博客
首页
博客
elasticsearch-mathc和term的区分
作者:ArcherWong
分类:elasticsearch
时间:2019-01-03 21:33:26
阅读:375
elasticsearch和mysql在思想上是有不同的,elasticsearch有分词一说,比如`北京奥运`分词成`北京`,`奥运`,`北京奥运`。分词要要考虑两点,一个是查询字符串要不要分词,还有就是原存储字段是不是精确值。 # 1. match 查询 无论你在任何字段上进行的是全文搜索还是精确查询,match 查询是你可用的标准查询。 `、如果你在一个全文字段上使用 match 查询,在执行查询前,它将用正确的分析器去分析查询字符串: ``` { "match": { "tweet": "About Search" }} ``` 2、如果在一个精确值的字段上使用它, 例如数字、日期、布尔或者一个 NOT_ANALYZED 字符串字段,那么它将会精确匹配给定的值: ``` { "match": { "age": 26 }} { "match": { "date": "2014-09-01" }} { "match": { "public": true }} { "match": { "tag": "full_text" }} ``` match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,因此相比于term的精确搜索,match是分词匹配搜索,match搜索还有两个相似功能的变种,一个是match_phrase,一个是multi_match # 2. term 查询 term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词,所以我们的搜索词必须是文档分词集合中的一个。比如说我们要找标题为北京奥运的所有文档 ``` $curl -XGET http://localhost:9200/index/doc/_search?pretty -d '{ "query":{ "term":{ "title":"北京奥运" } } }' ``` 将会得到如下结果 ``` { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.92055845, "hits": [ { "_index": "index", "_type": "doc", "_id": "3", "_score": 0.92055845, "_source": { "content": "同一个世界同一个梦想", "title": "北京奥运", "tags": [ "和平" ] } } ] } } ``` 搜索title包含北京或者奥运的,结果也一样,但是如果你搜索词为京奥,或者北京奥这样的,那么搜索结果将为空 ``` { "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null, "hits" : [ ] } } ``` # 3. match_phrase match_phrase为按短语搜索,match_phrase的搜索方式和match类似,先对搜索词建立索引,并要求**所有分词**必须在文档中出现(像不像operator为and的match查询),除此之外,还必须满足分词在文档中出现的**顺序**和搜索词中一致且各搜索词之间必须紧邻,因此match_phrase也可以叫做紧邻搜索。 所以,当我们搜`美国留给`时 ``` curl -XGET http://localhost:9200/index/doc/_search?pretty -d '{ "query": { "match_phrase": { "content": "美国留给" } } }' ``` 以下内容`美国留给伊拉克的是个烂摊子吗`是可以搜索出来的 ``` "_source" : { "content" : "美国留给伊拉克的是个烂摊子吗", "title" : "标题", "tags" : [ "美国", "伊拉克", "烂摊子" ] } ``` 但是我们搜索`留给美国`或`美国伊拉克`时,却没有搜索结果,因为第一个顺序不对,第二个不是紧邻(隔着留给)。 紧邻对于匹配度要求较高,为了减小精度增加可操作性,引入了slop参数。该参数可以指定相隔多少个词仍被算作匹配成功。如下, ``` curl -XGET http://localhost:9200/index/doc/_search?pretty -d '{ "query": { "match_phrase": { "content": { "query": "美国伊拉克", "slop": "1" } } } }' ``` 当我们将slop设置为1时,下面文档是可以搜索到的 ``` "_source" : { "content" : "美国留给伊拉克的是个烂摊子吗", "title" : "标题", "tags" : [ "美国", "伊拉克", "烂摊子" ] } ``` 需要注意的是,当slop的值过大时(超出文档总分词数),那么分词数据将可以是随意的,即跟operator为and的match查询效果一样。比如我们查询 ``` curl -XGET http://localhost:9200/index/doc/_search?pretty -d '{ "query": { "match_phrase": { "content": { "query": "伊拉克美国", "slop": "12" } } } }' ``` 将会得到与上面一样的结果 # 4. multi_match 如果我们希望两个字段进行匹配,其中一个字段有这个文档就满足的话,使用multi_match ``` { "query": { "multi_match": { "query" : "我的宝马多少马力", "fields" : ["title", "content"] } } } ``` 但是multi_match就涉及到匹配评分的问题了。
标签:
上一篇:
灵活的两列布局
下一篇:
elasticsearch安装中文分词器
文章分类
css
elasticsearch
git
golang
guacamole
javascript
letsencrypt
linux
nginx
other
php
python
vue
web
阅读排行
golang笔记---关联数组(map)
letsencrypt证书-管理工具certbot
centos7.3配置guacamole
golang笔记---template模板语法
nginx笔记-proxy_cache缓存详解
友情链接
node文件
laravel-vue
ArcherWong的博客园