spring-data-elasticsearch使用筆記


使用spring-data遇到了一些問題,記錄一下。

spring-data-elasticsearch版本選擇

這里有一份官方github上的spring-data-elasticsearch與elasticsearch的對應關系表,但是不太完整,但是還是比較有參考價值的

spring data elasticsearch elasticsearch
3.0.0.RC2 5.5.0
3.0.0.M4 5.4.0
2.0.4.RELEASE 2.4.0
2.0.0.RELEASE 2.2.0
1.4.0.M1 1.7.3
1.3.0.RELEASE 1.5.2
1.2.0.RELEASE 1.4.4
1.1.0.RELEASE 1.3.2
1.0.0.RELEASE 1.1.1

elasticsearch的客戶端版本必須與服務端版本主版本保持一致。

參考:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html

The client must have the same major version (e.g. 2.x, or 5.x) as the nodes in the cluster. Clients may connect to clusters which have a different minor version (e.g. 2.3.x) but it is possible that new functionality may not be supported. Ideally, the client should have the same version as the cluster.

由於公司使用的elasticsearch 2.1.1,所以選擇了spring-data-elasticsearch 2.1.7.RELEASE(2.1.7對應的原生客戶端版本是elasticsearch 2.4.0,上面表格可能不完整)。

遇到異常java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation

原因是項目使用的springframework版本為4.1.6,而spring-data-elasticsearch 2.1.7默認依賴的spring-context是4.3.11,所以初步確定是我們的項目使用的spring版本太低導致。

參考spring的api文檔,發現原來AnnotatedElementUtils.findMergedAnnotation是4.2版才有的(since 4.2)。

@Field日期類型

    @Field(type = FieldType.Date,
            index = FieldIndex.not_analyzed,
            format = DateFormat.custom,
            pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
    private Date createTime;

參考stackoverflow

@Document中動態indexName

配置Bean

@Component("esConfig")
public class ESConfig {

    @Value("${app.env}")
    private String env;

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }
}

@Document注解

@Document(indexName = "index-#{esConfig.env}", type = "typename", shards = 4, replicas = 1)

參考1
參考2

安裝ik插件

  1. 從官網下載es版本對應版本的ik插件,https://github.com/medcl/elasticsearch-analysis-ik

  2. 我開發環境安裝的es是2.4.1,一開始放在了D:\Program Files\下,結果加入ik插件后就啟動不了了,原來是不支持帶空格的路徑,換了路徑就好了。(參考

  3. 測試

    鏈接:http://localhost:9200/_analyze?analyzer=stardard&pretty=true&text=今天天氣真好

    {
      "tokens" : [ {
        "token" : "今",
        "start_offset" : 0,
        "end_offset" : 1,
        "type" : "<IDEOGRAPHIC>",
        "position" : 0
      }, {
        "token" : "天",
        "start_offset" : 1,
        "end_offset" : 2,
        "type" : "<IDEOGRAPHIC>",
        "position" : 1
      }, {
        "token" : "天",
        "start_offset" : 2,
        "end_offset" : 3,
        "type" : "<IDEOGRAPHIC>",
        "position" : 2
      }, {
        "token" : "氣",
        "start_offset" : 3,
        "end_offset" : 4,
        "type" : "<IDEOGRAPHIC>",
        "position" : 3
      }, {
        "token" : "真",
        "start_offset" : 4,
        "end_offset" : 5,
        "type" : "<IDEOGRAPHIC>",
        "position" : 4
      }, {
        "token" : "好",
        "start_offset" : 5,
        "end_offset" : 6,
        "type" : "<IDEOGRAPHIC>",
        "position" : 5
      } ]
    }
    

    鏈接:http://localhost:9200/_analyze?analyzer=ik&pretty=true&text=今天天氣真好

    {
      "tokens" : [ {
        "token" : "今天天氣",
        "start_offset" : 0,
        "end_offset" : 4,
        "type" : "CN_WORD",
        "position" : 0
      }, {
        "token" : "今天",
        "start_offset" : 0,
        "end_offset" : 2,
        "type" : "CN_WORD",
        "position" : 1
      }, {
        "token" : "天天",
        "start_offset" : 1,
        "end_offset" : 3,
        "type" : "CN_WORD",
        "position" : 2
      }, {
        "token" : "天氣",
        "start_offset" : 2,
        "end_offset" : 4,
        "type" : "CN_WORD",
        "position" : 3
      }, {
        "token" : "真好",
        "start_offset" : 4,
        "end_offset" : 6,
        "type" : "CN_WORD",
        "position" : 4
      } ]
    }
    

head插件安裝和使用

  1. 參考

elasticsearch client api

  1. 通過Query刪除

or and

sql: select * from table where active=1 and ( name like '%?%' or code like '%?%' )
elasticsearch 用java client怎么寫呢?
--------------------------------------
QueryBuilder qb = QueryBuilders.boolQuery()
.must(new QueryStringQueryBuilder("1").field("active"))
.must(QueryBuilders.boolQuery()
    .should(QueryBuilders.matchQuery("name", "小李子"))
    .should(QueryBuilders.matchQuery("code", 小李子"))
);

in

sql: select * from table where name in ('tom', 'john');
QueryBuilder qb = QueryBuilders.boolQuery()
List<String> list = new ArrayList<String>();
list.add("tom");
list.add("john");
BoolQueryBuilder in = QueryBuilders.boolQuery();
for(String name : list) {
   in.shoud(QueryBuilders.matchPhraseQuery("name", name));
}
qb.must(in);

打印dsl日志

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder);
        builder.withPageable(new PageRequest(param.getPageNo(), 20))
                .withSort(new FieldSortBuilder("updateTime").order(SortOrder.DESC));
        SearchQuery searchQuery = builder.build();
        logger.info("QueryDSL:\n{}", searchQuery.getQuery().toString());

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM