1、Windows系统双击elasticsearch.bat打开服务
2、如果想使用head插件,到head插件路径下,先输入npm install -g grunt -cli,再输入npm install,最后输入grunt server
3、浏览器输入http://localhost:9100/,再到下面输入http://localhost:9200/即可连接
4、如果不使用head插件,使用postman也可以
5、不管是head插件还是postman查询使用起来都有异曲同工之妙
6、postman写入删除举例
7、postman查询举例
8、head插件查询举例
9、 Springboot整合ES方式(一)
- 到start.spring.io上快速构建一个项目,包含有Spring Web和Spring Data ElasticSearch
- 根据需要修改Springboot配置文件,如我的elasticsearch.yml里面改了集群名所以
- 创建一个实体类,实现Serializable接口,并使用@Document注解,字段一定要有一个id主键,可以定义对字段定义搜索规则
package me.lpzz.es.entity;
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;
import java.io.Serializable;
@Document(indexName = "essay",type = "_doc")
public class Essay implements Serializable {
/**主键*/
@Id
@Field(type = FieldType.Integer)
private Integer id;
/**文章标题*/
@Field(type= FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_smart")
private String title;
/**文章内容*/
@Field(type= FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_smart")
private String content;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
- 定义接口,一定带有@Repository注解,并且继承ElasticsearchRepository,泛型里面是索引和主键数据类型
package me.lpzz.es.dao; import me.lpzz.es.entity.Essay; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface EssayRepository extends ElasticsearchRepository<Essay,Integer> { }
- 测试
package me.lpzz.es; import me.lpzz.es.dao.EssayRepository; import me.lpzz.es.entity.Essay; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; 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.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes = EsApplication.class) public class EsTest { @Autowired private EssayRepository essayRepository; @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Test public void testInsert(){ Essay essay=new Essay(); essay.setId(1); essay.setTitle("土巴兔装修1"); essay.setContent("装修就上土巴兔"); essayRepository.save(essay); essay.setId(2); essay.setTitle("土巴兔招聘2"); essay.setContent("Java实习"); essayRepository.save(essay); } @Test public void testInsertAll(){ List<Essay> list=new ArrayList<>(); Essay essay=new Essay(); essay.setId(3); essay.setTitle("土巴兔技术研发部3"); essay.setContent("当你感到不舒服的时候,就是你成长的时候3"); list.add(essay); essay.setId(4); essay.setTitle("土巴兔技术研发部4"); essay.setContent("当你感到不舒服的时候,就是你成长的时候4"); list.add(essay); essayRepository.saveAll(list); } @Test public void testUpdate(){ Essay essay=new Essay(); essay.setId(3); essay.setTitle("土巴兔技术研发部3"); essay.setContent("我在覆盖更新"); essayRepository.save(essay); } @Test public void testDelete(){ essayRepository.deleteById(4); } public void testDeleteAll(){ essayRepository.deleteAll();//删除所有,风险较高 } @Test public void testSearch(){ //多条件分页高亮 SearchQuery searchQuery= new NativeSearchQueryBuilder() .withQuery(QueryBuilders.multiMatchQuery("土巴兔","title","content")) //.withSort(SortBuilders.fieldSort("").order(SortOrder.DESC)) .withPageable(PageRequest.of(0,10)) .withHighlightFields( new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"), new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>") ) .build(); Page<Essay> page=essayRepository.search(searchQuery); for (Essay s:page) { System.out.println("###"+s.getId()+s.getTitle()+s.getContent()); } } }
测试通过