elasticsearch快速入门,一篇就够了,上手使用!

elasticsearch快速入门,一篇就够了,上手使用!_第1张图片

elasticsearch

spring整合es

1.导入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>


<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>2.0.7.graalversion>
dependency>

2.配置类

初始化配置bean,扔到ioc容器内

host链接–>builder–>RestHighLevelClient

@Configuration
public class ElasticSearchConfig {
    
    @Value("${spring.elasticsearch.uris}")
    private String httpPath;

    @Bean
    RestHighLevelClient restHighLevelClient(){
        
        HttpHost host = HttpHost.create(httpPath);//创建httpHost
        RestClientBuilder builder = RestClient.builder(host);//根据host建立此属性的服务器
        RestHighLevelClient client = new RestHighLevelClient(builder);//创建服务器对象
        return client;
    }
    
}

3.测试类

@Autowired
@Qualifier("restHighLevelClient")//避免按名称找不到
private RestHighLevelClient restHighLevelClient;
/**
 *测试
 **/
//创建索引 client.indices().create()
@Test
void TestCreateIndex() throws IOException {
    CreateIndexRequest request = new CreateIndexRequest("user");
    CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    System.out.println(createIndexResponse);
}

//删除索引 client.indices().delete()
@Test
void TestDeleteIndex() throws IOException {
    DeleteIndexRequest request = new DeleteIndexRequest("user2");
    //delete
    AcknowledgedResponse delete = restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
    System.out.println(delete);
}
//添加文档 client.index()
@Test
void TestAddDocument() throws IOException {
    User user = new User();
    user.setUsername("zhangsan");
    user.setAge(20);

    //创建请求
    IndexRequest request = new IndexRequest("user");
    //规则:put /indexName/_doc/1
    request.id("2");
    request.timeout(TimeValue.timeValueSeconds(1));
    request.timeout("5s");

    //数据放入请求
    request.source(JSON.toJSONString(user), XContentType.JSON);
    //向客户端发送请求,获取响应结果
    IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);//这里用的index
    System.out.println(indexResponse.toString());

    //命令返回的状态
    System.out.println(indexResponse.status());
}


//获取文档,判断是否存在 get /index/doc/1
@Test
void TestIfExist() throws IOException {
    GetRequest getRequest = new GetRequest("user","2");
    //请求发送出去,得到响应
    GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
    System.out.println(getResponse.getSourceAsString());//要看的是响应内的资源
    System.out.println(getResponse);
}
//更新文档的信息 updateRequest.doc(实体的json)   client.update(请求)
@Test
void TestUpdateDocument() throws IOException {
    UpdateRequest updateRequest = new UpdateRequest("user","2");
    updateRequest.timeout("5s");
    User user = new User();
    user.setUsername("蟹老板");
    user.setAge(40);
    updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//发送请求
    System.out.println(updateResponse.status());
}

//删除文档信息
@Test
void TestDeleteDocumentInfo() throws IOException {
    DeleteRequest request = new DeleteRequest("user","2");
    request.timeout("5s");
    //发送请求
    DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
    System.out.println(deleteResponse);
}

//批量插入数据
@Test
void TestBulkRequest() throws IOException {
    BulkRequest bulkRequest = new BulkRequest();//存放一堆请求
    bulkRequest.timeout("10s");
    List<User> list = new ArrayList<>();
    list.add(new User("zysheep1", 1));
    list.add(new User("zysheep2", 2));
    list.add(new User("zysheep3", 3));
    list.add(new User("zysheep4", 5));
    list.add(new User("zysheep5", 6));
    list.add(new User("zysheep7", 7));

    for(int i=0;i<list.size();i++){
        bulkRequest.add(new IndexRequest("user").id(""+(i+1)).source(JSON.toJSONString(list.get(i)),XContentType.JSON));
    }

    //发送
    BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    System.out.println(bulkResponse);
}

//查询
/**
 * 查询
 * SearchRequest 搜索请求
 * SearchSourceBuilder 条件构造
 * HighlightBuilder 构建高亮
 * TermQueryBuilder 精确查询
 * MatchAllQueryBuilder匹配所有
 * QueryBuilder 对应我们刚才看到的命令!
 * 查询条件,我们可以使用 QueryBuilders 工具来实现
 * QueryBuilders.termQuery 精确
 * QueryBuilders.matchAllQuery() 匹配所有
 */
@Test
void TestSearch() throws IOException {
    SearchRequest searchRequest = new SearchRequest("user");
    //构建搜索条件,这里且设置高亮
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.highlighter();//高亮
    //精确查询
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("username","zysheep1");

    searchSourceBuilder.query(termQueryBuilder);
    searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    System.out.println(searchResponse.getHits());
    for(SearchHit hit : searchResponse.getHits().getHits()){
        System.out.println(hit.getSourceAsMap());
    }

}

