springboot学习笔记(七)集成低版本和高版本Elasticsearch 搜索引擎

历经风雨,翻山越岭,一路坎坷,才发现原来真理就在身旁!

简述坎坷过程:

1.由于项目当中使用的是低版本的elasticsearch 1.x ,使用spring data Elasticsearch 分分钟集成。

只需导入spring-data-elasticsearch的依赖,在配置文件中配置参数即可(如下):

<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-elasticsearch</artifactId>
			<version>1.3.6.RELEASE</version>
</dependency>	


spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

然后建立与搜索引擎相关的实体:

package com.bigdata.lab.ymlib.model.elasticsearch;

import java.io.Serializable;
import java.util.Date;

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

@Document(indexName = "ym_liter_text", type = "ym_liter_text")
public class LiteratureTextSearch implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1982880315717929068L;
	
	private String _id;

	private String literId;

	private long dirId;

	private String filename;

	private long length;

	private String contentType;

	private String md5;

	private String suffix;

	private int pageNo;

	private Date uploadDate;

	private String literImageId;

	// 简体
	private String zhHansContent;

	// 繁体
	private String zhHantContent;
	
	

	public String get_id() {
		return _id;
	}

	public void set_id(String _id) {
		this._id = _id;
	}

	public String getLiterId() {
		return literId;
	}

	public void setLiterId(String literId) {
		this.literId = literId;
	}

	public long getDirId() {
		return dirId;
	}

	public void setDirId(long dirId) {
		this.dirId = dirId;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public long getLength() {
		return length;
	}

	public void setLength(long length) {
		this.length = length;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public Date getUploadDate() {
		return uploadDate;
	}

	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}

	public String getLiterImageId() {
		return literImageId;
	}

	public void setLiterImageId(String literImageId) {
		this.literImageId = literImageId;
	}

	public String getZhHansContent() {
		return zhHansContent;
	}

	public void setZhHansContent(String zhHansContent) {
		this.zhHansContent = zhHansContent;
	}

	public String getZhHantContent() {
		return zhHantContent;
	}

	public void setZhHantContent(String zhHantContent) {
		this.zhHantContent = zhHantContent;
	}
	
	

}
此中索引类似传统数据库的数据库,而类型类似于传统数据库中的表。


然后在业务层中 引入templat


	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

NativeSearchQueryBuilder nsqbText = new NativeSearchQueryBuilder();
		nsqbText.withQuery(qbText);
		
//		if (StringUtils.isEmpty(searchContent)) {
//			// 如果搜索条件为空,翻页以文献为准,则在当前文献集合中拿排好序的前limit页
//			pageable = new PageRequest(0, limit);
//			nsqbText.withPageable(pageable);
//		} else {
//			// 如果有搜索条件,翻页以搜索内容数据为准
//			pageable = new PageRequest(page, limit);
//			nsqbText.withPageable(pageable);
//		}
		
		pageable = new PageRequest(page, limit);
		nsqbText.withPageable(pageable);
		
		
		FieldSortBuilder _score = setFieldSort("_score", "desc");
		FieldSortBuilder sortLiterId = setFieldSort("literId", "desc");
		// FieldSortBuilder sortdirId = setFieldSort("dirId", "asc");
		FieldSortBuilder sortPageNo = setFieldSort("pageNo", "asc");
		nsqbText.withSort(_score);
		nsqbText.withSort(sortLiterId);
		// nsqbText.withSort(sortdirId);
		nsqbText.withSort(sortPageNo);
		SearchQuery searchQueryText = nsqbText.build();
		Page<LiteratureTextSearch> pageLiteraturesText = null;
		try {
			pageLiteraturesText = elasticsearchTemplate.queryForPage(searchQueryText, LiteratureTextSearch.class);
		} catch (Exception e) {
			throw new YMLibWebApplicationException("搜索分类:" + literClassfication + "后根据文献ids集合查找对应的文献文本出错", e);
		}

还可使用仓储类进行搜索:

