目录
1.springboot整合ES
2.进行相关对ES操作
3. 综合案例--京东搜索
1.springboot整合ES
1.1创建一个Springboot工程并加入相关的依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2 创建一个配置,获取ES工具类对象
@Configuration
public class ESConfig {
//该对象可以对我们的ES进行相关的操作
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
return client;
}
}
2.进行相关对ES操作
2.1 操作索引---创建索引
//创建索引
@Test
void contextLoads() throws Exception{
//该类把创建索引的信息都封装到该类中
CreateIndexRequest createIndexRequest=new CreateIndexRequest("qy151-index");
CreateIndexResponse createIndexResponse=client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isShardsAcknowledged());
}
2.2操作索引--删除索引
@Test
//删除索引
public void testDeleteIndex()throws Exception{
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy151-index");
AcknowledgedResponse delete=client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
2.3 索引操作--判断索引是否存在
@Test
//判断索引是否存在
public void testIndexExists()throws Exception{
GetIndexRequest getIndexRequest=new GetIndexRequest("qy151-index");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
2.4 操作文档---添加文档
//添加文档
public void testInsertDoc() throws Exception{
IndexRequest indexRequest=new IndexRequest("qy151-index");
indexRequest.id("1");//指定文档的id
//指定文档的内容:String文档的json内容,XContentType xContentType:以什么格式
indexRequest.source(JSON.toJSONString(new User("张三","北京",22)), XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
}
2.5 查询文档--id
//查询文档--id
@Test
public void testGetDoc() throws Exception{
GetRequest getRequest=new GetRequest("qy151-index");
getRequest.id("1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
String string = getResponse.getSourceAsString();
User user=JSON.parseObject(string,User.class);
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap.get("name"));
}
2.6 判断索引文档是否存在
//判断索引文档是否存在
@Test
public void testDocExist() throws Exception{
GetRequest indexRequest=new GetRequest("qy151-index");
indexRequest.id("2");
boolean exists = client.exists(indexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
2.7 删除文档
@Test
//删除文档
public void testDeleteDoc()throws Exception{
DeleteRequest deleteRequest= new DeleteRequest("qy151-index");
deleteRequest.id("1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.getResult());
}
2.8 修改文档
@Test
//修改文档
public void testUpdateDoc() throws Exception{
UpdateRequest updateRequest=new UpdateRequest("qy151-index","1");
User user=new User();
user.setName("黎明");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update);
}
2.9 批量添加文档
@Test
//批量添加
public void TestBuck()throws Exception{
BulkRequest bulk=new BulkRequest("qy151-index");
List<User> list=new ArrayList<>();
list.add(new User("2","张三","北京",22));
list.add(new User("3","李四","上海",22));
list.add(new User("4","王五","杭州",22));
list.add(new User("5","赵六","广州",22));
list.add(new User("6","孙琪","南京",22));
//list.stream().forEach(item->bulk.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
for(User user:list){
IndexRequest indexRequest=new IndexRequest();
indexRequest.id(user.getId());
indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
bulk.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulk,RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());
}
2.10 复杂查询
@Test
//复杂查询
public void testSearch() throws Exception{
SearchRequest searchRequest=new SearchRequest("qy151-index");
//创建条件对象
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//查询条件 使用TermQueryBuilder工具类创建
TermQueryBuilder matchQuery = QueryBuilders.termQuery("name", "张");
sourceBuilder.query(matchQuery);
//分页
sourceBuilder.from(0);
sourceBuilder.size(1);
//排序
// sourceBuilder.sort("age");
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("<font color='red'>");
highlightBuilder.postTags("</font>");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数:"+searchResponse.getHits().getTotalHits().value);
SearchHit[] hits = searchResponse.getHits().getHits();
Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
}
3. 综合案例--京东搜索
package com.wjk.utils;
import com.wjk.entity.Product;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class HtmlParseUtil {
public static void main(String[] args)throws Exception {
String path="https://search.jd.com/Search?keyword=java";
//Document整个网页
Document document = Jsoup.parse(new URL(path), 30000);
// System.out.println(document);
Element j_goodsList = document.getElementById("J_goodsList");
//System.out.println(j_goodsList);
Elements li = j_goodsList.getElementsByTag("li");
// List<Product> list=new ArrayList<>();
for (Element element:li){
//拿到所有价格
String text = element.getElementsByClass("p-price").eq(0).text();
String pname = element.getElementsByClass("p-name").eq(0).text();
String img = element.getElementsByTag("img").eq(0).attr("data-lazy-img");
System.out.println(img);
}
}
}
?
|