接上文讲述了ES环境的安装;本文将继续介绍和springboot整合并实战写demo;
Springboot整合创建一个springboot项目
省略
编写配置4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE com.damon damonyuan-es-api 0.0.1-SNAPSHOT damonyuan-es-api Demo project for Spring Boot 1.8 7.6.1 org.jsoup jsoup 1.10.2 com.alibaba fastjson 1.2.70 org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
)
);
return client;
}
}
实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
}
测试
测试都是在DamonyuanEsApiApplicationTests类中
@SpringBootTest
class DamonyuanEsApiApplicationTests {
@Autowired
public RestHighLevelClient restHighLevelClient;
//创建索引
@Test
public void testCreateIndex() throws Exception{
CreateIndexRequest request = new CreateIndexRequest("damon_index");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());//查看是否成功
System.out.println(response);//查看返回对象
restHighLevelClient.close();
}
//查看索引是否存在
@Test
public void testIndexIsExist() throws Exception{
GetIndexRequest request = new GetIndexRequest("damon_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);//查看是否成功
restHighLevelClient.close();
}
//删除索引
@Test
public void testDeleteIndex() throws Exception{
DeleteIndexRequest request = new DeleteIndexRequest("damon_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());//查看是否成功
restHighLevelClient.close();
}
//创建文档
@Test
public void testAddDocument() throws Exception{
User damon = new User(1,"damon");
IndexRequest request = new IndexRequest("damon_index");//创建请求
request.id("1");//设置id
request.timeout(TimeValue.timeValueMillis(1000));//设置超时时间
request.source(JSON.toJSONString(damon), XContentType.JSON);//将数据存入请求中
IndexResponse response = restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(response.status());//获取建立索引的状态信息
System.out.println(response);
restHighLevelClient.close();
}
//获取文档
@Test
public void testGetDocument() throws Exception{
GetRequest request = new GetRequest("damon_index","1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
restHighLevelClient.close();
}
//查看文档是否存在
@Test
public void testDocumentIsExists() throws Exception{
GetRequest request = new GetRequest("damon_index","1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
restHighLevelClient.close();
}
//更新文档
@Test
public void testUpdateDocument() throws Exception{
UpdateRequest request = new UpdateRequest("damon_index","1");
User user = new User(27,"damon Yuan");
request.doc(JSON.toJSON(user),XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request,RequestOptions.DEFAULT);
System.out.println(response.status());
restHighLevelClient.close();
}
//删除文档
@Test
public void testDeleteDocument() throws Exception{
DeleteRequest request = new DeleteRequest("damon_index","1");
request.timeout("1s");
DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(delete.status());
restHighLevelClient.close();
}
// 查询
// SearchRequest 搜索请求
// SearchSourceBuilder 条件构造
// HighlightBuilder 高亮
// TermQueryBuilder 精确查询
// MatchAllQueryBuilder
// xxxQueryBuilder ...
@Test
public void testSearch() throws Exception{
SearchRequest request = new SearchRequest();
SearchSourceBuilder builder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "damon");
// builder.highlighter(new HighlightBuilder());
// builder.from(0);
// builder.size(5);
builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
builder.query(termQueryBuilder);
SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(JSON.toJSONString(hits));
System.out.println("-------------");
for(SearchHit doc:hits.getHits()){
System.out.println(doc.getSourceAsMap());
}
restHighLevelClient.close();
}
//不能批量添加 后面的把前面的覆盖了
@Test
public void testBulkDocument() throws Exception{
IndexRequest request = new IndexRequest("bulk");
request.source(JSON.toJSONString(new User(1,"ni")),XContentType.JSON);
request.source(JSON.toJSONString(new User(2,"hao")),XContentType.JSON);
request.source(JSON.toJSONString(new User(3,"ma")),XContentType.JSON);
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.status());
restHighLevelClient.close();
}
@Test
public void testBulkDocument2() throws Exception{
BulkRequest request = new BulkRequest();
request.timeout("10s");
List list = new ArrayList<>();
list.add(new User(1,"ni"));
list.add(new User(2,"shi"));
list.add(new User(3,"shui"));
list.add(new User(4,"?"));
for (int i = 0; i < list.size(); i++) {
request.add(new IndexRequest("bulk_index").source(JSON.toJSONString(list.get(i)),XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(bulk.status());
restHighLevelClient.close();
}
}
测试结果可以在head里面查看,此处就不一一体贴图了!
ElasticSearch实战教程中两个项目是分开的;由于本人主要是学习入门,一个项目也方便保存学习,就不新建项目了
项目结构 pom就是前面的,强调一下就是需要注意导入的版本和我们安装的版本一致
application.prepertiesserver.port=9999 spring.thymeleaf.cache=false前端素材
下载地址
链接:https://pan.baidu.com/s/1pmK0uEkdaPBvhfmGY06Nlg
提取码:6666
@Controller
public class IndexController {
@GetMapping({"/","index"})
public String index(){
return "index";
}
}
启动访问
编写工具栏
解析html
public class HtmlParseUtil {
public static void main(String[] args) throws Exception {
System.out.println(parseJD("java"));
// // 请求url
// String url = "https://search.jd.com/Search?keyword=java";
// Document document = Jsoup.parse(new URL(url), 30000);
//
// //获取元素
// Element j_goodsList = document.getElementById("J_goodsList");
// Elements liList = j_goodsList.getElementsByTag("li");
// //System.out.println(liList);
//
// //获取img price name
// for (Element li:liList) {
// String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");//获取li第一张图
// String name = li.getElementsByClass("p-name").eq(0).text();
// String price = li.getElementsByClass("p-price").eq(0).text();
// System.out.println("--------------");
// System.out.println("img:"+img);
// System.out.println("name:"+name);
// System.out.println("price:"+price);
// }
}
public static List parseJD(String keyword) throws Exception{
// 请求url
String url = "https://search.jd.com/Search?keyword="+keyword;
Document document = Jsoup.parse(new URL(url), 30000);
//获取元素
Element j_goodsList = document.getElementById("J_goodsList");
Elements liList = j_goodsList.getElementsByTag("li");
//System.out.println(liList);
List list = new ArrayList<>();
//获取img price name
for (Element li:liList) {
String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");//获取li第一张图
String name = li.getElementsByClass("p-name").eq(0).text();
String price = li.getElementsByClass("p-price").eq(0).text();
// System.out.println("--------------");
// System.out.println("img:"+img);
// System.out.println("name:"+name);
// System.out.println("price:"+price);
Goods goods = new Goods(img,name,price);
list.add(goods);
}
return list;
}
}
用main 方法可以查看到es-head中填入了很多数据
至于节点为啥是那个几个可以在JD上面审查元素查看
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods implements Serializable {
private static final long serialVersionUID = -8049497962627482693L;
private String img;
private String name;
private String price;
}
ContentService
@Service
public class ContentService {
@Autowired
private RestHighLevelClient restHighLevelClient;
public Boolean parseContent(String keywords) throws Exception{
List list = HtmlParseUtil.parseJD(keywords);
BulkRequest request = new BulkRequest();
request.timeout("2m");
for (int i = 0; i < list.size(); i++) {
request.add(new IndexRequest("jd_goods_index")
//.id(""+(i)) 随机id
.source(JSON.toJSONString(list.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
restHighLevelClient.close();
return !bulk.hasFailures();
}
public List
追加控制器
@RestController
public class GoodsController {
@Autowired
private ContentService contentService;
@GetMapping("/parse/{keyword}")
public Boolean parse(@PathVariable("keyword")String keyword) throws Exception{
return contentService.parseContent(keyword);
}
@GetMapping("/search/{keyword}/{pageNum}/{pageSize}")
public List> search(@PathVariable("keyword")String keyword,
@PathVariable("pageNum")Integer pageNum,
@PathVariable("pageSize")Integer pageSize) throws Exception{
return contentService.search(keyword,pageNum,pageSize);
}
}
resource
下载并引入vue和axios
npm install vue npm install axios
resource下放入相应资源
狂神说Java-ES仿京东实战
启动9999访问
输入访问



