ElasticSearch编程操作

1.创建工程,导入依赖

    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>5.6.8</version>
    </dependency>

    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>transport</artifactId>
      <version>5.6.8</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-to-slf4j</artifactId>
      <version>2.9.1</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.24</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.21</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

2.创建索引index

package com.wn.index;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IndexTest {
    /*创建索引*/
    @Test
    public void test01() throws UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //创建名称为blog2的索引
        client.admin().indices().prepareCreate("blog2").get();
        //释放资源
        client.close();
    }
}

    

3.创建映射mapping

  /*创建映射*/
    @Test
    public void test02() throws IOException, ExecutionException, InterruptedException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //添加映射
        XContentBuilder builder= XContentFactory.jsonBuilder()
                .startObject()
                .startObject("table01")
                .startObject("properties")
                .startObject("id")
                .field("type","integer").field("store","yes")
                .endObject()
                .startObject("title")
                .field("type","string").field("store","yes").field("analyzer","ik_smart")
                .endObject()
                .startObject("content")
                .field("type","string").field("store","yes").field("analyzer","ik_smart")
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        //创建映射
        PutMappingRequest mapping= Requests.putMappingRequest("blog2").type("table01").source(builder);
        client.admin().indices().putMapping(mapping).get();
        //释放资源
        client.close();
    }

    

4.创建文档document

  4.1 通过XContentFactory方式

/*创建文档document*/
    @Test
    public void test03() throws IOException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //创建文档信息
        XContentBuilder builder=XContentFactory.jsonBuilder()
                .startObject()
                .field("id",1)
                .field("title","ES是一个基于Lucene的搜索服务器")
                .field("content","提供了一个分布式多用户能力的全文引擎")
                .endObject();
        client.prepareIndex("blog2","table01","1").setSource(builder).get();
        //释放资源
        client.close();
    }

    

  4.2 通过Jackson转换实体

    4.2.1 创建实体

package com.wn.index;

public class Table01 {
    private Integer id;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

    4.2.2 导入依赖

  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.1</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.1</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.1</version>
    </dependency>

    4.2.3 编写代码

    /*创建文档document*/
    /*使用Jackson转换实体*/
    @Test
    public void test04() throws UnknownHostException, JsonProcessingException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //描述数据
        Table01 table01=new Table01();
        table01.setId(2);
        table01.setTitle("搜索工作其实很快乐");
        table01.setContent("希望我们的搜索解决方案要快");
        ObjectMapper objectMapper=new ObjectMapper();
        //建立文档
        client.prepareIndex("blog2","table01",table01.getId().toString())
                .setSource(objectMapper.writeValueAsString(table01).getBytes(),
                XContentType.JSON).get();
        //释放资源
        client.close();
    }

    

5.查询文档操作

  5.1 关键词查询

/*关键词查询*/
    @Test
    public void testTermQuery() throws UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        //设置搜索条件
        SearchResponse searchResponse=client.prepareSearch("blog2")
                .setTypes("table01")
                .setQuery(QueryBuilders.termQuery("content","搜索")).get();

        //遍历搜索结果数据
        SearchHits hits=searchResponse.getHits();    //获取命中次数,查询结果有多少对象
        System.out.println("查询结果有:"+hits.getTotalHits()+"条");
        Iterator<SearchHit> iterator=hits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit=iterator.next();    //每个查询对象
            System.out.println(searchHit.getSourceAsString());  //获取字符串格式打印
            System.out.println("title:"+searchHit.getSource().get("title"));
        }
        //释放资源
        client.close();
    }

    

  5.2 字符串查询

 /*字符串查询*/
    @Test
    public void testStringQuery() throws UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        //设置搜索条件
        SearchResponse searchResponse=client.prepareSearch("blog2")
                .setTypes("table01")
                .setQuery(QueryBuilders.queryStringQuery("搜索")).get();

        //遍历搜索结果数据
        SearchHits hits=searchResponse.getHits();   //获取命中次数,查询结果有多少对象
        System.out.println("查询结果有:"+hits.getTotalHits()+"条");
        Iterator<SearchHit> iterator=hits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit=iterator.next();    //每个查询对象
            System.out.println(searchHit.getSourceAsString());  //获取字符串格式打印
            System.out.println("title:"+searchHit.getSource().get("title"));
        }
        //释放资源
        client.close();
    }

    

  5.3 使用文档ID查询

 /*使用文档ID查询*/
    @Test
    public void testIdQuery() throws UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        //设置搜索条件
        SearchResponse response=client.prepareSearch("blog2")
                .setTypes("table01")
                //设置需要查询的id
                .setQuery(QueryBuilders.idsQuery().addIds("2")).get();
        //取出查询结果
        SearchHits searchHits=response.getHits();
        //取出查询结果总记录数
        System.out.println(searchHits.getTotalHits());
        Iterator<SearchHit> hitIterator=searchHits.iterator();
        while (hitIterator.hasNext()){
            SearchHit searchHit=hitIterator.next();
            System.out.println(searchHit.getSourceAsString());
        }
    }

    