项目实战

1.导入依赖

<dependencies>
    
    
    <dependency>
        <groupId>org.jsoupgroupId>
        <artifactId>jsoupartifactId>
        <version>1.10.2version>
    dependency>
    
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.70version>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-elasticsearchartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-thymeleafartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
        <scope>runtimescope>
        <optional>trueoptional>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-configuration-processorartifactId>
        <optional>trueoptional>
    dependency>
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
dependencies>

2.导入前端素材

链接:https://pan.baidu.com/s/1M5uWdYsCZyzIAOcgcRkA_A
提取码:qk8p
复制这段内容后打开百度网盘手机App,操作更方便哦

3.application配置文件

server:
  port: 80

spring:
  elasticsearch:
    uris: http://localhost:9200

#关闭thymeleaf缓存
  thymeleaf:
    cache: false

4.先来测试一下controller和view

@Controller
public class IndexController {
    @GetMapping("/index")
    public String index(){
        return "/index";
    }
}

如果说是无法解析MVC视图的就导入一下下面这个依赖

        <dependency>
            <groupId>org.thymeleafgroupId>
            <artifactId>thymeleaf-spring5artifactId>
        dependency>

测试结果:

elasticsearch快速入门,一篇就够了,上手使用!_第2张图片

5.爬取jd官网的数据

@Component
public class HtmlParseUtil {

    public static List<Content> parseJD (String keywords) throws Exception {
        //获取请求

        String url = "https://search.jd.com/Search?keyword="+keywords;
        //解析网页
        Document document = Jsoup.parse(new URL(url),30000);

        Element element = document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements elements = element.getElementsByTag("li");
        //System.out.println(elements.html());

        List<Content> goodsList = new ArrayList<>();
        //获取所有的li元素
        for(Element el:elements){
            //关于图片:所有的图片都是延迟加载的
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();

            Content content = new Content();
            content.setTitle(title);
            content.setImg(img);
            content.setPrice(price);

            goodsList.add(content);
        }
        return goodsList;
    }
    //测试获取
    public static void main(String[] args) throws Exception {
        new HtmlParseUtil().parseJD("代码").forEach(System.out::println);
    }


}

image-20221010194203940

6.config类

elasticsearchConfig类---->restHighLevelClient放入ioc

@Configuration
public class ElasticSearchConfig {

    @Value("${spring.elasticsearch.uris}")
    private String httpPath;

    @Bean
    RestHighLevelClient restHighLevelClient(){

        HttpHost host = HttpHost.create(httpPath);//创建httpHost
        RestClientBuilder builder = RestClient.builder(host);//根据host建立此属性的服务器
        RestHighLevelClient client = new RestHighLevelClient(builder);//创建服务器对象
        return client;
    }

}

7.service层

@Service
public class ContentService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //1.解析数据放入es
    public Boolean parseContent(String keywords) throws Exception {
        //获取数据
        List<Content> contents = HtmlParseUtil.parseJD(keywords);
        //将内容放入es
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");
        for(int i=0;i<contents.size();i++){
            bulkRequest.add(
                    new IndexRequest("jd_goods")
                            .id(""+(i+1))
                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON)
            );
        }
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        
        restHighLevelClient.close();
        return !bulk.hasFailures();
    }
}

elasticsearch快速入门,一篇就够了,上手使用!_第3张图片

基本查询的步骤:
1.QueryBuilders.termQuery("title",keyWord) 然后形成精准查询termQueryBuilder
2.将精准查询ilder
3.将搜索资源放入搜索请求searchRequest
4.将请求给Client然后执行请求即可
//获取这些数据实现搜索功能
public List<Map<String,Object>> searchPage(String keyWord,int pageNo,int pageSize) throws IOException {
    if(pageNo<=1){
        pageNo=1;
    }
    //建立查询请求和请求资源
    SearchRequest searchRequest = new SearchRequest("jd_goods");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    //分页功能
    searchSourceBuilder.from(pageNo);
    searchSourceBuilder.size(pageSize);

    //精准查询
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title",keyWord);

    //精准查询放入资源,且进行设置时限
    searchSourceBuilder.query(termQueryBuilder);
    searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

    //资源放入查询请求
    searchRequest.source(searchSourceBuilder);

    //交给客户端
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    //解析结果
    ArrayList<Map<String, Object>> list = new ArrayList<>();
    for (SearchHit documentFields : searchResponse.getHits().getHits()) {
        list.add(documentFields.getSourceAsMap());
    }
    return list;
}

8.controller层

@Controller
public class IndexController {

    @Autowired
    private ContentService contentService;

    @GetMapping("/index")
    public String index(){
        return "/index";
    }

