Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
Es
ik分词器与Es集成 查询方式:0)id 1) trim 2) query_string
安装 Es 将IK 解压后的ElasticSearch 文件夹放到 Es 中 Plugin 里
ik分词器使用时 在请求路径下 添加参数 ?analyzer=ik_smart&text=交友网站
Es 原生api
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
public class ElasticSearchClientTest {
private TransportClient client;
@Before
public void init() throws Exception {
//1.创建一个settings对象,相当于是是配置信息
Settings builder = Settings.builder()
.put("cluster.name", "my-elasticsearch").build();
//2.创建一个客户端对象
client = new PreBuiltTransportClient(builder);
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300)
);
}
//创建索引库
@Test
public void createIndex() throws UnknownHostException {
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9301)
);
//3.创建client对象创建一个索引库
client.admin().indices().prepareCreate("index-hello1")
//执行操作
.get();
//4.关闭client
client.close();
}
//使用java客户端设置mapping
@Test
public void setMappings() throws Exception {
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300)
);
client.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9301)
);
//创建一个Mappings信息
XContentBuilder builders = XContentFactory.jsonBuilder()
.startObject() //{
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type", "integer").field("store", "yes")
.endObject()
.startObject("title")
.field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
.endObject()
.startObject("content")
.field("type", "string").field("store", "yes").field("analyzer", "ik_smart")
.endObject()
.endObject()
.endObject()//}
.endObject();
// 创建映射
/* PutMappingRequest mapping = Requests.putMappingRequest("index-hello")
.type("article").source(builders);
client.admin().indices().putMapping(mapping).get();*/
client.admin().indices().
//设置要映射的索引
preparePutMapping("index-hello1")
//设置要做映射的type
.setType("article")
//mapping信息,可以是XContentBuilder对象可以是json格式的字符串
.setSource(builders)
.get();
//释放资源
client.close();
}
//添加文档
/*
步骤:
1>创建一个Settings对象
2>创建一个Client对象
3>创建一个文档对象,创建一个json格式的字符串或者使用XContertBuilder
4>使用client对象添加到索引库中
*/
@Test
public void testAddDocument() throws Exception {
//创建一个client对象
//创建一个文档对象
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id", 1)
.field("title", "ElasticSearch是一个基于Lucene的搜索服务器")
.field("content",
"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。")
.endObject();
//把文档对象添加到索引库
client.prepareIndex().setIndex("index-hello1")
// 设置索引名称
.setType("article")
.setId("1")
//执行文档信息
.setSource(builder)
//执行操作
.get();
}
@Test
public void testAddDocument2() throws JsonProcessingException {
//创建一个article对象
Article article = new Article();
//设置对象的属性
article.setId(2l);
article.setTitle("逃不过不放手");
article.setContent("化工太难过哈哈哈哈哈");
//将Article对象转换为json格式的字符串,或者使用XContentBuilder
ObjectMapper objectMapper = new ObjectMapper();
String jsonDoc = objectMapper.writeValueAsString(article);
System.out.println(jsonDoc);
//使用Client对象吧文档添加到索引库中
client.prepareIndex("index-hello1", "article", "2").setSource(jsonDoc, XContentType.JSON).get();
//关闭client
client.close();
}
//实现搜索,
//1. 根据id搜索,
//2.根据term(关键词)进行查询
//3.QueryString查询方式(带分析的查询)
/*
创建一个client对象
创建一个查询对象,可以使用QueryBuilds工具类
使用client执行查询
*/
@Test
public void testSearchById() throws Exception {
//创建一个client对象
//创建一个查询对象根据id查询,可以查询多个id
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
//执行查询 ↓ 可抽取为一个方法 search(queryBuilder)
SearchResponse searchBuilder = client.prepareSearch("index-hello1").setTypes("article").setQuery(queryBuilder).get();
//取查询结果
SearchHits searchHits = searchBuilder.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数" + searchHits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
String sourceAsString = searchHit.getSourceAsString();
//打印文档json对象
System.out.println(sourceAsString);
//文档的属性
System.out.println("-----文档的属性");
Map<String, Object> document = searchHit.getSource();
System.out.println(document.get("id"));
System.out.println(document.get("title"));
System.out.println(document.get("content"));
}
//关闭client
client.close();
}
//utils 提取search
private void search(QueryBuilder queryBuilder) throws Exception {
//执行查询
SearchResponse searchBuilder = client.prepareSearch("index-hello1")
.setTypes("article").setQuery(queryBuilder)
//设置分页信息
.setFrom(0)
.setSize(5)
.get();
//取查询结果
SearchHits searchHits = searchBuilder.getHits();
//取查询结果的总记录数
System.out.println("拆查询结果总记录数" + searchHits.getTotalHits());
//查询结果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
String sourceAsString = searchHit.getSourceAsString();
//打印文档对象
System.out.println("文档对象的json形式:" + sourceAsString);
//文档的属性
System.out.println("-----文档的属性----------");
Map<String, Object> document = searchHit.getSource();
System.out.println("id--->"+document.get("id"));
System.out.println("title--->"+document.get("title"));
System.out.println("content--->"+document.get("content"));
}
//关闭client
client.close();
}
//根据term查询
@Test
public void testQueryByTerm() throws Exception {
//创建一个queryBuild
//1.搜索字段,搜索关键词
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("title", "放手");
//使用search 执行查询,传入queryBuilder
search(queryBuilder);
}
@Test
public void testQueryStringQuery() throws Exception {
//创建一个QueryBuilder对象
QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery("哈哈").defaultField("content");
search(queryBuilder);
}
//分页数据
//在执行查询之前,设置分页就行,使用设置高亮的search方法
@Test
public void testQueryStringQueryPage() throws Exception {
QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery("哈哈").defaultField("content");
search(queryBuilder, "content");
}
/**
* 高亮显示
* @throws JsonProcessingException
*/
private void search(QueryBuilder queryBuilder, String highlight) throws Exception {
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field(highlight);
highlightBuilder.preTags("<em>");
highlightBuilder.postTags("</em>");
//执行查询
SearchResponse searchBuilder = client.prepareSearch("index-hello").setTypes("article").setQuery(queryBuilder)
//设置分页信息
.setFrom(0)
.setSize(5)
//高亮显示
.highlighter(highlightBuilder)
.get();
//取查询结果
SearchHits searchHits = searchBuilder.getHits();
//取查询结果的总记录数
System.out.println("查询结果总记录数" + searchHits.getTotalHits());
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
//查询结果列表
Iterator<SearchHit> iterator = searchHits.iterator();
while (iterator.hasNext()) {
SearchHit searchHit = iterator.next();
String sourceAsString = searchHit.getSourceAsString();
//打印文档对象
System.out.println("文档的json格式:-->"+sourceAsString);
//文档的属性
System.out.println("-----文档的属性");
Map<String, Object> document = searchHit.getSource();
System.out.println("id:"+document.get("id"));
System.out.println("title:"+document.get("title"));
System.out.println("content:"+document.get("content"));
Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
System.out.println("*****高亮结果******"+highlightFields); //获取的是一个k,v格式的map k为查询的字段 v为高亮后的内容
//取高亮显示的结果
HighlightField field = highlightFields.get(highlight);
Text[] fragments = field.getFragments(); //高亮显示的片段
if (fragments != null) {
String title = fragments[0].toString();
System.out.println("高亮结果后的:" + title);
System.out.println("++++++++++++++++++++++++++++++++++++++++");
}
}
//关闭client
client.close();
}
//批量添加数据
@Test
public void testAddDocument3() throws JsonProcessingException {
//创建一个article对象
Article article = new Article();
for (int i = 4; i < 100; i++) {
//设置对象的属性
article.setId(i);
article.setTitle("逃不过不放手" + i);
article.setContent("化工太难过哈哈哈哈哈" + i);
//将Article对象转换为json格式的字符串,或者使用XContentBuilder
ObjectMapper objectMapper = new ObjectMapper();
String jsonDoc = objectMapper.writeValueAsString(article);
System.out.println(jsonDoc);
//使用Client对象吧文档添加到索引库中
client.prepareIndex("index-hello1", "article", i + "").setSource(jsonDoc, XContentType.JSON).get();
}
//关闭client
client.close();
}
}
public class Article {
private long id;
private String title;
private String content;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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;
}
}
``