美文网首页
ES DSL搜索 - 排序和高亮

ES DSL搜索 - 排序和高亮

作者: 小P聊技术 | 来源:发表于2021-03-12 08:23 被阅读0次

1 介绍

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

统一请求地址:

POST /search_demo/_doc/_search

2 排序

es的排序类似SQL,可以desc也可以asc,同时支持组合排序。

2.1 普通排序

传递JSON数据

  {
    "query": {
        "match": {
            "desc": "好的"
        }
    },
    "sort": [
        {
            "money": "desc"
        }, 
        {
            "age": "desc"
        }  
    ]
  
}

请求结果

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": null,
                "_source": {
                    "id": 1007,
                    "age": 19,
                    "username": "oldBoy",
                    "nickname": "老男孩",
                    "money": 1056.8,
                    "desc": "确实是个很好的组合,筷子 筷子",
                    "sex": 1,
                    "birthday": "1985-05-14",
                    "face": "http://www.p2pi.cn/static/img/1007_face.png"
                },
                "sort": [
                    1056.8,
                    19
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": null,
                "_source": {
                    "id": 1012,
                    "age": 19,
                    "username": "youzi",
                    "nickname": "youzi",
                    "money": 188.8,
                    "desc": "永远的神",
                    "sex": 1,
                    "birthday": "1980-08-14",
                    "face": "http://www.p2pi.cn/static/img/1012_face.png"
                },
                "sort": [
                    188.8,
                    19
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": null,
                "_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"
                },
                "sort": [
                    180.8,
                    31
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": null,
                "_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"
                },
                "sort": [
                    155.8,
                    25
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": null,
                "_source": {
                    "id": 1009,
                    "age": 22,
                    "username": "lucy",
                    "nickname": "露西",
                    "money": 96.8,
                    "desc": "露西是一只很聪明的cat",
                    "sex": 1,
                    "birthday": "1998-07-14",
                    "face": "http://www.p2pi.cn/static/img/1009_face.png"
                },
                "sort": [
                    96.8,
                    22
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": null,
                "_source": {
                    "id": 1001,
                    "age": 18,
                    "username": "Tic",
                    "nickname": "飞翔的荷兰号",
                    "money": 88.8,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
                    "sex": 0,
                    "birthday": "1992-12-24",
                    "face": "http://www.p2pi.cn/static/img/1001_face.png"
                },
                "sort": [
                    88.8,
                    18
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": null,
                "_source": {
                    "id": 1002,
                    "age": 19,
                    "username": "Ailun",
                    "nickname": "进击的巨人",
                    "money": 77.8,
                    "desc": "艾伦是会变成真正的巨人的",
                    "sex": 1,
                    "birthday": "1993-01-24",
                    "face": "http://www.p2pi.cn/static/img/1002_face.png"
                },
                "sort": [
                    77.8,
                    19
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": null,
                "_source": {
                    "id": 1003,
                    "age": 20,
                    "username": "youTuTu",
                    "nickname": "涡轮增压",
                    "money": 66.8,
                    "desc": "极限的速度是需要涡轮增压的",
                    "sex": 1,
                    "birthday": "1996-01-14",
                    "face": "http://www.p2pi.cn/static/img/1003_face.png"
                },
                "sort": [
                    66.8,
                    20
                ]
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": null,
                "_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"
                },
                "sort": [
                    55.8,
                    22
                ]
            }
        ]
    }
}

2.2 对文本排序

由于文本会被分词,所以往往要去做排序会报错,通常我们可以为这个字段增加一个额外的附属属性,类型为keyword,让他不去分词,用于排序。

创建新的索引

{
  "properties": {
    "id": {
      "type": "long"
    },
    "nickname": {
      "type": "text",
      "analyzer": "ik_max_word",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      }
    }
  }
}

排序

{
  "sort": [
    {
      "nickname.keyword": "desc"
    }
  ]
}

3 高亮

传递JSON数据

  • pre_tags:自定义高亮标签的开始
  • post_tags:自定义高亮标签的结束
{
    "query": {
        "match": {
            "desc": "好的"
        }
    },
    "highlight": {
        "pre_tags":["<span>"],
        "post_tags":["</span>"],
        "fields": {
            "desc": {}
        }
    }
  
}

请求结果

