ES实战:黑马旅游案例

 

实现功能

 

ES索引Mappe

PUT /hotel_index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address": {
        "type": "keyword",
        "index": true
      },
      "price": {
        "type": "integer"
      },
      "brand": {
        "type": "keyword",
        "copy_to": "all"   
      },
      "city": {
        "type": "keyword",
        "index": true
      },
      "starName": {
        "type": "keyword",
        "index": true
      },
      "business": {
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "location":{
        "type": "geo_point"
      },
      "all": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "hotelSuggest":{
        "type": "completion",
        "analyzer": "ik_max_word"
      }
    }
  }
}

 

1.合并多个字段的内容:`copy_to`允许你将多个字段的值合并到一个目标字段中。这对于以后进行全文搜索、聚合或排序非常有用。例如,你可以将标题、酒店名称、 品牌、商业地址 字段的值复制到一个名为`all`的目标字段中,以便在搜索时对整个文档内容进行检索

2.控制字段的值是否被索引 : idnex选项。接受 true or false,默认为 true。该参数设置为 false的字段是不能被搜索的。所有数据类型的字段都支持这个参数

3.搜索推荐:suggest  搜索一般都会要求具有“搜索推荐”或者叫“搜索补全”的功能,即在用户输入搜索的过程中,进行自动补全或者纠错。以此来提高搜索文档的匹配精准度,进而提升用户的搜索体验

 

酒店置顶

      // 酒店置顶 增加分数权重
        FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(
                boolQuery,
                new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                QueryBuilders.termQuery("isAD", true),
                                ScoreFunctionBuilders.weightFactorFunction(10)
                        )
                });

 DSL:

POST /hotel_index/_update/433576
{
  "doc": {
    "isAD" :true
  }
}

 

附件酒店:

//  排序
String location = params.getLocation();
if (location != null && !location.equals("")) {
    request.source().sort(SortBuilders
                          .geoDistanceSort("location", new GeoPoint(location))
                          .order(SortOrder.ASC)
                          .unit(DistanceUnit.KILOMETERS));
}

 

智能提示:

    // 自动补全
    public List<String> suggestionList(String prefixText)  {
        // 智能搜索的字段需要使用completion类型
        // 字段值需要是多词条的数组
        List<String> suggestList = new ArrayList<>();
        try {
            SearchRequest request = new SearchRequest(getIndex());
            SuggestBuilder suggestBuilder = new SuggestBuilder().addSuggestion("suggesttions",
                    SuggestBuilders.completionSuggestion("hotelSuggest")
                            .prefix(prefixText)
                            .skipDuplicates(true)
                            .size(10));
            request.source().suggest(suggestBuilder);
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            if(null == response){
                return suggestList;
            }
            CompletionSuggestion completionSuggestion =  response.getSuggest().getSuggestion("suggesttions");
            for (CompletionSuggestion.Entry.Option option : completionSuggestion.getOptions()) {
                String suggesttion = option.getText().toString();
                suggestList.add(suggesttion);
            }
            return suggestList;
        } catch (Exception e) {
            log.error("自动补全异常:",e);
        }
        return suggestList;
    }

 DSL:

GET hotel_index/_search
{
  "suggest": {
    "testSuggest": {
      "text": "",
      "completion": {
        "field": "hotelSuggest"
      }
    }
  }
}

 

 

博客地址参考:

https://blog.csdn.net/qq_45797116/article/details/126714924

https://blog.csdn.net/llg___/article/details/131606389

https://www.zhaojun.ink/archives/1010

 

posted @ 2023-10-15 19:56  爵士灬  阅读(69)  评论(0编辑  收藏  举报