javaAPI操作Elasticsearch

这篇具有很好参考价值的文章主要介绍了javaAPI操作Elasticsearch。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

mapping属性


mapping是对索引库中文档的约束, 常见的mapping属性包括:

  • type: 字段数据类型,常见的简单类型有:
    • 字符串: text(可分词的文本), keyword(精确值, 例如: 品牌,国家)
    • 数值: long, integer, short, byte, double, float
    • 布尔: boolean
    • 日期: date
    • 对象: object
  • index: 是否创建索引, 默认为true
  • analyzer: 使用哪种分词器
  • properties: 该字段的子字段

索引库操作


创建索引库

PUT /zyw
{
  "mappings": {
    "properties": {
      "info": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword",
        "index": false
      },
      "name": {
        "type": "object",
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

查看索引库

GET /zyw
GET /zyw/_mapping
# 查看索引库列表和健康状态
GET /_cat/indices

删除索引库

DELETE /zyw

修改索引库, 添加新字段

索引库和mapping一旦创建无法修改, 但是可以添加新字段

PUT /zyw/_mapping
{
  "properties": {
    "age": {
      "type": "integer"
    }
  }
}

文档操作


新增文档

POST /zyw/_doc/1
{
  "info": "java是最好的语言",
  "email": "zy@163.com",
  "name": {
    "firstName": "云",
    "lastName": "赵"
  }
}

可以不指定id, es会自动生成id

查询文档

GET /zyw/_doc/1

删除文档

DELETE /zyw/_doc/1

修改文档

  • 全量修改, 会删除旧文档, 添加新文档
PUT /zyw/_doc/1
{
  "info": "java是最好的语言",
  "email": "zy@163.com",
  "name": {
    "firstName": "云",
    "lastName": "赵"
  }
}
  • 局部修改
POST /zyw/_update/1
{
  "doc": {
    "email": "test@163.com"
  }
}

RestClient操作索引库


javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

  • 引入依赖
		<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.12.1</version>
        </dependency>

注意:
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
springboot管理了elasticsearch的部分依赖, 查看springboot的依赖管理
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
我们需要在pom文件中定义这个版本值,覆盖springboot的
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

  • HotelDoc.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;

    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();
    }
}
  • hotel索引库
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "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": "ik_max_word"
      }
    }
  }
}

基于elasticsearch的规则, id用keyword

  • 操作索引库
import com.zyw.elasticsearchdemo.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class ElasticsearchDemoApplicationTests {

    private RestHighLevelClient client;


    /**
     * 删除索引库
     */
    @Test
    void deleteHotelIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    /**
     * 判断索引库是否存在
     */
    @Test
    void existHotelIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("hotel");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists ? "索引库已经存在" : "索引库不存在");
    }

    /**
     * 创建索引库
     */
    @Test
    void createHotelIndex() throws IOException {
        // 1.创建request对象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        // 2.准备请求的参数, DSL语句
        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        // 3. 发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}

RestClient操作文档


import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.mapper.HotelMapper;
import com.zyw.elasticsearchdemo.pojo.Hotel;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class ElasticsearchDemoApplicationTests1 {

    private RestHighLevelClient client;

    @Autowired
    private HotelMapper hotelMapper;


    /**
     * 删除文档
     */
    @Test
    void deleteDocument() throws IOException {
        DeleteRequest request = new DeleteRequest("hotel", "200216665");
        client.delete(request, RequestOptions.DEFAULT);

    }

    /**
     * 修改文档-局部更新, 全量和创建一样
     */
    @Test
    void updateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("hotel", "200216665");
        request.doc("price", 2600, "starName", "六钻");
        client.update(request, RequestOptions.DEFAULT);
    }

    /**
     * 查询文档
     */
    @Test
    void getDocument() throws IOException {
        // 准备request对象
        GetRequest request = new GetRequest("hotel", "200216665");
        // 发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }

    /**
     * 新增文档
     */
    @Test
    void addDocument() throws IOException {
        // 根据id查询酒店数据
        Hotel hotel = hotelMapper.selectById(200216665);
        // 转换为文档对象
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 准备request对象
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
        // 准备json文档
        request.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON);
        // 发送请求
        client.index(request, RequestOptions.DEFAULT);
    }

    /**
     * 批量导入文档
     */
    @Test
    void batchAddDocument() throws IOException {
        List<Hotel> hotels = hotelMapper.selectList(null);
        BulkRequest request = new BulkRequest();
        for (Hotel hotel : hotels) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString())
                    .source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON));
        }
        // 发送
        client.bulk(request, RequestOptions.DEFAULT);
    }

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}

