ElasticSearch& ElasticSearch JAVA API

ElasticSearch

几个概念

1.Elastic Search特点:分布式 、REstFul风格、数据分析引擎

2.Elastic stack :包括Es、Kibana、Logstash、Beats (亦称为ELK Static)
特点:任意数据来源和格式、实时分析、搜索、可视化

3.类比


相对于solr特点:易于安装、轻巧、json配置 、监控和指标

安装和启动

https://www.elastic.co/cn/downloads/elasticsearch
Windows版本后解压缩后启动bin/elasticsearch.bat
访问localhost:9200 如下信息:

Es基础操作

一、索引操作

1.创建索引 put

创建一个名为shopping 的索引
Put http://127.0.0.1:9200/shopping Put具有幂等性

2.获取索引信息 get

3.查看所有索引信息 get

http://127.0.01:9200/_cat/indices?v

4.删除某个索引 delete

二、文档操作

1.创建文档post

POST http://127.0.0.1:9200/shopping/_doc 数据写在请求body中 json格式
自定义id http://127.0.0.1:9200/shopping/_doc/001,

2.查询文档get

●通过ID查询
Get http://127.0.0.1:9200/shopping/_doc/001

●获取所有数据
Get http://127.0.0.1:9200/shopping/_search

3.更新文档 put/post

●全局更新 更新整条json
Post/put http://127.0.0.1:9200/shopping/_doc/001

●局部修改 只更新json的相应键的值
Post http://127.0.0.1:9200/shopping/_update/001

4.删除文档 delete

Delete http://127.0.0.1:9200/shopping/_doc/001

三、查询

1.条件查询

条件查询1 http://127.0.0.1:9200/shopping/_search?q=category:小米
●条件查询2(推荐)

2.分页查询、选取想要的字段、排序

From size分页 _source选取字段 sort 排序
按照price(价格)倒序

3.多条件查询和范围查询

  • Query中:
    • Bool:条件
      • Must:(与)
      • Should:(或)
  • filter 过滤
    • range 范围

4.全文检索\完全匹配、高亮查询

match全文检索
match_phrase完全匹配

高亮查询

5.聚合查询

//term分组 avg平均值 max min sum ...

6.映射关系

为user索引创建映射关系

四、---

所有doc结构

{
"took": 541, #响应时间:ms
"timed_out": false, #超时:false
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": { #命中的集合
"total": { #数量
"value": 1,
"relation": "eq"
},
"max_score": 1.0, #集合中最大的权重
"hits": [ #命中的集合
{
"_index": "user", #索引
"_type": "_doc", #类型
"_id": "001", #id
"_score": 1.0, #权重
"_source": { #记录详情
"age": "23",
"gender": "女",
"name": "李啊"
}
}
]
}
}

单条doc结构

{
    "_index": "user",
    "_type": "_doc",
    "_id": "001",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "age": "23",
        "gender": "女",
        "name": "李康平"
    }
}

JAVA API

1.pom.xml

        <!-- es-->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!--es客户端-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.53</version>
        </dependency>

        <!--log4j-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>

2.实体类

public class User {
    private String name;
    private Integer age;
    private String gender;

