配置application.yml
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端
这是kibana的配置修改
入口类
package cn.itsource.index;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ESApplication {
public static void main(String[] args) {
SpringApplication.run(ESApplication.class);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ESApplication.class)
public class ESTest {
@Autowired
private ElasticsearchTemplate template; //JdbcTemplate
//创建索引,并且建立类型映射
@Test
public void testPre() throws Exception{
//创建索引
elasticsearchTemplate.createIndex(PersonDoc.class);
//类型映射
elasticsearchTemplate.putMapping(PersonDoc.class);
}
personDoc
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* 原来jpa Domain @Entity @Column @ID 建立的是对象与表之间的映射
* 现在ES Doc @Document(index,type) @Id @Feild 建立的是对象与文档直接映射
*/
@Document(indexName = "test",type = "person") //操作的是test索引库下面的person类型的文档
public class PersonDoc {
@Id //文档的id就是对象的id
private Long id;
//没有特殊要求不用@Field
private Integer age;
@Field(type = FieldType.Keyword)
private String name; //zs
@Field(type = FieldType.Text,analyzer = "ik_max_word"
,searchAnalyzer = "ik_max_word")
private String intro; //zs is
//关键字搜索,作用要name和intro字段
//方案1:做组合查询 or 如果字段过多
//方案2:虚拟字段all(所有需要做关键字搜索的值,中间通过空格分隔) zs +" "+ zs is .相当于一个字段代替了很多字段
//模糊查询所有字段
@Field(type =FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String all; //name intro
//进行拼接
public String getAll() {
return this.name+" "+this.intro;
}
public void setAll(String all) {
this.all = all;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
public PersonDoc(Long id, Integer age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "PersonDoc{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
public PersonDoc() {
}
}
PersonDocRepository.java
import cn.itsource.doc.PersonDoc;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface PersonDocRepository extends
ElasticsearchRepository<PersonDoc,Long> {
}
crud
//以后所有的操作都是要通过ElasticSearchTemplate
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private PersonDocRepository repository;
//创建索引库(待会看是否会自动创建)全局映射(暂时) 类型映射 crud(Repository)
@Test
public void init() {
System.out.println(elasticsearchTemplate);
//创建索引
elasticsearchTemplate.createIndex(PersonDoc.class);
//类型映射
elasticsearchTemplate.putMapping(PersonDoc.class);
}
//添加和修改是同一个方法
@Test
public void testAddOrUpdate() {
PersonDoc personDoc = new PersonDoc(1L,18,"zs-edit");
personDoc.setIntro("zs person");
repository.save(personDoc);
}
//批量添加
@Test
public void testAddAll() {
List<PersonDoc> personDocs = new ArrayList<>();
for (int i = 0; i < 50; i++) {
PersonDoc personDoc = new PersonDoc(2L+i,19+i,"zs"+i);
personDoc.setIntro("zs person"+i);
personDocs.add(personDoc);
}
repository.saveAll(personDocs);
}
//删除
@Test
public void testDel() {
repository.deleteById(1L);
}
//获取一个
@Test
public void testGetOne() {
System.out.println(repository.findById(2L));
//repository.findAllById(Arrays.asList(1L,2L));
}
//获取所有
@Test
public void testGetALl() {
System.out.println(repository.findAll().getClass());
}
//dsl分页+高级查询+排序
@Test
public void testDsl (){
//1 创建一个构建器 NativeSearchQueryBuilder
NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//2 设置条件 BoolQuery query bool
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//2.1 must
boolQuery.must(QueryBuilders.matchQuery("all","zs"));
//2.2 filter
boolQuery.filter(QueryBuilders.rangeQuery("age").gte(18).lte(40));
builder.withQuery(boolQuery);
//3 排序
builder.withSort(SortBuilders.fieldSort("id"));
//4 分页 页数从0开始
builder.withPageable(PageRequest.of(0,10));
//5 截取字段
builder.withSourceFilter(new FetchSourceFilter(new String[]{"id","name","age"},
null));
//6 查询并封装结果 - 总数,当前页数据
NativeSearchQuery build = builder.build();
Page<PersonDoc> page = repository.search(build);
System.out.println(page.getTotalElements());
System.out.println(page.getContent());
}