ES DSL搜索 - 排序和高亮

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

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

猜你喜欢

转载自blog.csdn.net/qq_15769939/article/details/114684816