Springboot+Elasticsearch+IK分詞器實現全文檢索(2)

Springboot+Elasticsearch+IK分詞器實現全文檢索(2)

logstash實時同步數據到Elasticsearch中

logstash下載地址[\[https://blog.csdn.net/weixin_37281289/article/details/101483434\]](%5Bhttps://blog.csdn.net/weixin_37281289/article/details/101483434%5D)

官網下載地址[https://www.elastic.co/cn/downloads/elasticsearch](https://www.elastic.co/cn/downloads/elasticsearch)

下載完成后打開config文件,在config文件下新建mysql.conf文件用于與mysql數據庫進行連接

內容如下

```java

input{

jdbc{

? ? ? ? ? #jdbc驅動包位置

? ? ? ? ? jdbc_driver_library =>"/Users/zhang/Downloads/elasticsearch/logstash-6.3.2/mysql-connector-java-8.0.16.jar"

? ? ? ? ? #要使用的驅動包類,有過開發經驗的都應該知道這個

? ? ? ? ? jdbc_driver_class =>"com.mysql.jdbc.Driver"

? ? ? ? ? #mysql數據庫連接信息

? ? ? ? ? jdbc_connection_string =>"jdbc:mysql://127.0.0.1:3306/abcd"

? ? ? ? ? #mysql用戶名

? ? ? ? ? jdbc_user =>"root"

? ? ? ? ? #mysql密碼

? ? ? ? ? jdbc_password =>"自己到密碼"

? ? ? ? ? #定時任務,多久執行一次查詢,默認是一分鐘,如何想要沒有延遲,可以使用schedule =>"* * * * *"

? ? ? ? ? schedule =>"* * * * *"

? ? ? ? ? #清空上一次的sql_last_value記錄

? ? ? ? ? clean_run =>true

? ? ? ? ? #你想要執行的語句

? ? ? ? ? statement =>"SELECT * FROM t_abcd WHERE update_time > :sql_last_value AND update_time < NOW() ORDER BY update_time desc"

}

}

output{

elasticsearch{

#es host:port

hosts=>["127.0.0.1:9200"]

#索引

index=>"abcd"

#_id

document_id=>"%{id}"

}

```

在logstash文件到根目錄下引入mysql到驅動程序

啟動命令bin目錄下 輸入logstash -f ../config/mysql.conf//此為mac電腦到命令

查看是否有數據

GET /abcd/_stats

查看是否有數據

POST /abcd/_search

{


}

下載Ik分詞器用于分詞

下載地址 [https://github.com/medcl/elasticsearch-analysis-ik](https://github.com/medcl/elasticsearch-analysis-ik)

[https://github.com/medcl/elasticsearch-analysis-ik/releases](https://github.com/medcl/elasticsearch-analysis-ik/releases)找到對應的版本和es版本一致

下載完成后需要去到es安裝目錄中的plugins目錄中新建一個文件夾,將下載后的ik包解壓后把里面的所有東西都復制到新建的文件夾中即可,之后需要重啟es

```java

第一種方式

POST _analyze

{

? "analyzer": "ik_smart",

? "text": "我是中國人"

}

第二種將詞分為更多的部分

POST _analyze

{

? "analyzer": "ik_max_word",

? "text": "我是中國人"

}

```

ik/config/main.dic中包括很多的詞

springboot+Elasticsearch

主要:springboot的版本不要太高比如我用的是2.2.0的版本,版本太高有報錯

在配置時需要先打開es文件中的elasticsearch.yml文件將my-application這句話的注解打開否則會報錯

springboot application.properties配置

```java

#es配置

spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

spring.data.elasticsearch.cluster-name=my-application

```

實體類配置pojo

```java

import lombok.Data;

import org.springframework.data.annotation.Id;

import org.springframework.data.elasticsearch.annotations.DateFormat;

import org.springframework.data.elasticsearch.annotations.Document;

import org.springframework.data.elasticsearch.annotations.Field;

import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.Column;

import java.util.Date;

@Data

@Document(indexName = "blog",type = "doc",useServerConfiguration = true,createIndex = false)

public class ESBlog {

? ? @Id

? ? private Integer id;

? ? //添加需要分詞的字段用 @Field(type = FieldType.Text,analyzer = "ik_max_word")

? ? @Field(type = FieldType.Text,analyzer = "ik_max_word")

? ? private String title;

? ? @Field(type = FieldType.Text,analyzer = "ik_max_word")

? ? private String author;

? ? @Field(type = FieldType.Text,analyzer = "ik_max_word")

? ? private? String content;

? ? @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")

? ? private Date createTime;

? ? @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")

? ? private? Date updateTime;

}

```

DAO類

```java

public interface EsBlogDao extends ElasticsearchRepository<ESBlog,Integer> {

}

```

Controller類

```java

package com.example.springbootelasticsearch.BlogController;

import com.example.springbootelasticsearch.esdao.EsBlogDao;

import com.example.springbootelasticsearch.espojo.ESBlog;

import com.example.springbootelasticsearch.esservice.ESBlogservice;

import com.example.springbootelasticsearch.mysqlpojo.MysqlBlog;

import com.example.springbootelasticsearch.mysqlservice.MysqlService;

import com.example.springbootelasticsearch.pojvo.Blogvo;

import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;

import org.elasticsearch.index.query.BoolQueryBuilder;

import org.elasticsearch.index.query.QueryBuilders;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;

import org.springframework.util.StopWatch;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import java.util.List;

@RestController

@RequestMapping("/blog")

public class BlogController {

@Autowired

? ? EsBlogDao esBlogDao;

? ? @PostMapping("/search")

? ? public Object search(@RequestBody Blogvo blogvo){

? ? ? ? HashMap<String,Object> map=new HashMap<>();

? ? ? ? StopWatch stopWatch=new StopWatch();

? ? ? ? stopWatch.start();

? ? ? ? String type=blogvo.getType();


? ? ? ? ? ? BoolQueryBuilder builder= QueryBuilders.boolQuery();

? ? ? ? ? ? ? //matchQuery先將搜索詞進行分詞然后再進行查詢只要里面包含任意一個詞都可以被查出

//? ? ? ? ? ? builder.should(QueryBuilders.matchPhraseQuery("title",blogvo.getKeyword()));

//? ? ? ? ? ? builder.should(QueryBuilders.matchPhraseQuery("content",blogvo.getKeyword()));

? ? ? ? ? ? //matchPhraseQuery搜索詞不進行分詞,精準匹配搜索詞,如何不一致則無法進行匹配

? ? ? ? ? ? //相當于or

? ? ? ? ? ? builder.should(QueryBuilders.matchPhraseQuery("title",blogvo.getKeyword()));

? ? ? ? ? ? builder.should(QueryBuilders.matchPhraseQuery("content",blogvo.getKeyword()));

? ? ? ? ? ? Page<ESBlog> search= (Page<ESBlog>) esBlogDao.search(builder);

? ? ? ? ? ? List<ESBlog> content = search.getContent();

? ? ? ? ? ? map.put("list",content);

? ? ? ? stopWatch.stop();

? ? ? ? long totalTimeMillis = stopWatch.getTotalTimeMillis();

? ? ? ? map.put("duration",totalTimeMillis);

? ? ? ? return map;

? ? }

}

```

至此springboot配置完成啟動即可訪問

持續更新java知識。。。。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。