elasticsearch实战三部曲之三:搜索操作(2)

“_score”: 0.5754429,

“_source”: {

“id”: “4”,

“title”: “Thinking in Java”,

“language”: “java”,

“author”: “Bruce Eckel”,

“price”: 70.1,

“publish_time”: “2015-07-06”,

“description”: “Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!”

}

},

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “3”,

“_score”: 0.2876821,

“_source”: {

“id”: “3”,

“title”: “Core Java”,

“language”: “java”,

“author”: “Horstmann”,

“price”: 85.9,

“publish_time”: “2016-06-01”,

“description”: "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "

}

}

]

}

}

分词查询(match query)

  1. term query的特点是将输入的内容作为一个词项来用,例如以下的查询是没有结果的:

GET englishbooks/_search

{

“query”:{

“term”:{“title”:“core java”}

}

}

上述查询没有结果的原因,是因为"core java"被当做一个词项去查询了,而title的分词结果中只有"core"、"java"这些分词过的词项,并没有一个叫做"core java"的词项,所以搜不到结果;

  1. 如果输入的查询条件"core java"也被做一次分词处理,再把处理结果"core"和"java"用来搜索,应该就能得到结果了,match query就是用来对输入条件做分词处理的,如下:

GET englishbooks/_search

{

“query”:{

“match”:{“title”:“Core Java”}

}

}

搜索结果如下,包含了java的两条记录都被查出来了:

{

“took”: 8,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 2,

“max_score”: 0.5754429,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “4”,

“_score”: 0.5754429,

“_source”: {

“id”: “4”,

“title”: “Thinking in Java”,

“language”: “java”,

“author”: “Bruce Eckel”,

“price”: 70.1,

“publish_time”: “2015-07-06”,

“description”: “Thinking in Java should be read cover to cover by every Java programmer, then kept close at hand for frequent reference. The exercises are challenging, and the chapter on Collections is superb!”

}

},

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “3”,

“_score”: 0.5753642,

“_source”: {

“id”: “3”,

“title”: “Core Java”,

“language”: “java”,

“author”: “Horstmann”,

“price”: 85.9,

“publish_time”: “2016-06-01”,

“description”: "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "

}

}

]

}

}

  1. 如果我们的本意是只要"Core Java"的匹配结果,上面的结果显然是不符合要求的,此时可以给查询条件加个"operator":"and"属性,就会查询匹配了所有关键词的文档,注意json的结构略有变化,以前title的属性是搜索条件,现在变成了一个json对象,里面的query属性是原来的搜索条件:

GET englishbooks/_search

{

“query”:{

“match”:{

“title”:{

“query”:“Core Java”,

“operator”:“and”

}

}

}

}

这次的搜索结果就是同时匹配了"core"和"java"两个词项的记录了(为什么core和java是小写? 因为"Core Java"被分词后改为了小写,再去搜索的):

{

“took”: 11,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 1,

“max_score”: 0.5753642,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “3”,

“_score”: 0.5753642,

“_source”: {

“id”: “3”,

“title”: “Core Java”,

“language”: “java”,

“author”: “Horstmann”,

“price”: 85.9,

“publish_time”: “2016-06-01”,

“description”: "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "

}

}

]

}

}

match_phrase搜索

match_phrase搜索和前面的match搜索相似,并且有以下两个特点:

  1. 分词后的所有词项都要匹配上,也就是前面的"operator":"and"属性的效果;

  2. 分析后的词项顺序要和搜索字段的顺序一致,才能匹配上;

GET englishbooks/_search

{

“query”:{

“match_phrase”:{“title”:“Core Java”}

}

}

上述查询可以搜索到结果,但如果将"Core Java"改成"Java Core"就搜不到结果了,但是match query用"Java Core"是可以搜到结果的;

match_phrase_prefix搜索

match_phrase_prefix的功能和前面的match_phrase类似,不过match_phrase_prefix支持最后一个词项做前缀匹配,如下所示,"Core J"这个搜索条件用match_phrase是搜不到结果的,但是match_phrase_prefix可以,因为"J"可以作为前缀和"Java"匹配:

GET englishbooks/_search