package com.bigdata.lab.ymlib.repository;

import java.util.List;

import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.bigdata.lab.ymlib.model.elasticsearch.LiteratureSearch;


public interface ElasticsearchLiteratureRepository extends ElasticsearchRepository<LiteratureSearch, String> {
//	@Query("{\"bool\" : {\"must\" : [{\"term\" : {\"title\" : \"?0\"}},{ \"term\" : {\"author\" : \"?1\"} }]}}")
//	public LiteratureSearch findByTitleAndAuthor(String title, String author);
}


然后使用 他提供的方法:

elasticsearchLiteratureRepository.search(searchQuery)


2.想想搜索引擎已经更新到6.1.1版本想升一下elasticsearch版本:

于是在本地部署了一个elasticsearch6.1.1版本,然后使用spring-data-elasticsearchjar包,结果控制台一直爆空指针,令人费解:百度良久:才发现,spring-data-elasticsearch还没更新到最新的搜索引擎版本!而且现在才更新支持到elasticsearch2.1.9,好吧认栽!

查看spring-data-elasticsearch版本地址:

https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch

于是找了官方的java api开始集成如下:

官方文档地址:

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

第一步:导入相关依赖:

<dependency>
           <groupId>org.apache.logging.log4j</groupId>
           <artifactId>log4j-api</artifactId>
           <version>2.9.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.logging.log4j</groupId>
           <artifactId>log4j-core</artifactId>
           <version>2.9.1</version>
       </dependency>
       <dependency>
           <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
           <version>6.1.1</version>
       </dependency>
		
		<dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>transport</artifactId>
           <version>6.1.1</version>
       </dependency>

其中日志的jar 可以不用。

然后开始写案例

1.创建搜索引擎名称(更改了默认引擎名称的话):

Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
TransportClient client = new PreBuiltTransportClient(settings);



(使用默认引擎名称):

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300))   


2.开始执行命令:

首先插入一条数据:

String json = "{" +
			        "\"user\":\"kimchy\"," +
			        "\"postDate\":\"2013-01-30\"," +
			        "\"message\":\"trying out Elasticsearch\"" +
			    "}";

			IndexResponse response_insert = client.prepareIndex("xc", "test")
			        .setSource(json, XContentType.JSON)
			        .get();
			
			System.out.println(response_insert.getResult());

控制台返回:

CREATED

ok 我们用客户端查看一下记录:

GET /xc/test/_search

返回:”

{
  "took": 393,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "xc",
        "_type": "test",
        "_id": "YlIrm2ABcUzl4ssEeJ7X",
        "_score": 1,
        "_source": {
          "user": "kimchy",
          "postDate": "2013-01-30",
          "message": "trying out Elasticsearch"
        }
      }
    ]
  }
}

说明插入成功,并为我们生成了id 

下面我们查询此条数据:

  //搜索数据
			GetResponse response = client.prepareGet("xc", "test", "YlIrm2ABcUzl4ssEeJ7X").get();
			 //输出结果
	         System.out.println(response.getSourceAsString());

返回:

loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
{"user":"kimchy","postDate":"2013-01-30","message":"trying out Elasticsearch"}

ok 测试成功

其他操作:

GetResponse response = client.prepareGet("xc", "test", "YlIrm2ABcUzl4ssEeJ7X").get();
			 //输出结果
	         System.out.println(response.getSourceAsString());
	         
	         //删除
			DeleteResponse response_del = client.prepareDelete("xc", "test", "YlIrm2ABcUzl4ssEeJ7X").get();
			
			System.out.println(response_del.getResult());
			
			//修改
			UpdateRequest updateRequest = new UpdateRequest();
			updateRequest.index("index");
			updateRequest.type("type");
			updateRequest.id("1");
			updateRequest.doc(jsonBuilder()
			        .startObject()
			            .field("gender", "male")
			        .endObject());
			client.update(updateRequest).get();



ok 基本的操作测试完毕,helloword完成!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值