    @ResponseBody
    @GetMapping("/parse/{keyword}") //感觉关键词去京东官网爬取数据然后保存到ES
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return contentService.parseContent(keyword);
    }

    @ResponseBody
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String,Object>> search(
            @PathVariable("keyword") String keyword,
            @PathVariable("pageNo") int pageNo,
            @PathVariable("pageSize") int pageSize) throws IOException {
        return contentService.searchPage(keyword, pageNo, pageSize);
    }

}

9.view层

✒️导入Vue和axios : 创建个文档然后node.js即可

npm init
npm install vue
npm install axios

elasticsearch快速入门,一篇就够了,上手使用!_第4张图片

部分html


<div id="mallSearch" class="mall-search">
    <form name="searchTop" class="mallSearch-form clearfix">
        <fieldset>
            <legend>天猫搜索legend>
            <div class="mallSearch-input clearfix">
                <div class="s-combobox" id="s-combobox-685">
                    <div class="s-combobox-input-wrap" >
                        <input v-model="keywords" type="text" autocomplete="off" value="dd" id="mq"
                               class="s-combobox-input" aria-haspopup="true">
                    div>
                div>
                <button type="submit" id="searchbtn" @click.prevent="searchKey">搜索button>
            div>
        fieldset>
    form>
    <ul class="relKeyTop">
        <li><a>狂神说Javaa>li>
        <li><a>狂神说前端a>li>
        <li><a>狂神说Linuxa>li>
        <li><a>狂神说大数据a>li>
        <li><a>狂神聊理财a>li>
    ul>
div>

<div class="view grid-nosku">

    <div class="product" v-for="result in results">
        <div class="product-iWrap">
            
            <div class="productImg-wrap">
                <a class="productImg">
                    <img :src="result.img">
                a>
            div>
            
            <p class="productPrice">
                <em><b>¥b>{{result.price}}em>
            p>
            
            <p class="productTitle">
                <a>{{result.title}}<a>
            p>
            
            <div class="productShop">
                <span>店铺: wxp的小黑店 span>
            div>
            
            <p class="productStatus">
                <span>月成交<em>999笔em>span>
                <span>评价 <a>3a>span>
            p>
        div>
    div>
div>
<script th:src="@{/js/axios.min.js}">script>
<script th:src="@{/js/vue.js}">script>
<script>
    new Vue({
        el:'#app',
        data:{
            keyword:'',//关键词
            results:[]//搜索的结果
        },
        methods:{
            searchKey(){
                var keyword = this.keyword;
                //对接后端接口
                axios.get('search/' + keyword + "/1/10").then(resp=>{
                    this.results = resp.data;
            })
            }
        }
    })
script>

elasticsearch快速入门,一篇就够了,上手使用!_第5张图片

elasticsearch

ElasticSearch 的 Client

ElasticSearch 官方提供了 3 个 Client,具体如下:

  • org.elasticsearch.client.transport.TransportClient
  • org.elasticsearch.client.RestClient
  • org.elasticsearch.client.RestHighLevelClient

TransportClient 位于 Elasticsearch 包下,是 Elasticsearch 官方早期支持的 ElasticSearch Client,但是在 ElasticSearch 7.x 版本中已经标注为 Deprecated,并且将在 8.0 版本中移除,所以建议不使用 TransportClient 作为 ElasticSearch Client。

RestHighLevelClient 是 TransportClient 的直接替代者,也是 ElasticSearch 官方推荐和默认的 Client(松哥注:该客户端在最新版本中已经废弃)。

除了 ElasticSearch 官方提供的 Client,spring-data-elasticsearch 还支持响应式的客户端 ReactiveElasticsearchClient。ReactiveElasticsearchClient 是基于 WebClient 技术实现的 ElasticSearch Client,依赖于 Spring 的响应式栈。响应式栈在本书中不会涉及。

client.transport.TransportClient

  • org.elasticsearch.client.RestClient
  • org.elasticsearch.client.RestHighLevelClient

TransportClient 位于 Elasticsearch 包下,是 Elasticsearch 官方早期支持的 ElasticSearch Client,但是在 ElasticSearch 7.x 版本中已经标注为 Deprecated,并且将在 8.0 版本中移除,所以建议不使用 TransportClient 作为 ElasticSearch Client。

RestHighLevelClient 是 TransportClient 的直接替代者,也是 ElasticSearch 官方推荐和默认的 Client(松哥注:该客户端在最新版本中已经废弃)。

除了 ElasticSearch 官方提供的 Client,spring-data-elasticsearch 还支持响应式的客户端 ReactiveElasticsearchClient。ReactiveElasticsearchClient 是基于 WebClient 技术实现的 ElasticSearch Client,依赖于 Spring 的响应式栈。响应式栈在本书中不会涉及。

你可能感兴趣的:(elasticseacrch,elasticsearch,java,大数据)