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.lang.IllegalArgumentException: Unable to identify index name. Person is not a Document. Make sure the document class is annotated with @Document(indexName="foo")
	at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.getPersistentEntityFor(ElasticsearchRestTemplate.java:1527) ~[spring-data-elasticsearch-3.2.3.RELEASE.jar:3.2.3.RELEASE]
	at bleHandlerMethod.java:138) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
//...

背景原因分析:

	 @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;
    //...
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章