elasticsearch

安装

一、elasticsearch-7.6.1-windows-x86_64安装与配置
0.解压
1.解决跨域问题
    在config\elasticsearch.yml里追加
    http.cors.enabled: true
    http.cors.allow-origin: "*"
2.分词插件安装
	将elasticsearch-analysis-ik-7.6.1.zip拖到elasticsearch-7.6.1\plugins\ik解压出来
3.在bin里找到elasticsearch.bat可直接运行

二、elasticsearch-head-master启动
0.确保安装nodejs并且配置到环境变量
1.npm install
2.npm run start (这里和平时启动不一样)

三、kibana-7.6.1-windows-x86_64安装与配置
0.解压
1.在\config\kibana.yml追加。国际化切换中文配置。
	i18n.locale: "zh-CN"
2.\bin\kibana.bat 运行

在kibana的小扳手里面

词法分析

GET _analyze
{
  "analyzer": "ik_smart",
  "text": "控制台开发工具"
}

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "草草草123"
}

CRUD

PUT class/_doc/1
{
  "name":"穷哈哈",
  "age":24
}

PUT class/_doc/2
{
  "name":"张三",
  "age":21
}

PUT class/_doc/3
{
  "name":"王五",
  "age":32
}

POST class/_doc
{
  "name":"李四",
  "age":64
}

DELETE class/_doc/3

POST class/_update/1
{
  "doc": {
    "age":18
  }
}

GET class/_doc/1
GET class/_search?q=name:李四
GET class/_search?q=name:(张三 or 李四)
GET class/_search?q=name:(NOT 李四)
GET class/_search?q=age:(>=20 AND <=30)
GET class/_search?q=name:*GET class/_search
{
  "from":0,
  "size":2
}

Spring Boot整合ES

创建工程时选中NoSQL中的 Spring Data Elasticsearch

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

配置类

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ESConfiguration {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost",9200,"http")
                )
        );
        return client;
    }
}

傻逼方式一

建实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
}

各种方法

@Autowired
private RestHighLevelClient restHighLevelClient;

/*创建索引*/
@Test
void contextLoads() throws Exception {
    CreateIndexRequest request = new CreateIndexRequest("test_index");
    CreateIndexResponse response = restHighLevelClient.indices()
        .create(request, RequestOptions.DEFAULT);
    System.out.println(response.index());
}

/*判断索引是否存在*/
@Test
void getIndex() throws Exception {
    GetIndexRequest request = new GetIndexRequest("test_index");
    boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println(exists);
}

/*删除索引*/
@Test
void deletIndex() throws Exception {
    DeleteIndexRequest request = new DeleteIndexRequest("test_index");
    AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
    System.out.println(delete.isAcknowledged());
}


/**
     * CRUD文档
     */
/*增加*/
@Test
void add() throws Exception {
    User user = new User(1, "wwy");
    IndexRequest request = new IndexRequest("mytest");
    request.id("1");
    request.source(JSON.toJSONString(user), XContentType.JSON);
    IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
    System.out.println(response.toString());
    System.out.println(response.status());
}

/*查询*/
@Test
void get() throws Exception {
    GetRequest request = new GetRequest("mytest", "1");
    GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
    System.out.println(response.getSourceAsString());
    System.out.println(response.getSource());
}

/*修改*/
@Test
void update() throws Exception {
    UpdateRequest request = new UpdateRequest("mytest", "1");
    User user = new User(1, "李四");
    request.doc(JSON.toJSONString(user), XContentType.JSON);
    UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
    System.out.println(response.status());
}

/*删除*/
@Test
void delete() throws Exception {
    DeleteRequest request = new DeleteRequest("mytest", "1");
    DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
    System.out.println(response.status());
}

牛逼方式二

建实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "order")
public class Order {
    @Id
    private Integer id;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String name;
    private Date date;
}

创建接口

import com.ele.entity.Order;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface EsOrderRepository extends ElasticsearchRepository<Order,Integer> {
}

各种方法

    @Autowired
    private EsOrderRepository esOrderRepository;

    /*添加*/
    @Test
    void newadd() {
        Date date = new Date();
        Order order = new Order(1, "男士背包", date);
        Order save = esOrderRepository.save(order);
        System.out.println(save);
    }

    /*获取*/
    @Test
    void newget() {
        Iterable<Order> all = esOrderRepository.findAll();
        Iterator<Order> iterator = all.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    /*删除*/
    @Test
    void newupdate(){
        esOrderRepository.deleteById(1);
    }

/*还有判断是否存在、统计数量等,但是暂时没发现修改的*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值