{

“query”:{

“match_phrase”:{“title”:“Core J”}

}

}

multi_match搜素

multi_match是在match的基础上支持多字段搜索,以下查询就是用"1986"和"deep"这两个词项,同时搜索title和description两个字段:

GET englishbooks/_search

{

“query”:{

“multi_match”:{

“query”:“1986 deep”,

“fields”:[“title”, “description”]

}

}

}

响应如下,可见title和description中含有词项"1986"或者"deep"的文档都被返回了:

{

“took”: 4,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 2,

“max_score”: 0.79237825,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “2”,

“_score”: 0.79237825,

“_source”: {

“id”: “2”,

“title”: “Compilers”,

“language”: “c”,

“author”: “Alfred V.Aho”,

“price”: 62.5,

“publish_time”: “2011-01-01”,

“description”: “In the time since the 1986 edition of this book, the world of compiler designhas changed significantly.”

}

},

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “1”,

“_score”: 0.2876821,

“_source”: {

“id”: “1”,

“title”: “Deep Learning”,

“language”: “python”,

“author”: “Yoshua Bengio”,

“price”: 549,

“publish_time”: “2016-11-18”,

“description”: “written by three experts in the field, deep learning is the only comprehensive book on the subject.”

}

}

]

}

}

terms query

terms是term查询的升级,用来查询多个词项:

GET englishbooks/_search

{

“query”:{

“terms”:{

“title”:[“deep”, “core”]

}

}

}

响应如下,title中含有deep和core的文档都被查到:

{

“took”: 5,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 2,

“max_score”: 1,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “1”,

“_score”: 1,

“_source”: {

“id”: “1”,

“title”: “Deep Learning”,

“language”: “python”,

“author”: “Yoshua Bengio”,

“price”: 549,

“publish_time”: “2016-11-18”,

“description”: “written by three experts in the field, deep learning is the only comprehensive book on the subject.”

}

},

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “3”,

“_score”: 1,

“_source”: {

“id”: “3”,

“title”: “Core Java”,

“language”: “java”,

“author”: “Horstmann”,

“price”: 85.9,

“publish_time”: “2016-06-01”,

“description”: "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "

}

}

]

}

}

范围查询

range query是范围查询,例如查询publish_time在"2016-01-01"到"2016-12-31"之间的文档:

GET englishbooks/_search

{

“query”:{

“range”:{

“publish_time”:{

“gte”:“2016-01-01”,

“lte”:“2016-12-31”,

“format”:“yyyy-MM-dd”

}

}

}

}

篇幅所限,此处略去返回结果;

exists query

exists query返回的是字段中至少有一个非空值的文档:

GET englishbooks/_search

{

“query”:{

“exists”:{

“field”:“author”

}

}

}

前缀查询

用于查询某个字段是否以给定前缀开始:

GET englishbooks/_search

{

“query”:{

“prefix”:{

“title”:“cor”

}

}

}

以上请求可以查到title字段为"Core Java"的文档:

{

“took”: 6,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 1,

“max_score”: 1,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “3”,

“_score”: 1,

“_source”: {

“id”: “3”,

“title”: “Core Java”,

“language”: “java”,

“author”: “Horstmann”,

“price”: 85.9,

“publish_time”: “2016-06-01”,

“description”: "The book is aimed at experienced programmers who want to learn how to write useful Java applications and applets. "

}

}

]

}

}

通配符查询

以下查询,可以搜到title字段中含有"core"的文档,另外需要注意的是,"?“匹配一个字符,”*"匹配零个或者多个字符:

GET englishbooks/_search

{

“query”:{

“wildcard”:{

“title”:“cor?”

}

}

}

正则表达式

使用属性regexp可以进行正则表达式查询,例如查找description字段带有4位数字的分词的文档:

GET englishbooks/_search

{

“query”:{

“regexp”:{

“description”:“[0-9]{4}”

}

}

}

查找结果如下,description字段中带有数字1986:

{

“took”: 4,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 1,

“max_score”: 1,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “2”,

“_score”: 1,

“_source”: {

“id”: “2”,

“title”: “Compilers”,

“language”: “c”,

“author”: “Alfred V.Aho”,

“price”: 62.5,

“publish_time”: “2011-01-01”,

“description”: “In the time since the 1986 edition of this book, the world of compiler designhas changed significantly.”

}

}

]

}

}

