Elasticsearch筆記

安裝Elasticsearch

安裝Elasticsearch

https://blog.csdn.net/FullStackDeveloper0/article/details/88821537

Elasticsearch6.2.2下載地址

https://www.elastic.co/downloads/past-releases/elasticsearch-6-2-2

學習資料

 慕課網    https://www.imooc.com/video/15762

 BiBi視頻  https://www.bilibili.com/video/av36292270/?p=20

 

安裝kibana插件(推薦)

參考

https://blog.csdn.net/FullStackDeveloper0/article/details/88821951

下載地址

https://www.elastic.co/cn/downloads/past-releases#kibana

 

 

安裝Elasticsearch-head插件

安裝node和npm

yum install -y nodejs npm

將Elasticsearch-head下載下來並且解壓

https://github.com/mobz/elasticsearch-head

 

進入 解壓的目錄 elasticsearch-head-master 下執行 

npm install

 

在目錄 elasticsearch-head-master 下執行啓動es-head

npm run start

 

將Elasticsearch 和 Elasticsearch-head 綁定在一起

修改 /usr/local/elasticsearch-6.2.2/config/elasticsearch.yml文件

在最後添加一下配置

http.cors.enabled: true
http.cors.allow-origin: "*"

後臺啓動 Elasticsearch (esuser用戶)(esuser用戶)(esuser用戶)

 ./bin/elasticsearch -d

啓動Elasticsearch -head

npm run start

 

下下策:在箭頭處自己輸入

 

Elasticsearch數據結構

 

操作

添加

查詢

查詢一個

http://47.105.132.96:9200/people/man/1

查詢全部

http://47.105.132.96:9200/people/man/_search

條件查詢

添加

刪除

SpringBoot用Jest操作Elasticsearch

POM

<dependency>
  <groupId>io.searchbox</groupId>
  <artifactId>jest</artifactId>
  <version>6.3.1</version>
</dependency>

application.properties

spring.elasticsearch.jest.uris=http://47.105.132.96:9200

Entity

package com.example.demo;

import io.searchbox.annotations.JestId;

public class Article {

	@JestId
	private Integer id;
	private String author;
	private String title;
	private String content;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Article(Integer id, String author, String title, String content) {
		this.id = id;
		this.author = author;
		this.title = title;
		this.content = content;
	}

	public Article() {
	}

	@Override
	public String toString() {
		return "Article [id=" + id + ", author=" + author + ", title=" + title + ", content=" + content + "]";
	}

}

 

Controller

package com.example.demo;

import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;

@RestController
public class ArticleController {

	@Autowired
	private JestClient jestClient;

	@RequestMapping("/select")
	public String selectArticle() {

		/*
		 * { "query" : { "match" : { "author" : "zhangsan" } } }
		 */
		String queryStr = "{\n" + "   \"query\" : {\n" + "     \"match\" : {\n" + "       \"author\" : \"zhangsan\"\n"
				+ "    }\n" + "}\n" + "}";

		// 構建搜索功能
		Search index = new Search.Builder(queryStr).addIndex("cbeann").addType("news").build();
		try {
			SearchResult result = jestClient.execute(index);
			return result.getJsonString();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}

	}

	@RequestMapping("/add")
	public String addArticle() {

		Article a = new Article(100, "zhangsan", "請吃飯", "2019年1月1日我請吃飯");

		Index index = new Index.Builder(a).index("cbeann").type("news").build();

		try {
			jestClient.execute(index);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("--------出錯-----------");
		}

		return "success";
	}

	@RequestMapping("/update")
	public String updateArticle() {
		// 添加ID相同的就會覆蓋
		return "success";
	}

	@RequestMapping("/delete")
	public String deleteArticle() {
		// 還未涉及

		return "success";
	}

}

 

 

 

 

SpringBoot用SpringDate操作Elasticsearch(注意:版本對應很重要)

SpringBoot 1.5.12   +   elasticsearch2.4.6

Docker下載2.4.6

docker pull elasticsearch:2.4.6
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myes 5e9d896dcc

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.12.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ElasticsearchJestDemo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
		<!-- 根據你的Elasticsearch 的版本選擇 jest 的版本 ,比如 你的 ES 是6.x.x ,jest的版本可以使 6.x.x -->
		<dependency>
			<groupId>io.searchbox</groupId>
			<artifactId>jest</artifactId>
			<version>6.3.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

application.properties

spring.elasticsearch.jest.uris=http://47.105.132.96:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=47.105.132.96:9300

實體類 

package com.example.demo;

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

@Document(indexName="cbeann",type="books")
public class Book {
	private Integer id;
	private String name;
	private String auther;

	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", auther=" + auther + "]";
	}

	public Book(Integer id, String name, String auther) {
		super();
		this.id = id;
		this.name = name;
		this.auther = auther;
	}

	public Book() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuther() {
		return auther;
	}

	public void setAuther(String auther) {
		this.auther = auther;
	}

}

Repository(類似JPA)

package com.example.demo;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, Integer>{

}

BookController

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/books")
public class BookController {
	
	@Autowired
	private BookRepository bookRepository;
	
	@RequestMapping("/add")
	public String add(){
		Book book=new Book(1, "西遊記", "吳承恩");
		bookRepository.index(book);
		return "success";
	}

}

或者

@RestController
public class OtherController {
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章