美文网首页elasticsearch
ES搜索补全建议一

ES搜索补全建议一

作者: rainbowz | 来源:发表于2021-12-13 00:41 被阅读0次

    当用户在搜索框输入字符时,我们应该提示出与该字符有关的搜索项,如图


    image.png

    elasticsearch提供了Completion Suggesttion查询实现自动补全。为了提高补全查询的效率,对于文档字段的类型有限制

    • 参与补全查询的字段必须是completetion类型
    • 字段的内容一般是用来补全的多个字条组成的数组
    PUT test
    {
    "mappings": {
    "  properties": {
      "title":{
      "type": "completion"
          }
        }
      }
    }
    
    

    插入下面数据

    // 示例数据
    POST test/_doc
    {
    "title": ["Sony", "WH-1000XM3"]
    }
    POST test/_doc
    {
    "title": ["SK-II", "PITERA"]
    }
    POST test/_doc
    {
    "title": ["Nintendo", "switch"]
    }
    
    

    查询的DSL语句如下

    // 自动补全查询
    GET /test/_search
    {
    "suggest": {
    "title_suggest": {
    "text": "s", // 关键字
    "completion": {
    "field": "title", // 补全查询的字段
    "skip_duplicates": true, // 跳过重复的
    "size": 10 // 获取前10条结果
            }
          }
      }
    }
    
    搜索补全结果

    实现酒店搜索框的自动补全

    1修改hotel索引的索引库结构,设置自定义拼音分词器
    2修改索引库需要被自定义分词字段那么/all
    3索引库添加一个新字段suggestion,类型为completetion类型,使用我们自己自定义分词器
    4给HotelDoc类添加suggestestion字段,内容包含brand、business
    5重新导入数据到hotel库

    酒店索引映射结构
    PUT /hotel
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "text_anlyzer": {
              "tokenizer": "ik_max_word",
              "filter": "py"
            },
            "completion_analyzer": {
              "tokenizer": "keyword",
              "filter": "py"
            }
          },
          "filter": {
            "py": {
              "type": "pinyin",
              "keep_full_pinyin": false,
              "keep_joined_full_pinyin": true,
              "keep_original": true,
              "limit_first_letter_length": 16,
              "remove_duplicated_term": true,
              "none_chinese_pinyin_tokenize": false
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "id":{
            "type": "keyword"
          },
          "name":{
            "type": "text",
            "analyzer": "text_anlyzer",
            "search_analyzer": "ik_smart",
            "copy_to": "all"
          },
          "address":{
            "type": "keyword",
            "index": false
          },
          "price":{
            "type": "integer"
          },
          "score":{
            "type": "integer"
          },
          "brand":{
            "type": "keyword",
            "copy_to": "all"
          },
          "city":{
            "type": "keyword"
          },
          "starName":{
            "type": "keyword"
          },
          "business":{
            "type": "keyword",
            "copy_to": "all"
          },
          "location":{
            "type": "geo_point"
          },
          "pic":{
            "type": "keyword",
            "index": false
          },
          "all":{
            "type": "text",
            "analyzer": "text_anlyzer",
            "search_analyzer": "ik_smart"
          },
          "suggestion":{
              "type": "completion",
              "analyzer": "completion_analyzer"
          }
        }
      }
    }
    
    

    JAVA类

    @Data
    @NoArgsConstructor
    public class HotelDoc {
        private Long id;
        private String name;
        private String address;
        private Integer price;
        private Integer score;
        private String brand;
        private String city;
        private String starName;
        private String business;
        private String location;
        private String pic;
        private Object distance;
        private Boolean isAD;
        private List<String> suggestion;
    
     public HotelDoc(Hotel hotel) {
            this.id = hotel.getId();
            this.name = hotel.getName();
            this.address = hotel.getAddress();
            this.price = hotel.getPrice();
            this.score = hotel.getScore();
            this.brand = hotel.getBrand();
            this.city = hotel.getCity();
            this.starName = hotel.getStarName();
            this.business = hotel.getBusiness();
            this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
            this.pic = hotel.getPic();
    
            // 判断商圈是否包含/
            if (this.business.contains("/")) {
                // 需要切割
                String[] arr = this.business.split("/");
                this.suggestion = new ArrayList<>();
                this.suggestion.add(this.brand);
                Collections.addAll(this.suggestion, arr);
            } else {
                this.suggestion = Arrays.asList(this.brand, this.business);
            }
    
        }
    }
    

    重新导入数据,编写DSL语句


    image.png 测试补全 参考图片 解析语句

    参考
    elasticsearch系列五:搜索详解(查询建议介绍、Suggester 介绍

    相关文章

      网友评论

        本文标题:ES搜索补全建议一

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