DSL查询语法


分类和基本语法

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

全文检索查询

全文检索查询, 会对用户输入内容分词, 常用于搜索框搜索
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
建议把多个字段copy到一个字段里

精确查询

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

地理查询

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

复合查询

  • 复合(compound)查询: 复合查询可以将其他简单查询组合起来, 实现更复杂的搜索逻辑.
    • function score: 复分函数查询, 可以控制文档相关性算分, 控制文档排名. 例如百度竞价

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

搜索结果处理


排序

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

分页

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

高亮

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
默认字段要一致, 可以用require_field_match 取消一致

RestClient查询文档–高级查询


javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Map;

public class QueryDocumentTest {

    private RestHighLevelClient client;

    /**
     * 广告置顶
     */
    @Test
    void adScore() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.termQuery("city", "上海"));
        // 算分控制
        FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(boolQuery, new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAd", true),
                        // 分数10
                        ScoreFunctionBuilders.weightFactorFunction(10))
        }).boostMode(CombineFunction.SUM);  // 用加法 --> 分数+10
        request.source().query(functionScoreQuery);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    /**
     * 高亮
     */
    @Test
    void testHighlight() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().query(QueryBuilders.matchQuery("all", "维也纳"));
        // 高亮设置
        request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    /**
     * 排序和分页
     */
    @Test
    void sortAndPage() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().sort("price", SortOrder.ASC).from(20).size(5);
        request.source().query(QueryBuilders.matchAllQuery());
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    /**
     * 根据地理坐标排序
     */
    @Test
    void sortByLocation() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().sort(SortBuilders.geoDistanceSort("location",
                // 坐标字符串前面是纬度,后面是经度
                new GeoPoint("31.21, 121.5")).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));
        request.source().query(QueryBuilders.matchQuery("all", "如家"));
        // 高亮设置
        request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);

    }

    /**
     * bool查询
     * @throws IOException
     */
    @Test
    void testBool() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 添加term
        boolQuery.must(QueryBuilders.termQuery("city", "上海"));
        // 添加range
        boolQuery.filter(QueryBuilders.rangeQuery("price").lte(500));
        request.source().query(boolQuery);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

	/**
     * bool查询 --should
     * @throws IOException
     */
    @Test
    void testBool() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
        shouldQuery.should(QueryBuilders.matchQuery("name", "上海")).should(QueryBuilders.matchQuery("name","北京"));
        shouldQuery.minimumShouldMatch(1); // name中有上海或者北京,满足一个
        boolQuery.must(shouldQuery);
        // 添加range
        boolQuery.filter(QueryBuilders.rangeQuery("price").lte(180));
        request.source().query(boolQuery);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    /**
     * match查询
     */
    @Test
    void testMatch() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().query(QueryBuilders.matchQuery("all", "如家"));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    /**
     * 处理结果
     * @param response
     */
    private static void handleResponse(SearchResponse response) {
        SearchHits searchHits = response.getHits();
        // 查询的总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("total = " + total);
        // 查询的结果数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            // 得到source
            String json = hit.getSourceAsString();
            HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
            // 获取高亮结果
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            if(!highlightFields.isEmpty()){
                // 根据字段名获取高亮结果
                HighlightField highlightField = highlightFields.get("name");
                // 获取高亮值
                String name = highlightField.getFragments()[0].toString();
                // 覆盖非高亮结果
                hotelDoc.setName(name);
            }
            // 获取location距离排序值 --> 距离4.5km
            Object[] sortValues = hit.getSortValues();
            if (sortValues.length != 0) {
                hotelDoc.setDistance(sortValues[0]);
            }
            System.out.println("hotelDoc = " + hotelDoc);
        }
    }


    /**
     * 查询所有
     * @throws IOException
     */
    @Test
    void testMatchAll() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().query(QueryBuilders.matchAllQuery());
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);
    }

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}

