其他分享
首页 > 其他分享> > ES elasticserch

ES elasticserch

作者:互联网

ES elasticserch

正向索引和倒排索引

基本概念

分词器

mapping属性

索引库操作

文档操作

RestClient操作索引库

 

```file
PUT /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"
              },
          "pic": {
                  "type": "keyword",
                  "index": "false"
              },
          "location": {
                  "type": "geo_point"
            },
          "all": {
                  "type": "text",
                  "analyzer": "ik_max_word"
                }
          }
     
    }
     
}

 

操作文档

package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static cn.itcast.hotel.constants.HotelIndexConstants.MAPPING_TEMPLATE;
@SpringBootTest
public class HotelIndexDocumentTest {

   @Autowired
   private IHotelService hotelService;

   private RestHighLevelClient client;
   //操作文档
   @Test
   void selectIndexDocument() throws IOException {
       //查询hotel 对象
       Hotel hotel = hotelService.getById(36934L);
       //对象转换location
       HotelDoc hotelDoc = new HotelDoc(hotel);
       //正确的转json:toJSONString
       String json = JSON.toJSONString(hotelDoc);
       //创建文档对象
       IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
       //将对象转化为json
       //request.source(JSON.toJSON(hotelDoc),XContentType.JSON);
       request.source(json,XContentType.JSON);
       //发送
       client.index(request,RequestOptions.DEFAULT);
  }
   @BeforeEach
   void setUp(){
       //初始化
       this.client = new RestHighLevelClient(RestClient.builder(
               HttpHost.create("http://192.168.142.130:9200")
      ));
  }

   @AfterEach
   void deadDown() throws IOException {

       this.client.close();//关闭
  }
}
package cn.itcast.hotel.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

@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();
  }
}

查删改

//查询
   @Test
   void getDocumentByIdTest() throws IOException {
       
       //创建获取对象:指定索引和id
       GetRequest request = new GetRequest("hotel", "36934");
       //获取响应结果
       GetResponse response = client.get(request,RequestOptions.DEFAULT);
       //得到source:json原数据
       String json = response.getSourceAsString();
       //将字符串转换成对象
       HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);

       System.out.println(hotelDoc);
  }
   //更新
   @Test
   void updateDocument() throws IOException {
       //创建对象
       UpdateRequest request = new UpdateRequest("hotel", "36934");
       //准备请求参数
       request.doc(
               "starName","四钻",
               "price","1314"
      );
       //发送请求
       client.update(request,RequestOptions.DEFAULT);
  }
   //删除
   @Test
   void deleteDocument() throws IOException {
       DeleteRequest request = new DeleteRequest("hotel", "36934");
       client.delete(request,RequestOptions.DEFAULT);
  }

批量插入

@Test
   void bulkRequest() throws IOException {

       //数据库查询
       List<Hotel> hotels = hotelService.list();

       BulkRequest request = new BulkRequest();
       for (Hotel hotel : hotels) {
           //转换
           HotelDoc hotelDoc = new HotelDoc(hotel);
           //正确的转Json
           String json = JSON.toJSONString(hotelDoc);
           //批量添加数据
           request.add(new IndexRequest("hotel")
                  .id(hotel.getId().toString())
                  .source(json, XContentType.JSON));
      }
       //发送
       client.bulk(request, RequestOptions.DEFAULT);
  }
 

 

标签:elasticserch,hotel,client,文档,org,import,type,ES
来源: https://www.cnblogs.com/yzlworld/p/16500088.html