Maven使用Elasticsearch入门

windows10安装Elasticsearch与head-master教程参考另外一篇文章

文章链接 : windows10安装ElasticSearch与Head-master+IK分词器安装与自定义词库

1. ElasticSearch操作入门

创建Maven工程 pom.xml导入坐标依赖,除了第一个外,剩下都是日志依赖,可以不用

    <dependencies>
        <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.10</version>
        </dependency>
    </dependencies>

1.1 新建索引

package com.jwc.test;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.InetAddress;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-07-30 10:07
 */
public class TestES {
    /*
    * 使用默认方式创建文档
    * */
    @Test
    public void testCreateDoc()throws Exception{
        // 创建客户端访问对象 EMPTY表示使用默认设置
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY)
                // 使用对象方式设置客户端地址 和 端口号
                .addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // 创建文档内容 json {id:1,title:"这个是title",content:"这是个content"}
       XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id",1)
                .field("title","这是个title")
                .field("content","这是个content")
                .endObject();
        // 客户端发送文档到服务器,必须调用get 才能够获取到Response对象 ,不调用get不执行,如果不指定ID 那么是随机字符串`
        IndexResponse indexResponse = transportClient.prepareIndex("blog", "article", "1").setSource(xContentBuilder).get();
        RestStatus status = indexResponse.status();
        System.out.println(status);
        // 关闭客户端资源
        transportClient.close();
    }
}

1.2 查询所有

package com.jwc.test;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.InetAddress;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-07-30 10:07
 */
public class TestES {
    /*
    * 查询所有
    * */
    @Test
    public void queryAll()throws Exception{
        // 创建客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // 创建查询,查询的索引是 blog type是 article
        SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
                // 使用查询抽象接口查询所有  .get 表示执行
                .setQuery(QueryBuilders.matchAllQuery()).get();
        // 获取查询结果
        SearchHits hits = response.getHits();
        // 获取总条数
        System.out.println(hits.getTotalHits());
        // 获取查询内容
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        // 关闭资源
        transportClient.close();
    }
   }

1.3 字符串查询

package com.jwc.test;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.InetAddress;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-07-30 10:07
 */
public class TestES {
    /*
     * 按照字符串查询
     * */
    @Test
    public void queruString()throws Exception{
        // 创建客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // 创建查询,查询的索引是 blog type是 article
        SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
                // 使用查询抽象接口进行字符串查询  .get 表示执行
                .setQuery(QueryBuilders.queryStringQuery("这是")).get();
        // 获取查询结果
        SearchHits hits = response.getHits();
        // 获取总条数
        System.out.println(hits.getTotalHits());
        // 获取查询内容
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        // 关闭资源
        transportClient.close();
    }
}

1.4 词条查询

package com.jwc.test;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.InetAddress;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-07-30 10:07
 */
public class TestES {
    /*
     * 按照词条查询
     * */
    @Test
    public void queryTerm()throws Exception{
        // 创建客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // 创建查询,查询的索引是 blog type是 article
        SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
                // 使用查询抽象接口进行词条查询  .get 表示执行
                .setQuery(QueryBuilders.termQuery("title","这是")).get();
        // 获取查询结果
        SearchHits hits = response.getHits();
        // 获取总条数
        System.out.println(hits.getTotalHits());
        // 获取查询内容
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

        // 关闭资源
        transportClient.close();
    }
}

1.5 通配符查询/模糊查询

package com.jwc.test;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.InetAddress;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-07-30 10:07
 */
public class TestES {
    /*
     * 通配符查询
     * */
    @Test
    public void queryWildCard()throws Exception{
        // 创建客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        // 创建查询,查询的索引是 blog type是 article
        SearchResponse response = transportClient.prepareSearch("blog").setTypes("article")
                // 使用查询抽象接口进行通配符查询  .get 表示执行
                .setQuery(QueryBuilders.wildcardQuery("title","*这是")).get();
        // 获取查询结果
        SearchHits hits = response.getHits();
        // 获取总条数
        System.out.println(hits.getTotalHits());
        // 获取查询内容
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        // 关闭资源
        transportClient.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我三胖哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值