ElasticSearch

1.为何ElasticSearch

ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,其第一个版本于2010年2月出现在GitHub上并迅速成为最受欢迎的项目之一。

2.ES安装及使用说明

ES安装jdk环境必须是1.7+才能运行

1.官网下载ES安装包;https://www.elastic.co/downloads/elasticsearch

2.运行lib下面的elasticsearch.bat

3.验证服务端是否运行成功

访问:http://localhost:9200/

3.ES辅助管理工具Kibana

① Kibana5.2.2下载地址:https://www.elastic.co/downloads/kibana

② 解压并编辑config/kibana.yml,设置elasticsearch.url的值为已启动的ES

③ 启动Kibana5 : bin\kibana.bat

④ 默认访问地址:http://localhost:5601

Discover:可视化查询分析器

Visualize:统计分析图表

Dashboard:自定义主面板(添加图表)

Timelion:Timelion是一个kibana时间序列展示组件(暂时不用)

Dev Tools :Console(同CURL/POSTER,操作ES代码工具,代码提示,很方便)

Management:管理索引库(index)、已保存的搜索和可视化结果(save objects)、设置 kibana 服务器属性。

4.ES数据管理

4.2文档的增删改

增加:

PUT rpms/role/1
{
  "name":"李四",
  "age":18
}

删除:

#删除指定id的数据
DELETE rpms/role/1

修改:

PUT rpms/role/1
{
  "name":"王五",
  "age":18
}

修改部分内容:

#更新部分文档
POST rpms/role/1/_update
{
  "doc": {
    "name":"超级管理员",
  }
}

4.3文档的简单查询

根据id查询:

GET rpms/role/1

查询全部:


GET  rpms/role/_search

批量查询:

//方式i
GET _mget
{
  "docs":[
    {
      "_index":"rpms",
      "_type":"role",
      "_id":1
    },
    {
      "_index":"rpms",
      "_type":"role",
      "_id":3,
      "_source":"name"
    }
    
  ]
  
}
//方式2
GET rpms/role/_mget
{
  "ids":[1,3]
  
}

分页查询:

#DSL查询与过滤
GET crm/students/_search
{
  "query": {
    "match": {
      "name": "tq"
    }
  },
    "from":0,
    "size":2,
    "_source":["id","name"],
    "sort":[{"age":"desc"}]
  
}

过滤查询

//查询出年龄为20的
GET crm/students/_search?q=age:20

5.分词与映射

使用前的准备,将ik分词器的jar包在es/plugin文件夹下解压

使用:

POST _analyze
{
"analyzer":"ik_smart",
"text":"吃饭睡觉打豆豆"
}

自定义映射:

#映射

DELETE shop

PUT shop

POST shop/goods/_mapping
{
  "goods":{
    "properties":{
      "id":{
        "type":"integer"
      },
      "name":{
        "type":"text",
        "analyzer":"ik_smart",
        "search_analyzer":"ik_smart"

      }
    }
  }
}

查询映射:

#查询映射
GET shop/goods/_mapping

配置全局映射:

PUT _template/global_template
{
    "template": "*", 
    "settings": {
        "number_of_shards": 1
    }, 
    "mappings": {
        "_default_": {
            "_all": {
                "enabled": false
            }, 
            "dynamic_templates": [
                {
                    "string_as_text": {
                        "match_mapping_type": "string", 
                        "match": "*_text", 
                        "mapping": {
                            "type": "text", 
                            "analyzer": "ik_max_word", 
                            "search_analyzer": "ik_max_word", 
                            "fields": {
                                "raw": {
                                    "type": "keyword", 
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }, 
                {
                    "string_as_keyword": {
                        "match_mapping_type": "string", 
                        "mapping": {
                            "type": "keyword"
                        }
                    }
                }
            ]
        }
    }
}

6.java代码操作ElasticSearch

1.工具类获取连接

public static TransportClient getClient() throws Exception {

        return new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    }

2.新增数据:

@Test
    public void add() throws Exception {
        //获取连接
        TransportClient client = getClient();
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("crm", "teacher",  "1");

        Map mp  = new HashMap();
        mp.put("id", 1);
        mp.put("name", "小苍");
        mp.put("age", 30);
        IndexResponse indexResponse = indexRequestBuilder.setSource(mp).get();
        System.out.println(indexResponse);

    }

3.获取新增的数据:

@Test
    public void get() throws Exception {
        //获取连接
        TransportClient client = getClient();
        GetResponse getFields = client.prepareGet("crm", "teacher", "1").get();
        System.out.println(getFields.getIndex());

    }

4.更新某个数据:

//更新文档
    @Test
    public void upDateIndex() throws Exception {

        TransportClient client = getClient();
        Map mp = new HashMap();
        mp.put("id", 2);
        mp.put("name", "小泽");
        mp.put("age", 25);
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("crm", "teacher", "1").setDoc(mp);
        System.out.println(updateRequestBuilder.get());
    }

5.删除:

//删除文档
    @Test
    public void testDelete() throws Exception {
        TransportClient client = getClient();
        DeleteResponse deleteResponse = client.prepareDelete("crm", "teacher", "2").get();
        System.out.println(deleteResponse);

    }

6.批量添加:

 //批量添加
    @Test
    public void getBulk() throws Exception {

        TransportClient client = getClient();
        //得到批量请求对象
        BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

        for (int i=0;i<15;i++){
            Map mp = new HashMap();
            mp.put("id", 2+i);
            mp.put("name", "小泽"+i);
            mp.put("age", 12+i);
            mp.put("sex", "女");
            bulkRequestBuilder.add(client.prepareIndex("crm","teacher",i+"").setSource(mp));

        }
        BulkResponse bulkItemResponses = bulkRequestBuilder.get();
        if(bulkItemResponses.hasFailures()){
            System.out.println("error");
        }
        client.close();

    }

7.高级查询:

//高级查询
    @Test
    public void findByQuery() throws Exception {
        TransportClient client = getClient();
        //拿到提交查询对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //匹配性别为女的
        List<QueryBuilder> must = boolQueryBuilder.must();
        must.add(QueryBuilders.termQuery("sex", "女"));
        //年龄为18-25的人
        List<QueryBuilder> filter = boolQueryBuilder.filter();
        filter.add(QueryBuilders.rangeQuery("age").gte(18).lte(25));
        //设置分页
        SearchResponse searchResponse = client.prepareSearch("crm").setFrom(0).setSize(3).addSort("age", SortOrder.DESC).setQuery(boolQueryBuilder).get();
        //打印内容
        System.out.println(searchResponse.getHits().getTotalHits());
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSource());
        }
        client.close();

    }

转载于:https://my.oschina.net/u/4107793/blog/3042996

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值