    public User(String name, Integer age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

3.索引的操作

/**
 * 索引的操作 新增、删除、查询
 */
public class ES_Index {
    public static void main(String[] args) throws IOException {
        //ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        //创建索引
//         boolean flag=new ES_Index().createIndex(esClient,"user");
//        System.out.println(flag);

        //查询
//       String s=new ES_Index().searchIndex(esClient,"user").toString();
//        System.out.println(s);

        //删除索引
//        boolean b=new ES_Index().deleteIndex(esClient,"user");
//        System.out.println(b);


        //关闭ES客户端
        esClient.close();
    }

    /**
     * 创建索引
     * @param client  RestHighLevelClient ES客户端
     * @param indexName  索引名称
     * @return  boolean 是否创建成功
     */
    public boolean createIndex(RestHighLevelClient client,String indexName) throws IOException {
        CreateIndexRequest userIndex = new CreateIndexRequest(indexName);
        CreateIndexResponse response= client.indices().create(userIndex, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();

        return acknowledged;
    }

    /**
     *删除索引
     * @param client  RestHighLevelClient ES客户端
     * @param indexName  索引名称
     * @return boolean  是否删除成功
     * @throws IOException
     */
    public boolean deleteIndex(RestHighLevelClient client,String indexName) throws IOException {
        DeleteIndexRequest request= new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        return delete.isAcknowledged();
    }


    public Map searchIndex(RestHighLevelClient client,String indexName) throws IOException {
            GetIndexRequest request=new GetIndexRequest(indexName);
        GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
        //这边对应的是 get Index返回的参数
        Map<String,Map> resMap=new HashMap();
        resMap.put("aliases",getIndexResponse.getAliases());
        resMap.put("mappings",getIndexResponse.getMappings());
        resMap.put("settings",getIndexResponse.getSettings());
        return resMap;

    }
}

4.文档的操作


/**
 * 文档的操作
 *
 * 插入、更新  IndexRequest  request.source()    IndexResponse =client.index()
 * 局部更新    UpdateRequest    request.doc()   UpdateResponse=client.update()
 * 删除        DeleteReqeust
 * 查询       GetRequest
 *
 * 批量操作     BulkRequest.add(...Request);
 *
 */
public class ES_Doc {
    /**
     * 插入/更新 User
     * @param client
     * @param indexName
     * @param user  实例类
     * @param id
     * @return
     * @throws IOException
     */
    public Map createDoc(RestHighLevelClient client, String indexName, User user, String id) throws IOException {
        IndexRequest request = new IndexRequest();
        //request index&id
        request.index(indexName).id(id);
        //request source
        String userJson = JSON.toJSONString(user);
        request.source(userJson, XContentType.JSON);
        //发送并响应
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        Map<String, String> resMap = new HashMap();
        resMap.put("id", response.getId());
        resMap.put("result", response.getResult().toString());
        resMap.put("index", response.getIndex());
        return resMap;
    }


    /**
     * 局部修改文档-修改年龄
     * @param
     * @throws IOException
     */
    public Map updateDoc(RestHighLevelClient client,String indexName,String id,String age) throws IOException {
        UpdateRequest request=new UpdateRequest();
        //指定indexi&d
        request.index(indexName).id(id);
        //指定内容
        request.doc(XContentType.JSON,"age",age);
        //发送并响应
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        Map<String, String> resMap = new HashMap();
        resMap.put("id", response.getId());
        resMap.put("result", response.getResult().toString());
        resMap.put("index", response.getIndex());
        return resMap;


    }

    /**
     * 查询文档 返回json
     * @param client
     */
    public String  searchDoc(RestHighLevelClient client,String indexName,String id) throws IOException {
        GetRequest request=new GetRequest();
        request.index(indexName).id(id);
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        return response.getSourceAsString();

    }

    /**
     * 根据id删除文档
     * @param client
     * @param indexName
     * @param id
     */
    public  String deleteDoc(RestHighLevelClient client,String indexName,String id) throws IOException {
        DeleteRequest request=new DeleteRequest();
        request.index(indexName).id(id);
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
       return  response.getResult().toString();
    }


    public void executebatch(RestHighLevelClient client,String indexName) throws IOException {
        BulkRequest bulkRrequest=new BulkRequest();
        //这里可以将多个 IndexRequest\UpdateRequest\DeleteRequest 放入
        for(int i=0;i<=7;i++){
            IndexRequest indexRequest=new IndexRequest();
            indexRequest.index(indexName).id("00"+i);
            User user=new User("张"+i,18+i,"男");
            String userJson=JSON.toJSONString(user);
            indexRequest.source(userJson, XContentType.JSON);
            bulkRrequest.add(indexRequest);
        }
        BulkResponse bulkResponse = client.bulk(bulkRrequest, RequestOptions.DEFAULT);
    }


    public static void main(String[] args) throws IOException {
        //ES客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        //insert
//        User user = new User("何必", 33, "男");
//        Map<String,String> map=new ES_Doc().createDoc(client, "user", user, "003");
//        System.out.println(map);

        //update
//        Map<String,String> map=new ES_Doc().updateDoc(client,"user","001","31");
//        System.out.println(map);

        //search By id
//        String s=new ES_Doc().searchDoc(client,"user","002");
//        System.out.println(s);

        //delete By id
//        String dd=new ES_Doc().deleteDoc(client,"user","003");
//        System.out.println(dd);


        new ES_Doc().executebatch(client,"user");


        //关闭ES客户端
        client.close();
    }
}

5.查询


/**
 * 查询:
 *      全量、条件、组合、范围、模糊
 * 对查询结果:
 *      分页、排序、选取字段、高亮、聚合、分组
 */
public class ES_Search {
    public static void main(String[] args) throws IOException {
        //ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );
        //查询
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("user");
        //1.全量查询  matchAllQuery
//        QueryBuilder query=  QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        searchRequest.source(search);

        //2.条件查询 termQuery
//        QueryBuilder query=QueryBuilders.termQuery("age",23);
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        searchRequest.source(search);

        //3.组合查询 boolQuery
//        BoolQueryBuilder query = QueryBuilders.boolQuery();
//        query.must(QueryBuilders.matchQuery("age",23));
//        query.should(QueryBuilders.matchQuery("gender","男"));
//        query.mustNot(QueryBuilders.matchQuery("gender","女"));
//        SearchSourceBuilder search = new SearchSourceBuilder().query(query);
//        searchRequest.source(search);

        //4.范围查询  rangeQuery
//        RangeQueryBuilder query = QueryBuilders.rangeQuery("age");
//        query.gte(20);
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        searchRequest.source(search);

        //5.模糊查询 fuzzyQuery
//        FuzzyQueryBuilder query = QueryBuilders.fuzzyQuery("name", "张").fuzziness(Fuzziness.ONE);
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        searchRequest.source(search);


        //a.分页
//        QueryBuilder query = QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search = new SearchSourceBuilder().query(query);
//        search.from(0);
//        search.size(3);
//        searchRequest.source(search);

        //b.排序-只能对数值型排序
//           QueryBuilder query = QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search = new SearchSourceBuilder().query(query);
//        search.sort("age", SortOrder.DESC);
//        searchRequest.source(search);

        //c.过滤字段
//         QueryBuilder query = QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search = new SearchSourceBuilder().query(query);
//        search.fetchSource(new String[]{"name"},new String[]{});//String[] includes ,String[] excludes
//        searchRequest.source(search);

        //d。高亮查询
//        TermQueryBuilder query = QueryBuilders.termQuery("name", "张");
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        HighlightBuilder highlighter = new HighlightBuilder();
//        highlighter.preTags("<font color:'red'>");
//        highlighter.postTags("</font>");
//        highlighter.field("name");
//        search.highlighter(highlighter);
//        searchRequest.source(search);

        //e.聚合查询
//        MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        AggregationBuilder aggregationBuilder= AggregationBuilders.avg("avgAge").field("age");
//        search.aggregation(aggregationBuilder);
//        searchRequest.source(search);

        //f.分组查询
//        MatchAllQueryBuilder query = QueryBuilders.matchAllQuery();
//        SearchSourceBuilder search=new SearchSourceBuilder().query(query);
//        AggregationBuilder aggregationBuilder= AggregationBuilders.terms("ageGroup").field("age");
//        search.aggregation(aggregationBuilder);
//        searchRequest.source(search);



        //结果
        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        Arrays.asList(searchResponse.getHits().getHits()).forEach(
                a-> System.out.println(a)
        );



        //关闭ES客户端
         esClient.close();

    }
}

posted @ 2021-08-03 10:18  赵钱富贵  阅读(130)  评论(0编辑  收藏  举报