在这里只是简单的介绍一下查询,因为这里主要讲的EasySwoole结合ElasticSearch
实现站内搜索.后面还要封装EasySwoole
的类库,所以.要想深入了解ElasticSearch
的建议去看看官网,通过阅读文档去学习,这里暂时就不再往下深入的讲解了.这tm想用ES简写代替还不行
注意
keyword
只能精确查找,不会分词
准备工作
在之前的演示增删改等操作的时候创建了一些文档,这里对部分文档内容进行修改.便于查询对比~如果没有文档数据的话可以自行创建哦~前面的教程都是有详细步骤的.
创建json流文件
vim es_upd
# 添加
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "1"} }
{ "doc" : {"name" : "二滑大魔王","content":"你好啊"} }
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "2"} }
{ "doc" : {"name" : "二滑小天使","content":"你好啊"}} }
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "3"} }
{ "doc" : {"name" : "滑水","content":"你好啊"}} }
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "4"} }
{ "doc" : {"name" : "123","content":"二了吧唧的"}} }
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "5"} }
{ "doc" : {"name" : "今天星期二"} }
{ "update": { "_index": "small_video", "_type": "_doc", "_id": "6"} }
{ "doc" : {"name" : "滑冰去吗,小二货"} }
{ "delete": { "_index": "small_video", "_type": "_doc", "_id": "7"} }
{ "delete": { "_index": "small_video", "_type": "_doc", "_id": "8"} }
{ "delete": { "_index": "small_video", "_type": "_doc", "_id": "9"} }
请求地址
# 为了方便阅读,我还是给他换行了~
curl -H "Content-Type: application/json" -XPOST
http://127.0.0.1:8101/_bulk\?pretty --data-binary @es_upd
数据展示
通用请求地址
POST
http://localhost:8101/small_video/_doc/_search/
文本查询
{
"query": {
"match": {
"name": {
"query": "usre",
"fuzziness": 1
}
}
}
}
指定字段模糊查询
{
"query": {
"match": {
"name": "二滑大魔王"
}
}
}
返回结果
{
"took":42,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":1.4384104,
"hits":[
{
"_index":"small_video",
"_type":"_doc",
"_id":"1",
"_score":1.4384104,
"_source":{
"name":"二滑大魔王",
"content":"你好啊"
}
},
{
"_index":"small_video",
"_type":"_doc",
"_id":"3",
"_score":0.8405091,
"_source":{
"name":"滑水",
"content":"你好啊"
}
},
{
"_index":"small_video",
"_type":"_doc",
"_id":"5",
"_score":0.5897495,
"_source":{
"name":"今天星期二",
"content":"hello5"
}
},
{
"_index":"small_video",
"_type":"_doc",
"_id":"2",
"_score":0.5753642,
"_source":{
"name":"二滑小天使",
"content":"你好啊"
}
}
]
}
}
指定字段精准查询
{
"query":{
"match_phrase":{
"name":"二滑大魔王"
}
}
}
返回结果
{
"took":19,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":1,
"relation":"eq"
},
"max_score":1.4384103,
"hits":[
{
"_index":"small_video",
"_type":"_doc",
"_id":"1",
"_score":1.4384103,
"_source":{
"name":"二滑大魔王",
"content":"你好啊"
}
}
]
}
}
多字段模糊查询
{
"query":{
"multi_match":{
"query":"二滑大魔王",
"fields":[
"name",
"content"
]
}
}
}
name字段增加2倍权重
{
"query":{
"multi_match":{
"query":"二滑大魔王",
"fields":[
"name^2",
"content"
]
}
}
}
query_string查询
查找既有王
又有使
又有今
{
"query":{
"query_string":{
"query":"王AND使AND今"
}
}
}
查找既有王
又有使
或者有天
{
"query":{
"query_string":{
"query":"(王AND使)OR天"
}
}
}
指定name
和content
字段,查询王
或天
{
"query":{
"query_string":{
"query":"王OR天",
"fields":[
"name",
"content"
]
}
}
}
OωO鸽子的第八个月,评论一下证明我心里还有它