6.查询文档分页操作

  6.1 批量插入数据

 /*批量插入数据*/
    @Test
    public void test01() throws JsonProcessingException, UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        ObjectMapper objectMapper=new ObjectMapper();
        for (int i=1;i<=10;i++){
            //描述JSON数据
            Table01 table01=new Table01();
            table01.setId(i);
            table01.setTitle(i+"搜索工作其实很快乐");
            table01.setContent(i+"我们希望我们的搜索解决方案要快!!!");

            //建立文档
            client.prepareIndex("blog2","table01",table01.getId().toString())
                    .setSource(objectMapper.writeValueAsString(table01).getBytes(), XContentType.JSON).get();
        }
        //释放资源
        client.close();
    }

    

  6.2 分页查询

    /*分页查询*/
    @Test
    public void test02() throws UnknownHostException {
        //创建client连接对象
        Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build();
        TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        //搜索数据
        SearchRequestBuilder searchRequestBuilder=client.prepareSearch("blog2")
                .setTypes("table01").setQuery(QueryBuilders.matchAllQuery());   //默认每页10条数据

        //查询第2页数据,每页3条
        searchRequestBuilder.setFrom(0).setSize(3);
        SearchResponse searchResponse=searchRequestBuilder.get();

        SearchHits hits=searchResponse.getHits();   //获取命中次数,查询结果有多少对象
        System.out.println("查询结果有:"+hits.getTotalHits()+"条");
        Iterator<SearchHit> iterator=hits.iterator();
        while (iterator.hasNext()){
            SearchHit searchHit=iterator.next();    //每个查询对象
            System.out.println(searchHit.getSourceAsString());  //获取字符串格式打印
            System.out.println("id:"+searchHit.getSource().get("id"));
            System.out.println("title:"+searchHit.getSource().get("title"));
            System.out.println("content:"+searchHit.getSource().get("content"));
            System.out.println("---------------------------");
        }
        //释放资源
        client.close();
    }

    

7.查询结果高亮操作

  7.1 什么是高亮显示

    在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮

      

  7.2 高亮显示的html分析

    通过开发者工具查看高亮数据的htmldiamante实现;

      

    ES可以查看出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ES使用什么标签对高亮关键字进行包裹;

  7.3 高亮显示代码实现

package com.wn.index;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GaoTest {

    @Test
    public void test01() throws UnknownHostException {
        /*高亮查询*/
        //创建client连接对象
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        //搜索数据
        SearchRequestBuilder searchRequestBuilder = client
                .prepareSearch("blog2").setTypes("table01")
                .setQuery(QueryBuilders.termQuery("title", "搜索"));

        //设置高亮数据
        HighlightBuilder hiBuilder = new HighlightBuilder();
        hiBuilder.preTags("<font style=‘color:red‘>");
        hiBuilder.postTags("</font>"); hiBuilder.field("title");
        searchRequestBuilder.highlighter(hiBuilder);

        //获取查询结果数据
        SearchResponse searchResponse=searchRequestBuilder.get();

        //获取查询结果集
        SearchHits searchHits=searchResponse.getHits();
        System.out.println("共搜索到:"+searchHits.getTotalHits()+"条结果");
        //遍历结果
        for (SearchHit hit:searchHits){
            System.out.println("String方式打印文档搜索内容");
            System.out.println(hit.getSourceAsString());
            System.out.println("Map方式打印高亮内容");
            System.out.println(hit.getHighlightFields());
            System.out.println("遍历高亮集合,打印高亮片段");
            org.elasticsearch.common.text.Text[] text = hit.getHighlightFields().get("title").getFragments();
            System.out.println("------------------------------------------------------------------");
            for (Text str:text){
                System.out.println(str);
            }
            //关闭资源
            client.close();
        }
    }

}

    