模糊查询(fuzzy query)

fuzzy是通过计算词项与文档的编辑距离来得到结果的,例如查找description字段还有分词"1986"的时候,不小心输入了"1987",通过fuzzy查询也能得到结果,只是得分变低了,请求内容如下所示:

GET englishbooks/_search

{

“query”:{

“fuzzy”:{

“description”:“1987”

}

}

}

搜索到的文档如下所示,得分只有0.5942837,低于用"1986"查询的0.79237825:

{

“took”: 5,

“timed_out”: false,

“_shards”: {

“total”: 5,

“successful”: 5,

“skipped”: 0,

“failed”: 0

},

“hits”: {

“total”: 1,

“max_score”: 0.5942837,

“hits”: [

{

“_index”: “englishbooks”,

“_type”: “IT”,

“_id”: “2”,

“_score”: 0.5942837,

“_source”: {

“id”: “2”,

“title”: “Compilers”,

“language”: “c”,

“author”: “Alfred V.Aho”,

“price”: 62.5,

“publish_time”: “2011-01-01”,

“description”: “In the time since the 1986 edition of this book, the world of compiler designhas changed significantly.”

}

}

]

}

}

需要注意的是,fuzzy查询时消耗资源较大;

复合查询

常用到的复合查询是bool query,可以用下表中的条件组合查询:

| 属性 | 作用 |

| — | — |

| must | 必须匹配,相当于SQL中的AND |

| should | 可以匹配,相当于SQL中的OR |

| must_not | 必须不匹配 |

| filter | 和must一样,但是不评分 |

以下条件,搜索的是title中带有java,但是不包含core的文档:

GET englishbooks/_search

{

“query”:{

“bool”:{

“must”:{

“term”:{“title”:“java”}

},

“must_not”:[

{“term”:{“title”:“core”}}

]

}

}

}

得到的文档中,带有core词项的已经被过滤了:

总结

上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。

很多人担心学了容易忘,这里教你一个方法,那就是重复学习。

打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

“id”: “2”,

“title”: “Compilers”,

“language”: “c”,

“author”: “Alfred V.Aho”,

“price”: 62.5,

“publish_time”: “2011-01-01”,

“description”: “In the time since the 1986 edition of this book, the world of compiler designhas changed significantly.”

}

}

]

}

}

需要注意的是,fuzzy查询时消耗资源较大;

复合查询

常用到的复合查询是bool query,可以用下表中的条件组合查询:

| 属性 | 作用 |

| — | — |

| must | 必须匹配,相当于SQL中的AND |

| should | 可以匹配,相当于SQL中的OR |

| must_not | 必须不匹配 |

| filter | 和must一样,但是不评分 |

以下条件,搜索的是title中带有java,但是不包含core的文档:

GET englishbooks/_search

{

“query”:{

“bool”:{

“must”:{

“term”:{“title”:“java”}

},

“must_not”:[

{“term”:{“title”:“core”}}

]

}

}

}

得到的文档中,带有core词项的已经被过滤了:

总结

上述知识点,囊括了目前互联网企业的主流应用技术以及能让你成为“香饽饽”的高级架构知识,每个笔记里面几乎都带有实战内容。

很多人担心学了容易忘,这里教你一个方法,那就是重复学习。

打个比方,假如你正在学习 spring 注解,突然发现了一个注解@Aspect,不知道干什么用的,你可能会去查看源码或者通过博客学习,花了半小时终于弄懂了,下次又看到@Aspect 了,你有点郁闷了,上次好像在哪哪哪学习,你快速打开网页花了五分钟又学会了。

从半小时和五分钟的对比中可以发现多学一次就离真正掌握知识又近了一步。

[外链图片转存中…(img-DvHfDwJg-1714289946228)]

人的本性就是容易遗忘,只有不断加深印象、重复学习才能真正掌握,所以很多书我都是推荐大家多看几遍。哪有那么多天才,他只是比你多看了几遍书。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 30
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值