数据聚合


javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

Bucket聚合

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

Metrics聚合

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

  • 指定排序字段
    javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

RestClient实现聚合


javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AggregationTest {

    private RestHighLevelClient client;

    /**
     * 品牌, 城市聚合
     * example --> {品牌=[7天酒店, 如家, 皇冠假日, 速8, 万怡, 华美达, 和颐, 万豪, 喜来登, 希尔顿], 城市=[上海, 北京, 深圳]}
     */
    @Test
    void name() throws IOException {
        Map<String, List<String>> result = new HashMap<>();
        SearchRequest request = new SearchRequest("hotel");
        // TODO  可以增加查询条件过滤,条件和前面一样,对满足的文档进行聚合
        request.source().size(0); // 去掉文档,只看聚合结果
        request.source().aggregation(AggregationBuilders
                .terms("brandAgg") // 名称自己定
                .field("brand")
                .size(10));  // 结果的前十条
        request.source().aggregation(AggregationBuilders
                .terms("cityAgg") // 名称自己定
                .field("city")
                .size(10));  // 结果的前十条
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        // 处理结果
        Aggregations aggregations = response.getAggregations();
        List<String> brandList = getAggByName(aggregations, "brandAgg");
        result.put("品牌", brandList);
        List<String> cityList = getAggByName(aggregations, "cityAgg");
        result.put("城市", cityList);
        System.out.println(result);
    }

    private static List<String> getAggByName(Aggregations aggregations, String aggName) {
        Terms brandterms = aggregations.get(aggName);
        List<? extends Terms.Bucket> buckets = brandterms.getBuckets();
        List<String> list = new ArrayList<>();
        for (Terms.Bucket bucket : buckets) {
            list.add(bucket.getKeyAsString());
        }
        return list;
    }


    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}

拼音分词器


github地址: https://github.com/infinilabs/analysis-pinyin
解压放到elasticsearch插件目录
重启elasticsearch

  • 测试
POST /_analyze
{
  "text": "我们都是祖国的花朵",
  "analyzer": "pinyin"
}

自定义分词器

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
测试自定义分词器

GET /test/_analyze
{
  "text": "java是最好的语言",
  "analyzer": "my_analyzer"
}

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

自动补全

javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

RestApi实现自动补全


javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎
javaAPI操作Elasticsearch,elasticsearch,大数据,搜索引擎

  • 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",
       	  "search_analyzer": "ik_smart"
      }
    }
  }
}
  • HotelDoc.java

增加suggestion字段文章来源地址https://www.toymoban.com/news/detail-844094.html

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@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("/")){
            // business有多个值,切割
            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);
        }
    }
}
  • 查询和解析结果
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

public class SuggestionTest {

    private RestHighLevelClient client;


    /**
     * 自动补全
     */
    @Test
    void test() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().suggest(new SuggestBuilder()
                .addSuggestion("suggestions",  // 名称自定义 解析结果时要与此保持一致
                SuggestBuilders.completionSuggestion("suggestion") // HotelDoc定义的字段
                .prefix("sd")  // 关键字
                 .skipDuplicates(true).size(10))
        );
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        Suggest suggest = response.getSuggest();
        CompletionSuggestion suggestions = suggest.getSuggestion("suggestions"); // 名称和上面一致
        List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
        for (CompletionSuggestion.Entry.Option option : options) {
            String text = option.getText().toString();
            System.out.println(text);
        }
    }

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}

补充


  • 分词
POST /_analyze
{
  "text": "java是最好的语言",
  "analyzer": "ik_smart"
}
  • 查所有
GET /hotel/_search

