淘先锋技术网

首页 1 2 3 4 5 6 7

1. 创建工程

  • 相关依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	<!--<version>2.1.5.RELEASE</version>-->
	<version>2.2.2.RELEASE</version>
</dependency>

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

  • 配置yml
spring:
  data:
    elasticsearch:
      cluster-name: es6
      cluster-nodes: 192.168.1.187:9300

2. 配置yml

  • 版本要和安装Elasticsearch版本一致
    image.png

目前springboot-data-elasticsearch中的es版本贴合为es-6.4.3,如此一来版本需要统一,把es进行降级。等springboot升级es版本后可以在对接最新版的7.4。

3. 版本协调

4. Netty issue fix

  • 如图
    image.png
@Configuration
public class ESConfig {

    /**
     * 解决netty引起的issue
     */
    @PostConstruct
    void init() {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }

}

5. 配置文件

  • elasticsearch6.4.3配置文件
  • elasticsearch.yml
cluster.name: es6
node.name: node0
path.data: /usr/local/elasticsearch-6.4.3/data
path.logs: /usr/local/elasticsearch-6.4.3/logs
network.host: 0.0.0.0

./elasticsearch

  • 如果出现如下错误:
    image.png

  • 那么需要切换到root用户下去修改配置如下

vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096


vim /etc/sysctl.conf
# 添加以下内容
vm.max_map_count=262145

  • 别忘记 sysctl -p 刷新一下
  • 最后再次启动OK

6. 注意

  • 中文分词器也需要去配置一下噢别忘记!:)
  • 中文分词器的版本要记得使用6,版本一定要贴合噢~
  • 比如目前的所有版本都是统一为es-6.4.3,那么下载地址为:
  • https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.4.3

7. 安装包下载

Elasticsearch6.4.3 相关安装包

8. 测试用例

  • pojo
/**
 * @author Wgs
 * @version 1.0
 * @create:2020/07/07
 */
@Document(indexName = "stu",type = "_doc")
public class Stu {
    @Id
    private Integer id;
    @Field(store = true)
    private String name;
    @Field(store = true)
    private Integer age;
    @Field(store = true)
    private Float money;
    @Field(store = true)
    private String sign;
    @Field(store = true)
    private String description;


  • 测试
package com.wanggs.foodiesearch;

/**
 * @author Wgs
 * @version 1.0
 * @create:2020/07/07
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FoodieSearchApplication.class)
public class EsTest {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    // 创建索引
    public void createIndexStu() {
        Stu stu = new Stu();
        stu.setId(101);
        stu.setAge(25);
        stu.setName("李易峰");
        IndexQuery query = new IndexQueryBuilder().withObject(stu).build();
        elasticsearchTemplate.index(query);
    }

    @Test
    // 创建索引
    public void createIndexStus() {
        Stu stu = new Stu();
        stu.setId(105);
        stu.setAge(22);
        stu.setName("spider man");
        stu.setMoney(20.2f);
        stu.setSign("I am spider man");
        stu.setDescription("I with i am spider man");
        IndexQuery query = new IndexQueryBuilder().withObject(stu).build();
        elasticsearchTemplate.index(query);
    }

    @Test
    // 删除索引
    public void deleteIndex() {
        elasticsearchTemplate.deleteIndex(Stu.class);
    }

    @Test
    // 删除数据
    public void deleteStuDoc() {
        elasticsearchTemplate.delete(Stu.class, "101");
    }

    @Test
    // 更新
    public void updateStuDoc() {
        Map<String, Object> param = new HashMap<>();
        param.put("money", 88.6f);
        param.put("sign", "I am not super man");
        param.put("age", 33);

        IndexRequest indexRequest = new IndexRequest();
        indexRequest.source(param);
        UpdateQuery updateQuery = new UpdateQueryBuilder().
                withClass(Stu.class).withId("101").
                withIndexRequest(indexRequest).build();
        elasticsearchTemplate.update(updateQuery);

        // 接口如图 ![image.png](https://upload-images.jianshu.io/upload_images/4994935-f66b99038cc231ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    }

    @Test
    // 获取
    public void getStuDoc() {
        GetQuery getQuery = new GetQuery();
        getQuery.setId("101");
        Stu stu = elasticsearchTemplate.queryForObject(getQuery, Stu.class);
        System.out.println(stu);
    }

    @Test
    // 分页查询
    public void searchStuDoc() {
        Pageable pageable = PageRequest.of(0, 10);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("description", "save man")).
                withPageable(pageable).
                build();
        AggregatedPage<Stu> pageStu = elasticsearchTemplate.queryForPage(searchQuery, Stu.class);
        System.out.println("检索后的总分页数目为: " + pageStu.getTotalPages());
        List<Stu> stuList = pageStu.getContent();
        stuList.forEach(e -> {
            System.out.println(e);
        });
    }

    @Test
    // 分页查询
    public void highlightStuDoc() {
        // 高亮
        String preTag = "<font color='red'>";
        String postTag = "</font>";
        Pageable pageable = PageRequest.of(0, 10);

        // 排序
        SortBuilder sortBuilder = new FieldSortBuilder("money").order(SortOrder.ASC);
        SortBuilder sortBuilderAge = new FieldSortBuilder("age").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("description", "save man")).
                withPageable(pageable).
                withHighlightFields(new HighlightBuilder.Field("description").preTags(preTag).postTags(postTag)).
                withSort(sortBuilder).
                withSort(sortBuilderAge).
                build();
        AggregatedPage<Stu> pageStu = elasticsearchTemplate.queryForPage(searchQuery, Stu.class);
        System.out.println("检索后的总分页数目为: " + pageStu.getTotalPages());
        List<Stu> stuList = pageStu.getContent();
        stuList.forEach(e -> {
            System.out.println(e);
        });
    }
}

image.png