【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;