ES DSL搜索 - prefix、fuzzy和wildcard

1 介绍

主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticsearch 部署IP地址+端口号(例如 http://192.168.51.4:9200 。

统一请求地址:

POST /search_demo/_doc/_search

2 prefix

prefix 根据前缀去查询,如下示例:

传递JSON数据

{
  "query": {
    "prefix": {
      "desc": "every"
    }
  }
}

请求结果

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1010",
                "_score": 1.0,
                "_source": {
                    "id": 1010,
                    "age": 30,
                    "username": "cc",
                    "nickname": "cc",
                    "money": 100.8,
                    "desc": " i cc you everyday",
                    "sex": 1,
                    "birthday": "1988-07-14",
                    "face": "http://www.p2pi.cn/static/img/1010_face.png"
                }
            }
        ]
    }
}

3 fuzzy

fuzzy 模糊搜索,并不是指的SQL的模糊搜索,而是用户在进行搜索的时候打字错误现象,搜索引擎会自动纠正,然后尝试匹配索引库中的数据。

官方地址:

https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzzy-match-query.html

传递JSON数据

{
  "query": {
    "multi_match": {
      "fields": ["desc","nickname"],
      "query": "yuo everyady",
      "fuzziness": "AUTO"
    }
  }
}

请求结果

{
    "took": 35,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 4.5765147,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1010",
                "_score": 4.5765147,
                "_source": {
                    "id": 1010,
                    "age": 30,
                    "username": "cc",
                    "nickname": "cc",
                    "money": 100.8,
                    "desc": " i cc you everyday",
                    "sex": 1,
                    "birthday": "1988-07-14",
                    "face": "http://www.p2pi.cn/static/img/1010_face.png"
                }
            }
        ]
    }
}

自动纠正数据,还是可以查询到指定的信息的。

4 wildcard

占位符查询。

  • ? :1个字符
  • *: 1个或者多个字符

官网地址:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

传递JSON数据

{
    "query": {
        "wildcard": {
            "desc": "好*"
        }
    }
}

请求结果

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 1.0,
                "_source": {
                    "id": 1011,
                    "age": 31,
                    "username": "petter",
                    "nickname": "皮特",
                    "money": 180.8,
                    "desc": "皮特的姓氏好像是彼得",
                    "sex": 1,
                    "birthday": "1989-08-14",
                    "face": "http://www.p2pi.cn/static/img/1011_face.png"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 1.0,
                "_source": {
                    "id": 1005,
                    "age": 25,
                    "username": "switch",
                    "nickname": "switch游戏机",
                    "money": 155.8,
                    "desc": "好的游戏,才会有人购买,比如塞尔达",
                    "sex": 1,
                    "birthday": "1989-03-14",
                    "face": "http://www.p2pi.cn/static/img/1005_face.png"
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 1.0,
                "_source": {
                    "id": 1004,
                    "age": 22,
                    "username": "redHat",
                    "nickname": "红帽子",
                    "money": 55.8,
                    "desc": "好的系统必须拥有稳定的系统结构",
                    "sex": 0,
                    "birthday": "1988-02-14",
                    "face": "http://www.p2pi.cn/static/img/1004_face.png"
                }
            }
        ]
    }
}

5 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小P聊技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值