原文地址:https://www.cnblogs.com/wnwn/p/12369425.html

时间: 2024-10-12 11:57:00

ElasticSearch编程操作的相关文章

使用模板元编程操作类型集合(C++11下的TypeList)

Wrote by mutouyun. (http://darkc.at/cxx-type-list/) 群里有个朋友要实现这么一个功能:如何在编译期把一个函数类型的参数减少一个. 简单来说,就是实现下面这个模板: remove_func_par<2, void(int, long, short)>::type; // type = void(int, long) 根据输入的编译期整数,把函数参数表里对应的参数干掉一个. 为了实现这种功能,我们需要操作变参模板的参数包.比如像这样: // mak

SpringBoot使用WebFlux响应式编程操作数据库

这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 前言 在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring WebFlux还没有支持类似Mysql这样的关系型数据库,所以本文以MongoDb数据库为例. SpringBoot使用WebFlux响应式编程操作数据库 接下来介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 新建项目 pom文件 新建项目,在项目中加入webflux

elasticsearch java操作 api

默认进行了elasticsearch安装和ik安装, 超时配置, 分页压力配置等 添加maven依赖 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.4.0</version> </dependency> <dependency> <group

[PHP]PHP编程操作Mysql数据库的三种方式

当我开始去接触PHP的时候,真切的感受到其所具有的魅力,本着学习的态度和打破固有的语言和模式的想法,开始了PHP之旅,总的来说,走的还是比较顺利,在其中能够看到C,Java,Perl影子,学习曲线不大,但是做好产品仍然有着一条漫漫长路. 多余的话不说了,慢慢感受和领悟,本文主要讲述PHP操作数据库的三种扩展. 如下图是PHP访问数据库的三种扩展方式: 下面举出三种方式访问数据库并查询数据的实例代码: 1.mysql扩展 <?php //1:获取数据库连接 $connection = @ mysq

socket网络编程——操作篇

一.问题思考 问1.网络通信应用在什么场合?通信的前提是什么? 答1.主要应用在不同主机进程间的互相通信,同一主机的进程也可以使用网络进行通信.通信的前提是如何标识通信进程的唯一,由于不同主机的进程极有可能具有相同的PID,因此,在网络中单单靠PID是无法准确进行标识进程身份的,TCP/IP协议族网络层的IP地址可以唯一的标识连入网络的主机. 问2.socket编程重点是什么? 答2.掌握基于TCP.UDP的S/C架构的编程要点:掌握网络通信方式之间的区别和应用场合. 问3.什么是网络模型? 答

elasticsearch更新操作问题

elasticsearch在更新的时候,是通过id进行管理的,我们在前台传入id操作,id如果与elasticsearch相同,则覆盖,否则新增一条记录.且elasticsearch中的插入一条记录和更新一条记录的代码是一样的,如下: public boolean updateIndex(String indexName, String id, Map map)            throws Exception {        // TODO Auto-generated method

elasticsearch基础操作02

安装ES集群 准备三台机器: 192.168.1.10 192.168.1.11 192.168.1.12 三台机器分别部署elasticsearch 部署前,应该先检查机器是否有安装jdk,如果没有请先安装JDK,三台机器时间同步 安装方式非常简单,不会的请参考官方文档,下面我以elasticsearch-1.7.2来示例 rpm -ivh elasticsearch-1.7.2.noarch.rpm 安装完成修改配置文件: vim /etc/elasticsearch/elasticsear

elasticsearch聚合操作——本质就是针对搜索后的结果使用桶bucket(允许嵌套)进行group by,统计下分组结果,包括min/max/avg

分析 Elasticsearch有一个功能叫做聚合(aggregations),它允许你在数据上生成复杂的分析统计.它很像SQL中的GROUP BY但是功能更强大. 举个例子,让我们找到所有职员中最大的共同点(兴趣爱好)是什么: GET .monitoring-es-6-2018.08.16/_search{ "aggs": { "all_interests": { "terms": { "field": "sour

Python Elasticsearch API操作ES集群

环境 Centos 7.4 Python 2.7 Pip 2.7 MySQL-python 1.2.5 Elasticsearc 6.3.1 Elasitcsearch6.3.2 知识点 调用Python Elasticsearh API Python Mysqldb使用 DSL查询与聚合 Pyehon 列表操作 代码 #!/usr/bin/env python # -*- coding: utf-8 -*- #minyt 2018.9.1 #获取24小时内出现的模块次数 # 该程序通过elas