本文共 5869 字,大约阅读时间需要 19 分钟。
sudo docker pull elasticsearch : 2.4.6
sudo docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES_2.4.6 0b44dca495a2
io.searchbox jest 5.3.3
spring.elasticsearch.jest.uris=http://192.168.93.137:9200
package com.example.demo;import java.io.IOException;import javax.management.Query;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.example.ElasticsearchApplication;import com.example.bean.Article;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.core.SearchResult;@RunWith(SpringRunner.class)@SpringBootTest(classes = ElasticsearchApplication.class)public class ElasticsearchApplicationTests { @Autowired JestClient jestClient; @Autowired Article article; /* * 创建检索内容 */ @Test public void contextLoads() throws IOException { /* * huang 是索引, 相当于数据库 * yu 是类型, 相当于表 * 999 是ID, 相当于主键 * 每条内容相当于一条数据 */ //创建检索 Index index = new Index.Builder(article).index("huang").type("yu").id("999").build(); //执行后返回添加的信息 JestResult r = jestClient.execute(index); System.out.println(r.getJsonString()); } /* * 创建搜索条件语句 */ @Test public void contextLoads02() throws IOException { //搜索的条件语句 String query = "{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"author\" : \"听大野\"\n" + " }\n" + " }\n" + "}"; Search search = new Search.Builder(query).addIndex("huang").addType("yu").build(); //执行并返回 SearchResult result = jestClient.execute(search); System.out.println(result.getJsonString()); } }
huang 是索引, 相当于数据库 yu 是类型, 相当于表 999 是ID, 相当于主键 每条内容相当于一条数据
//条件语句 String query = "{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"author\" : \"听大野\"\n" + " }\n" + " }\n" + "}";
org.springframework.boot spring-boot-starter-data-elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearchspring.data.elasticsearch.cluster-nodes=192.168.93.137:9300
package com.example.bean;import org.springframework.boot.context.properties.ConfigurationProperties;//import org.springframework.data.elasticsearch.annotations.Document;//import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.stereotype.Component;//import io.searchbox.annotations.JestId;@Component//@ConfigurationProperties(prefix="huang")@Document(indexName="huang", type="articl")public class Article { private int id; private String author; private String title; private String content; public int getId() { return id; } public void setId(int 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; } @Override public String toString() { return "Article [id=" + id + ", author=" + author + ", title=" + title + ", content=" + content + "]"; } }
package com.example.respersity;import java.util.List;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import com.example.bean.Article;public interface authorRespersity extends ElasticsearchRepository{ public List findByAuthorLike(String article); }
package com.example.demo;import java.io.IOException;import java.util.List;import javax.management.Query;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.example.ElasticsearchApplication;import com.example.bean.Article;import com.example.respersity.authorRespersity;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.core.SearchResult;@RunWith(SpringRunner.class)@SpringBootTest(classes = ElasticsearchApplication.class)public class ElasticsearchApplicationTests { @Autowired authorRespersity ArticleRespersity; @Test public void test03(){ Article articl = new Article(); articl.setAuthor("zzzzzzz大神"); articl.setContent("牛逼"); articl.setId(3333); articl.setTitle("嗯嗯..."); ArticleRespersity.index(articl); //查询索引 Listlist = ArticleRespersity.findByAuthorLike("zzz"); for(Article a : list) System.out.println(a); } }
转载地址:http://fyern.baihongyu.com/