/**
* @author: yqq
* @date: 2019/2/28
* @description:
*/
public class TestMain {
private static RestClient restClient; static {
restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
} /**
* 1.查询所有数据
* @throws Exception
*/
@Test
public void QueryAllSkuId() throws Exception {
String method = "POST";
String endpoint = "/sku/doc/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} /**
* 2.查询匹配,message为条件,新增的时候必须包含该字段
* @throws IOException
*/
@Test
public void searchSkuId () throws IOException { /**
* 添加的数据格式
{"skuId":"111111111","content":"对于非基本类型,也就是常说的引用数据类型"}
*/ String method = "POST";
String endpoint = "/sku/doc/_search";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"query\": { \n" +
" \"match\": {\n" +
" \"content\": \"基本类型\"\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} /**
* 3.查询所有数据
* @throws Exception
*/
@Test
public void QueryAll() throws Exception {
String method = "POST";
String endpoint = "/delete-index/_search/";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON); Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} /**
*4. 查询匹配,message为条件,新增的时候必须包含该字段
* @throws IOException
*/
@Test
public void search () throws IOException { /**
* 添加的数据格式
{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}
*/ String method = "POST";
String endpoint = "/delete-index/_search/";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"query\": { \n" +
" \"match\": {\n" +
" \"message\": \"out\"\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}

引入依赖:

        <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.3</version>
</dependency>

Elasticsearch示例的更多相关文章

  1. 用beam实现连接kafka和elasticSearch示例 在flink平台运行

    示例实现beam用java编程,监听kafka的testmsg主题,然后将收取到的单词,按5秒做一次统计.结果输出到outputmessage 的kafka主题,同时同步到elasticSearch. ...

  2. 实战ELK(8) 安装ElasticSearch中文分词器

    安装 方法1 - download pre-build package from here: https://github.com/medcl/elasticsearch-analysis-ik/re ...

  3. Python操作ElasticSearch

    Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...

  4. ElasticSearch——Logstash输出到Elasticsearch配置

    位置 在Logstash的.conf配置文件中的output中配置ElasticSearch 示例: output { elasticsearch{ action => "index& ...

  5. Haystack-全文搜索框架

    Haystack 1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsear ...

  6. Haystack全文检索

    1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch(java写的 ...

  7. Haystack

    什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh,  ...

  8. django框架中的全文检索Haystack

    1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh ...

  9. express操作数据库

    Express 首页 入门 使用指南 API 中文手册 进阶话题 有用的资源 集成数据库 为 Express 应用添加连接数据库的能力,只需要加载相应数据库的 Node.js 驱动即可.这里将会简要介 ...

随机推荐

  1. SpringBoot 使用JPA时解决no session的方法

    1.在application.yml中添加 spring.jpa.open-in-view: true 2.在使用查询的方法添加 @Transactional

  2. jdk1.8 HashMap底层数据结构:深入解析为什么jdk1.8 HashMap的容量一定要是2的n次幂

    前言 1.本文根据jdk1.8源码来分析HashMap的容量取值问题: 2.本文有做 jdk1.8 HashMap.resize()扩容方法的源码解析:见下文“一.3.扩容:同样需要保证扩容后的容量是 ...

  3. MySQL-EXPLAIN执行计划Extra解释

    EXPLAIN命令输出的列中Extra字段可选值较多,这里单独说一下. 该Extra列 EXPLAIN输出包含MySQL解决查询的额外信息.以下列表说明了此列中可能出现的值.每个项目还指示JSON格式 ...

  4. lumen 路由访问路径

    项目目录/public/index.php/接你设置的路由 比如设置了 $app->get('/test', function () use ($app) {    return $app-&g ...

  5. Element-UI 表单验证规则rules 配置参数说明

    官方文档 : https://github.com/yiminghe/async-validator

  6. NVIDIA: Failed to initialize NVML: driver/library version mismatch

    [NVIDIA驱动:Failed to initialize NVML: driver/library version mismatch] 原因:Ubuntu16.04 装新驱动时,会报以上错误,定位 ...

  7. Linux配置部署_新手向(二)——Nginx安装与配置

    目录 前言 Nginx 配置(后续补充) 小结 @ 前言 上一篇整完Linux系统的安装,紧接着就开始来安装些常用的东西吧,首先Nginx. Nginx 简介 Nginx作为转发,负载均衡,凭着其高性 ...

  8. 技术改变生活| 免费看VIP视频,屏蔽广告,解锁新姿势!

    说到这个,我就忍不住的要介绍一下今天的主角 Tampermonkey 了.Tampermonkey 是一款免费的浏览器扩展和最为流行的用户脚本管理器,它适用于Chrome, Microsoft Edg ...

  9. Delphi - Indy TIdFTP控件实现文件的上传和下载

    FTP信息保存和获取 我们在做FTP相关开发时,为方便后续FTP切换,一般先把FTP账户信息保存到数据库表中,在使用时再通过Query获取出来. 一般通过如下方式获取到FTP相关信息,代码如下: // ...

  10. Windows Server - SVN 服务器搭建与项目配置、客户端安装与配置

    本教程以Windows Server 2012 R12 为例搭建SVN服务器,安装部署完成后,客户端可通过SVN客户端访问SVN服务器上的项目,也可以访问网上其他SVN服务器上的项目. 一.首先准备三 ...