{
    "took": 150,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 3.1980762,
        "hits": [
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1004",
                "_score": 3.1980762,
                "_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"
                },
                "highlight": {
                    "desc": [
                        "<span>好</span><span>的</span>系统必须拥有稳定<span>的</span>系统结构"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1005",
                "_score": 3.0979095,
                "_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"
                },
                "highlight": {
                    "desc": [
                        "<span>好</span><span>的</span>游戏,才会有人购买,比如塞尔达"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.37556386,
                "_source": {
                    "id": 1003,
                    "age": 20,
                    "username": "youTuTu",
                    "nickname": "涡轮增压",
                    "money": 66.8,
                    "desc": "极限的速度是需要涡轮增压的",
                    "sex": 1,
                    "birthday": "1996-01-14",
                    "face": "http://www.p2pi.cn/static/img/1003_face.png"
                },
                "highlight": {
                    "desc": [
                        "极限<span>的</span>速度是需要涡轮增压<span>的</span>"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1012",
                "_score": 0.36424035,
                "_source": {
                    "id": 1012,
                    "age": 19,
                    "username": "youzi",
                    "nickname": "youzi",
                    "money": 188.8,
                    "desc": "永远的神",
                    "sex": 1,
                    "birthday": "1980-08-14",
                    "face": "http://www.p2pi.cn/static/img/1012_face.png"
                },
                "highlight": {
                    "desc": [
                        "永远<span>的</span>神"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1002",
                "_score": 0.35254776,
                "_source": {
                    "id": 1002,
                    "age": 19,
                    "username": "Ailun",
                    "nickname": "进击的巨人",
                    "money": 77.8,
                    "desc": "艾伦是会变成真正的巨人的",
                    "sex": 1,
                    "birthday": "1993-01-24",
                    "face": "http://www.p2pi.cn/static/img/1002_face.png"
                },
                "highlight": {
                    "desc": [
                        "艾伦是会变成真正<span>的</span>巨人<span>的</span>"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1011",
                "_score": 0.27665582,
                "_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"
                },
                "highlight": {
                    "desc": [
                        "皮特<span>的</span>姓氏<span>好</span>像是彼得"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1007",
                "_score": 0.2639615,
                "_source": {
                    "id": 1007,
                    "age": 19,
                    "username": "oldBoy",
                    "nickname": "老男孩",
                    "money": 1056.8,
                    "desc": "确实是个很好的组合,筷子 筷子",
                    "sex": 1,
                    "birthday": "1985-05-14",
                    "face": "http://www.p2pi.cn/static/img/1007_face.png"
                },
                "highlight": {
                    "desc": [
                        "确实是个很<span>好</span><span>的</span>组合,筷子 筷子"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1009",
                "_score": 0.252381,
                "_source": {
                    "id": 1009,
                    "age": 22,
                    "username": "lucy",
                    "nickname": "露西",
                    "money": 96.8,
                    "desc": "露西是一只很聪明的cat",
                    "sex": 1,
                    "birthday": "1998-07-14",
                    "face": "http://www.p2pi.cn/static/img/1009_face.png"
                },
                "highlight": {
                    "desc": [
                        "露西是一只很聪明<span>的</span>cat"
                    ]
                }
            },
            {
                "_index": "search_demo",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.18093815,
                "_source": {
                    "id": 1001,
                    "age": 18,
                    "username": "Tic",
                    "nickname": "飞翔的荷兰号",
                    "money": 88.8,
                    "desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
                    "sex": 0,
                    "birthday": "1992-12-24",
                    "face": "http://www.p2pi.cn/static/img/1001_face.png"
                },
                "highlight": {
                    "desc": [
                        "我在p2pi网站解决项目中遇到<span>的</span>问题,学习到了很多知识"
                    ]
                }
            }
        ]
    }
}

4 相关信息

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

相关文章

  • ES DSL搜索 - 排序和高亮

    1 介绍 主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticse...

  • DSL搜索-高亮

    一、搜索结果高亮显示(添加标签)

  • DSL搜索-排序

    一、排序 es的排序同sql,可以desc也可以asc,也支持组合排序 1.数值的排序 2.文本的排序 由于文本会...

  • ES DSL搜索入门

    1 数据准备 1.1 创建索引库 新建一个测试索引库 search_demo 1.2 设置索引的 mapping ...

  • 1、elasticsearch搜索语句

    当把数据用es索引以后,可以通过_search结尾的请求来进行搜索,使用es的DSL在请求体中指定搜索条件可以在请...

  • ES DSL搜索 - prefix、fuzzy和wildcard

    1 介绍 主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticse...

  • es 搜索排序

    为了查找方便,我们对数据进行了索引,放到了es搜索引擎里面。 对于搜索结果我们需要进行排序,比如按时间,按名字等 ...

  • Elasticsearch(ES)的高级搜索(DSL搜索)(下篇

    1. 概述 之前聊了Elasticsearch(ES)的高级搜索(DSL搜索)的一部分内容,今天把剩下的部分聊完。...

  • 44、初识搜索引擎_Query DSL搜索语法

    1、什么是Query DSL 搜索内容在{···}中的就是Query DSL搜索,如: 2、Query DSL的基...

  • ES DSL搜索 - 过滤器

    1 介绍 主要介绍索引请求的基础API操作,使用postman进行请求,接口请求的前缀地址统一为elasticse...

网友评论

      本文标题:ES DSL搜索 - 排序和高亮

      本文链接:https://www.haomeiwen.com/subject/rvvaqltx.html