Elasticsearch采坑实践总结

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch采坑实践总结

【1】java.lang.AbstractMethodError

异常如下:


org.elasticsearch.transport.TcpTransport.connectToChannels(Lorg/elasticsearch/cluster/node/DiscoveryNode;Lorg/elasticsearch/transport/ConnectionProfile;)Lorg/elasticsearch/transport/TcpTransport$NodeChannels;

这种类似错误,通常是由于版本问题造成的。这里es版本是5.2.2,自动引入的netty版本是5.6.8,那么降低transport-netty4-client版本与elasticsearch保持一致即可。


pom如下:

   <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.2.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>transport-netty4-client</artifactId>
                    <groupId>org.elasticsearch.plugin</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
            <version>5.2.2</version>
        </dependency>

【2】 java.lang.IllegalArgumentException:Can only use fuzzy queries on keyword and text fields - not on [createDate] which is of type [date]

异常实例:

Caused by: java.lang.IllegalArgumentException: Can only use fuzzy queries on keyword and text fields - not on [createDate] which is of type [date]
  at org.elasticsearch.index.mapper.MappedFieldType.fuzzyQuery(MappedFieldType.java:354)
  at org.elasticsearch.index.query.FuzzyQueryBuilder.doToQuery(FuzzyQueryBuilder.java:341)
  at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:97)
  at org.elasticsearch.index.query.QueryShardContext.lambda$toQuery$1(QueryShardContext.java:312)
  at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:329)
  ... 14 more


原因分析

模糊查询不能适用于type==date 的字段,只能使用于type==keyword|text


【3】java.lang.IllegalArgumentException: Unable to identify index name. Person is not a Document. Make sure the document class is annotated with @Document(indexName=“foo”)

异常实例如下:

异常实例如下:
java


背景原因分析:

@PostMapping("/person")
public String save(@RequestBody Person person) {
    IndexQuery indexQuery = new IndexQueryBuilder()
            .withId(person.getId().toString())
            .withObject(person)
            .build();
    String documentId = elasticsearchOperations.index(indexQuery);
    return documentId;
}

代码中直接用ES操作对象,那么需要对对象添加注解进行映射,Object Mapping,可以点击参考该文档。

解决方案

给Person添加映射注解,如下所示:

@Document(indexName = "person",type = "person")
public class Person {
    @Id
    private Integer id;
    @Field
    private String name;
    //...
}

解决方案

给Person添加映射注解,如下所示:

@Document(indexName = "person",type = "person")
public class Person {
    @Id
    private Integer id;
    @Field
    private String name;
    //...
}

【4】ElasticSearch中 使用 LocalDateTime

实体属性上添加注解:

@Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
7月前
|
存储 监控 安全
大厂案例 - 腾讯万亿级 Elasticsearch 架构实践1
大厂案例 - 腾讯万亿级 Elasticsearch 架构实践
90 0
|
14天前
|
运维 监控 Java
探索Elasticsearch在Java环境下的全文检索应用实践
【4月更文挑战第17天】本文介绍了在Java环境下使用Elasticsearch实现全文检索的步骤。首先,简述了Elasticsearch的功能和安装配置。接着,通过Maven添加`elasticsearch-rest-high-level-client`依赖,创建`RestHighLevelClient`实例连接Elasticsearch。内容包括:创建/删除索引,插入/查询文档。还探讨了高级全文检索功能、性能优化和故障排查技巧。通过Elasticsearch,开发者能高效处理非结构化数据,提升应用程序价值。
|
5月前
|
存储 Java Maven
SpringBoot整合Jest和Elasticsearch实践
SpringBoot整合Jest和Elasticsearch实践
109 1
|
7月前
|
存储 缓存 搜索推荐
百度搜索:蓝易云【Elasticsearch 底层技术原理以及性能优化实践】
和副本、优化硬件、设计合理的索引、编写高效的查询以及利用缓存和预热等策略。通过综合考虑这些方面,可以提升Elasticsearch的性能并获得更好的搜索和分析体验。
282 0
|
3月前
|
存储 JSON 测试技术
异步检索在 Elasticsearch 中的理论与实践
异步检索在 Elasticsearch 中的理论与实践
39 0
|
5月前
|
运维 Kubernetes API
ElasticSearch容器化从0到1实践(一)
通过kubernetes集群聚合多个Elasticsearch集群碎片资源,提高运维效率。
|
7月前
|
存储 Java 数据库
大厂案例 - 腾讯万亿级 Elasticsearch 架构实践2
大厂案例 - 腾讯万亿级 Elasticsearch 架构实践2
36 0
|
9月前
|
SQL AliSQL API
阿里云ElasticSearch安装开源插件实践
阿里云ElasticSearch安装开源插件实践,包含较为常用的NLPchina/elasticsearch-sql与medcl/elasticsearch-analysis-stconvert两个插件的安装、使用测试及同类插件的差异对比
|
11月前
|
SQL 关系型数据库 MySQL
【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(五)
【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(五)
60 0
|
11月前
【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(四)
【ElasticSearch从入门到放弃系列 十一】Elasticsearch常用查询方式讨论及实践(四)
51 0

热门文章

最新文章