Elastic Search 学习笔记

Elastic Search 学习笔记

是基于luence来实现的.
es 是面向文档的,一个elasticsearch相当于一个关系型数据库,而一个indices相当于一个数据库,一个Types相当于一个表,一个Documents相当于一行,一个fields相当于一列.

使用Java客户端来管理ES

创建索引库

  1. 创建一个Java工程
  2. 添加jar包
  3. 编写测试方法实现创建索引库
    1)创建一个Settings对象,相当于是一个配置信息,主要是配置集群的名称.
    2)创建一个Client对象
    3)使用client对象创建一个索引库
    4)关闭client对象.

引入依赖

        <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>7.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.12.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.28</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.28</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

使用Java客户端设置Mappings(mapping相当于一种映射关系,类似于数据库的建表语句)

  1. 创建一个Settings对象
  2. 创建一个Client对象
  3. 创建一个mapping信息,应该是一个json数据,可以是字符串,也可以是XContentBuilder对象.
  4. 使用client向es服务器发送mapping信息
  5. 关闭client对象.

使用Java客户端新增文档

  1. 创建一个Settings对象
  2. 创建一个Client对象
  3. 创建一个文档对象,创建一个json字符串,或者使用XContentBuilder
  4. 使用client对象把文档添加到索引库中
  5. 关闭client

引入依赖

 <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>7.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.0.pr3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0.pr3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.0.pr3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.0.5.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.plugin</groupId>
                    <artifactId>transport-netty4-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

新建一个spring配置文件

(如果用springboot的话,从网上找一下写配置类的方法)

管理索引库

  1. 创建一个Entity类,其实就是一个JavaBean(pojo),需要添加一些注解进行标注,映射到一个type(类似于数据库的表)上.
    在类上加的注解有,@Document(indexName="索引的名称", type = "type的名称")
    是ID的就加这个@Id id和所有属性都加这个@Field(type =Field.各种类型, store = true, analyzer = 分词器)
  2. 创建一个dao,是一个接口,需要继承ElasticSearchRepository接口,这个接口带个范型,就像springdatajpa一样,第一个范型是pojo的类型,第二个范型是id的类型.
  3. 编写测试代码.
    要注入你写的dao,以及注入 ElasticsearchTemplate,就叫他 template吧.
@Autowired
private MyDaoRepository mydao;
@Autowired
private ElasticsearchTemplate template;

@Test
public void createIndex() throws Exception {
    //创建索引,并配置映射关系
    template.createIndex(MyPojo.class);
}

向索引添加以及删除和更新文档

mydao由于继承了ElasticSearchRepository接口,所以自带好多基本方法,直接拿来用即可.(spring大法万岁)

  1. 创建一个MyPojo对象,向索引库中添加文档
public void addDocument() throws Exception{
    //创建一个MyPojo对象
    MyPojo mypojo = new MyPojo();
    //对mypojo进行初始化
    //添加文档(没错就是这么简单)
    mydao.save(mypojo);

    //想要删除文档的话,直接调用deleteById()方法即可
    mydao.deleteById(mypojo.id);
    //想要更新的话,其实就是直接添加新的就好.他会自动删除旧的.保证ID一致即可.
}

springdataes查询

直接使用接口提供的方法即可.
有findAll()方法查询所有数据.还有findById()方法根据ID来查询数据.

自定义查询方法.

类似于jpa,他同样有自定义方法名字,而不需要写方法.如果不设置分页信息,默认带分页,每页显示10条信息,如果设置分页信息,应该在方法中添加一个参数Pageable
Pageable pageable = PageRequest(1, 15);设置分页信息,默认从0页开始.

使用原生的查询条件查询

  1. 创建一个NativeSearchQuery对象,设置查询条件
  2. 使用ElasticSearchTemplate对象执行查询
  3. 取查询结果
public void testNativeSearchQuery() throws Exception{
    //创建一个查询对象
    NativeSearchQuery query = new NativeSearchQueryBuilder()
        .withQuery(QueryBuilders.queryStringQuery("你要查询的关键字").defaultFieldId("你要查询的字段名"))
        .build();
    //执行查询并打印结果
    template.queryForList(query, MyPojo.class).forEach(System.out::println);
}
posted @ 2019-12-30 18:06  时光轻轻吹  阅读(202)  评论(0)    收藏  举报