到了这里,关于javaAPI操作Elasticsearch的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用Elasticsearch进行数据批量操作

    Elasticsearch是一个开源的搜索和分析引擎,基于Lucene库开发。它可以用来实现文本搜索、数据分析、实时数据处理等功能。在大数据时代,Elasticsearch成为了处理和分析大量数据的首选工具之一。 数据批量操作是Elasticsearch中的一种常见操作,它可以用来对大量数据进行创建、更

    2024年02月22日
    浏览(45)
  • Java操作Elasticsearch(新增数据)

    天行健,君子以自强不息;地势坤,君子以厚德载物。 每个人都有惰性,但不断学习是好好生活的根本,共勉! 文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。 首先需要准备好elasticsearch和kibana elasticsearch的下载、安装、使用可参考:Elasticsearch安装

    2024年02月03日
    浏览(38)
  • Spring Data Elasticsearch - 在Spring应用中操作Elasticsearch数据库

    Spring Data Elasticsearch为文档的存储,查询,排序和统计提供了一个高度抽象的模板。使用Spring Data ElasticSearch来操作Elasticsearch,可以较大程度的减少我们的代码量,提高我们的开发效率。 要使用Elasticsearch我们需要引入如下依赖: 还需要在配置文件中增加如下配置 类比于MyBat

    2024年02月14日
    浏览(47)
  • elasticsearch|大数据|elasticsearch的api部分实战操作以及用户和密码的管理

    本文主要内容是通过elasticsearch的api来进行一些集群的管理和信息查询工作,以及elasticsearch用户的增删改查和密码的重设以及重置如何操作 接上文: elasticsearch|大数据|elasticsearch低版本集群的部署安装和安全增强---密码设置问题-CSDN博客 上文主要介绍了elasticsearch低版本集群的

    2024年02月04日
    浏览(43)
  • Java操作Elasticsearch进行数据检索

    1.安装依赖 (注意版本要和自己安装的es版本对应)          打开发现部分依赖和我们es版本不一致,是因为springboot指定了版本,我们需要更换为自己对应版本。 1.1、改为自己es对应版本  2.编写配置类 3.配置类添加请求选项 4、测试 4.1、存储数据到es  4.2、检索数据  

    2024年02月16日
    浏览(43)
  • 常规操作elasticSearch查看和索引(存储)数据

    常规操作elasticSearch: 对于elasticSearch的操作 通常用rest API完成 查看所有节点: 示例返回: 查看健康状态: 示例返回: 查看主节点: 示例返回: 查看所有索引(类比mysql查看所数据库): 示例返回: 保存一条数据 put 保存: 注意: put保存必须有id(唯一识别) 示例返回:

    2023年04月08日
    浏览(46)
  • (Rest风格API)Elasticsearch索引操作、映射配置、数据操作、查询操作

    1.请求方式:put 2.请求路径:索引库名 3.请求参数:json格式 number_of_shards 是指索引要做多少个分片,只能在创建索引时指定,后期无法修改。 number_of_replicas 是指每个分片有多少个副本,后期可以动态修改 什么是分片? ES中所存数据的文件块,也是数据的最小单元块。假如有

    2024年04月26日
    浏览(51)
  • Elasticsearch ES操作:查询数据(全部、分页、单条)

    查询 条件查询 指定条数 返回结果

    2024年02月16日
    浏览(39)
  • (五) ElasticSearch 数据类型和文档CRUD操作

    官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#_complex_datatypes 核心数据类型是 Elasticsearch 最基本和常用的数据类型,用于存储大部分数据。这些核心数据类型包括: Text(文本):用于存储长文本数据,进行全文搜索和分析。 Keyword():

    2024年02月11日
    浏览(39)
  • Spring Boot 中的 Elasticsearch 的数据操作配置

    Elasticsearch是一个基于Lucene的搜索引擎,可以快速地存储、搜索和分析大量的数据。Spring Boot是一个开发框架,提供了快速构建基于Spring的应用程序的工具和技术。在本文中,我们将讨论如何在Spring Boot应用程序中配置Elasticsearch数据操作。 Elasticsearch是一个开源的全文搜索和分

    2024年02月05日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包