在这里只是简单的介绍一下查询,因为这里主要讲的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/

文本查询

match 可以使用 fuzziness 支持纠错查询

{
  "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天"
        }
    }
}

指定namecontent字段,查询

{
    "query":{
        "query_string":{
            "query":"王OR天",
            "fields":[
                "name",
                "content"
            ]
        }
    }
}

字段查询(更新中...)

Last modification:July 12th, 2022 at 04:41 pm