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 相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/114738522
今日推荐