package com.tx;
import com.tx.model.Student;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElaticSearchApplicaton.class)
public class ESTest{
// 参考文章 https://www.cnblogs.com/linlf03/p/12828414.html
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**
* 创建索引
*/
@Test
public void createIndexStu(){
Student stu =new Student();
stu.setStuid(1091L);
stu.setName("d的心");
stu.setAge(30);
IndexQuery query = new IndexQueryBuilder().withObject(stu).build();
elasticsearchTemplate.index(query);
}
/**
* 更新索引
*/
@Test
public void updateIndexStu(){
Map<String,Object> source =new HashMap<>();
// source.put("age","100");
// source.put("name","厅阿斯蒂芬");
source.put("sign","I am not a english teacher");
source.put("money",88.8f);
source.put("age","100");
IndexRequest indexRequest = new IndexRequest();
indexRequest.source(source);
UpdateQuery query = new UpdateQueryBuilder().withClass(Student.class).withId("1001").withIndexRequest(indexRequest).build();
elasticsearchTemplate.update(query);
}
/**
* 删除索引
*/
// 注意: 不建议使用ElasticsearchTemplate对索引进行管理(创建索引,更新映射,删除索引)。
//
// 索引就像是数据库或者数据库中的表,我们平时不会通过Java代码频繁的去创建修改删除数据库或者表,之后针对数据做CRUD操作,在es中也是同理,
@Test
public void deleteIndexStu(){
elasticsearchTemplate.delete(Student.class,"1001");
}
/**
* 查询索引
*
*/
@Test
public void getStuInfo(){
GetQuery getQuery = new GetQuery();
getQuery.setId("1001");
Student student = elasticsearchTemplate.queryForObject(getQuery, Student.class);
System.out.println(student);
}
@Test
public void createIndex2(){
for(int i = 0 ; i < 15; i++){
Student stu = new Student();
stu.setStuid(1000L + i);
stu.setName("Nick" + i);
stu.setAge(20 + i);
stu.setMoney(18.9f + i);
stu.setSign("I am a teacher");
stu.setDescription("I am a english teacher " + i);
IndexQuery indexQuery = new IndexQueryBuilder().withObject(stu).build();
elasticsearchTemplate.index(indexQuery);
}
}
@Test
public void searchStuDoc() {
Pageable pageable = PageRequest.of(0, 15);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("sign","a teacher"))
.withPageable(pageable).build();
AggregatedPage<Student> pageStu = elasticsearchTemplate.queryForPage(query, Student.class);
System.out.println("总分页数:" + pageStu.getTotalPages());
List<Student> stuList = pageStu.getContent();
for (Student student : stuList) {
System.out.println(student);
}
}
// @Test
// public void highlightStuDoc(){
// String preTag = "<font color='red'>";
// String postTag = "</font>";
// Pageable pageable = PageRequest.of(0,2);
// SearchQuery query = new NativeSearchQueryBuilder()
// .withQuery(QueryBuilders.matchQuery("sign","a teacher"))
// .withHighlightFields(new HighlightBuilder.Field("sign").preTags(preTag).postTags(postTag))
// .withPageable(pageable).build();
// AggregatedPage<Student> pageStu = elasticsearchTemplate.queryForPage(query, Student.class, new SearchResultMapper() {
// @Override
// public <T> AggregatedPage<T> mapResults(SearchResponse searchResponse, Class<T> aClass, Pageable pageable) {
// List<Student> stuHighlight = new ArrayList<>();
// SearchHits hits = searchResponse.getHits();
// for(SearchHit h: hits){
// HighlightField highlightField = h.getHighlightFields().get("description");
// Integer stuId = (Integer)h.getSourceAsMap().get("stuId");
// String name = (String)h.getSourceAsMap().get("name");
// Integer age = (Integer)h.getSourceAsMap().get("age");
// String sign = (String)h.getSourceAsMap().get("sign");
// Object money = h.getSourceAsMap().get("money");
// //高亮数据
// String description = highlightField.getFragments()[0].toString();
// Student studentHL = new Student();
// studentHL.setDescription(description);
// studentHL.setStuid(Long.parseLong(stuId.toString()));
// studentHL.setName(name);
// studentHL.setAge(age);
// studentHL.setSign(sign);
// studentHL.setMoney(Float.parseFloat(money.toString()));
// stuHighlight.add(studentHL);
// }
// if(!stuHighlight.isEmpty()){
// return new AggregatedPageImpl<>((List<T>) stuHighlight);
// }
// return null;
// }
//
//
// public <T> T mapSearchHit(SearchHit searchHit, Class<T> aClass) {
// return null;
// }
// });
//
// System.out.println("总分页数:" + pageStu.getTotalPages());
// List<Student> stuList = pageStu.getContent();
// for(Student student : stuList){
// System.out.println(student);
// }
//
// }
}
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
@Document(indexName = "student_index",type = "_doc")
public class Student {
@Id
private Long stuid;
@Field(store = true)
private String name;
@Field(store = true)
private Integer age;
@Field(store = true)
private float money;
@Field(store = true)
private String sign;
@Field(store = true)
private String Description;
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public Long getStuid() {
return stuid;
}
public void setStuid(Long stuid) {
this.stuid = stuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"stuid=" + stuid +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
问题:springframework.data.elasticsearch.ElasticsearchException: failed to map source
添加一个无参构造方法解决
public Item() { }