运行结果:返回5条数据

参考代码ESTestDocumentAPI.java

package com.dajiangtai.djt_spider.elasticsearch;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchHits;
import org.junit.Before;
import org.junit.Test; /**
* Document API 操作
*
* @author 大讲台
*
*/
public class ESTestDocumentAPI {
private TransportClient client; @Before
public void test0() throws UnknownHostException { // 开启client.transport.sniff功能,探测集群所有节点
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "escluster")
.put("client.transport.sniff", true).build();
// on startup
// 获取TransportClient
client = TransportClient
.builder()
.settings(settings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("master"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave1"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave2"), 9300));
} /**
* 创建索引:use ElasticSearch helpers
*
* @throws IOException
*/
@Test
public void test1() throws IOException {
IndexResponse response = client
.prepareIndex("twitter", "tweet", "1")
.setSource(
jsonBuilder().startObject().field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()).get();
System.out.println(response.getId());
client.close();
} /**
* 创建索引:do it yourself
*
* @throws IOException
*/
@Test
public void test2() throws IOException {
String json = "{" + "\"user\":\"kimchy\","
+ "\"postDate\":\"2013-01-30\","
+ "\"message\":\"trying out Elasticsearch\"" + "}";
IndexResponse response = client.prepareIndex("twitter", "tweet")
.setSource(json).get();
System.out.println(response.getId());
client.close();
} /**
* 创建索引:use map
*
* @throws IOException
*/
@Test
public void test3() throws IOException {
Map<String, Object> json = new HashMap<String, Object>();
json.put("user", "kimchy");
json.put("postDate", new Date());
json.put("message", "trying out Elasticsearch"); IndexResponse response = client.prepareIndex("twitter", "tweet")
.setSource(json).get();
System.out.println(response.getId());
client.close();
} /**
* 创建索引:serialize your beans
*
* @throws IOException
*/
@Test
public void test4() throws IOException {
User user = new User();
user.setUser("kimchy");
user.setPostDate(new Date());
user.setMessage("trying out Elasticsearch"); // instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse // generate json
byte[] json = mapper.writeValueAsBytes(user); IndexResponse response = client.prepareIndex("twitter", "tweet")
.setSource(json).get();
System.out.println(response.getId());
client.close();
} /**
* 查询索引:get
*
* @throws IOException
*/
@Test
public void test5() throws IOException {
GetResponse response = client.prepareGet("twitter", "tweet", "1").get();
System.out.println(response.getSourceAsString()); client.close();
} /**
* 删除索引:delete
*
* @throws IOException
*/
@Test
public void test6() throws IOException {
client.prepareDelete("twitter", "tweet", "1").get();
client.close();
} /**
* 更新索引:Update API-UpdateRequest
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test7() throws IOException, InterruptedException,
ExecutionException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("twitter");
updateRequest.type("tweet");
updateRequest.id("AVyi3OORot7zkId708s8");
updateRequest.doc(jsonBuilder().startObject().field("gender", "male")
.endObject());
client.update(updateRequest).get();
System.out.println(updateRequest.version());
client.close();
} /**
* 更新索引:Update API-prepareUpdate()-doc
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test8() throws IOException, InterruptedException,
ExecutionException {
client.prepareUpdate("twitter", "tweet", "AVyikSKIot7zkId708s6")
.setDoc(jsonBuilder().startObject().field("gender", "female")
.endObject()).get();
client.close();
} /**
* 更新索引:Update API-prepareUpdate()-script
* 需要开启:script.engine.groovy.inline.update: on
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test9() throws IOException, InterruptedException,
ExecutionException {
client.prepareUpdate("twitter", "tweet", "AVyi4oZfot7zkId708s-")
.setScript(
new Script("ctx._source.gender = \"female\"",
ScriptService.ScriptType.INLINE, null, null))
.get();
client.close();
} /**
* 更新索引:Update API-UpdateRequest-upsert
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test10() throws IOException, InterruptedException,
ExecutionException {
IndexRequest indexRequest = new IndexRequest("twitter", "tweet", "1")
.source(jsonBuilder()
.startObject()
.field("name", "Joe Smith")
.field("gender", "male")
.endObject());
UpdateRequest updateRequest = new UpdateRequest("twitter", "tweet", "1")
.doc(jsonBuilder()
.startObject()
.field("gender", "female")
.endObject()).upsert(indexRequest);
client.update(updateRequest).get();
client.close();
} /**
* 批量查询索引:Multi Get API
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test11() throws IOException, InterruptedException,
ExecutionException {
MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
.add("twitter", "tweet", "1")
.add("twitter", "tweet", "AVyi4oZfot7zkId708s-", "AVyi3OORot7zkId708s8", "AVyikSKIot7zkId708s6")
.add("djt2", "user", "1")
.get(); for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
GetResponse response = itemResponse.getResponse();
if (response.isExists()) {
String json = response.getSourceAsString();
System.out.println(json);
}
}
client.close();
} /**
* 批量操作索引:Bulk API
*
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test12() throws IOException, InterruptedException,
ExecutionException {
BulkRequestBuilder bulkRequest = client.prepareBulk(); // either use client#prepare, or use Requests# to directly build index/delete requests
bulkRequest.add(client.prepareIndex("twitter", "tweet", "3")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
); bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "another post")
.endObject()
)
);
DeleteRequestBuilder prepareDelete = client.prepareDelete("twitter", "tweet", "AVyikSKIot7zkId708s6");
bulkRequest.add(prepareDelete); BulkResponse bulkResponse = bulkRequest.get();
//批量操作:其中一个操作失败不影响其他操作成功执行
if (bulkResponse.hasFailures()) {
// process failures by iterating through each bulk response item
BulkItemResponse[] items = bulkResponse.getItems();
for (BulkItemResponse bulkItemResponse : items) {
System.out.println(bulkItemResponse.getFailureMessage());
}
}else{
System.out.println("bulk process success!");
}
client.close();
} /**
* 批量操作索引:Using Bulk Processor
* 优化:先关闭副本,再添加副本,提升效率
* @throws IOException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void test13() throws IOException, InterruptedException,
ExecutionException { BulkProcessor bulkProcessor = BulkProcessor.builder(
client,
new BulkProcessor.Listener() { public void beforeBulk(long executionId, BulkRequest request) {
// TODO Auto-generated method stub
System.out.println(request.numberOfActions());
} public void afterBulk(long executionId, BulkRequest request,
Throwable failure) {
// TODO Auto-generated method stub
System.out.println(failure.getMessage());
} public void afterBulk(long executionId, BulkRequest request,
BulkResponse response) {
// TODO Auto-generated method stub
System.out.println(response.hasFailures());
}
})
.setBulkActions(1000) // 每个批次的最大数量
.setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB))// 每个批次的最大字节数
.setFlushInterval(TimeValue.timeValueSeconds(5))// 每批提交时间间隔
.setConcurrentRequests(1) //设置多少个并发处理线程
//可以允许用户自定义当一个或者多个bulk请求失败后,该执行如何操作
.setBackoffPolicy(
BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
.build();
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}"; for (int i = 0; i < 1000; i++) {
bulkProcessor.add(new IndexRequest("djt6", "user").source(json));
}
//阻塞至所有的请求线程处理完毕后,断开连接资源
bulkProcessor.awaitClose(3, TimeUnit.MINUTES);
client.close();
}
/**
* SearchType使用方式
* @throws Exception
*/
@Test
public void test14() throws Exception {
SearchResponse response = client.prepareSearch("djt")
.setTypes("user")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
// .setSearchType(SearchType.QUERY_AND_FETCH)
.execute()
.actionGet();
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
}
}

先创建索引库djt1

curl -XPUT 'master:9200/djt1/user/1' -d '{"name":"tom","age":18, "info":"tom"}'
curl -XPUT 'master:9200/djt1/user/2' -d '{"name":"jack","age":29, "info":"jack"}'
curl -XPUT 'master:9200/djt1/user/3' -d '{"name":"jetty","age":18, "info":"jetty"}'
curl -XPUT 'master:9200/djt1/user/4' -d '{"name":"daival","age":19, "info":"daival"}'
curl -XPUT 'master:9200/djt1/user/5' -d '{"name":"lilei","age":18, "info":"lilei"}'
curl -XPUT 'master:9200/djt1/user/6' -d '{"name":"lili","age":29, "info":"lili"}'
curl -XPUT 'master:9200/djt1/user/7' -d '{"name":"tom1","age":18, "info":"tom1"}'
curl -XPUT 'master:9200/djt1/user/8' -d '{"name":"tom2","age":19, "info":"tom2"}'
curl -XPUT 'master:9200/djt1/user/9' -d '{"name":"tom3","age":11, "info":"tom3"}'
curl -XPUT 'master:9200/djt1/user/10' -d '{"name":"tom4","age":18, "info":"tom4"}'
curl -XPUT 'master:9200/djt1/user/11' -d '{"name":"tom5","age":18, "info":"tom5"}'
curl -XPUT 'master:9200/djt1/user/12' -d '{"name":"tom6","age":18, "info":"tom6"}'
curl -XPUT 'master:9200/djt1/user/13' -d '{"name":"tom7","age":18, "info":"tom7"}'
curl -XPUT 'master:9200/djt1/user/14' -d '{"name":"tom8","age":18, "info":"tom8"}'
curl -XPUT 'master:9200/djt1/user/15' -d '{"name":"tom9","age":18, "info":"tom9"}'
curl -XPUT 'master:9200/djt1/user/16' -d '{"name":"john","age":18, "info":"john"}'
curl -XPUT 'master:9200/djt1/user/17' -d '{"name":"marry","age":38, "info":"marry"}'
curl -XPUT 'master:9200/djt1/user/18' -d '{"name":"jjk1","age":18, "info":"jjk1"}'

执行程序(这里是条件匹配查询)

这里是把djt1库的所以数据都查出来

对多字段查询

模糊字符匹配查询方法

根据权重来查询

完全匹配查询

从0开始只查询2条数据

对年龄进行升序查询2条数据

查询某个年龄段的10条数据

参考代码 ESTestQuery.java

package com.dajiangtai.djt_spider.elasticsearch;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map; 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.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Before;
import org.junit.Test; /**
* Query 操作
*
* @author 大讲台
*
*/
public class ESTestQuery {
private TransportClient client; @Before
public void test0() throws UnknownHostException { // 开启client.transport.sniff功能,探测集群所有节点
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "escluster")
.put("client.transport.sniff", true).build();
// on startup
// 获取TransportClient
client = TransportClient
.builder()
.settings(settings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("master"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave1"), 9300))
.addTransportAddress(
new InetSocketTransportAddress(InetAddress
.getByName("slave2"), 9300));
}
/**
* 查询:query
* 分页:from to
* 排序:sort
* 过滤:filter
*
* @throws Exception
*/
@Test
public void test1() throws Exception {
SearchRequestBuilder builder = client.prepareSearch("djt1");
builder//.setQuery(QueryBuilders.matchQuery("info", "marry and john"))
.setQuery(QueryBuilders.matchAllQuery())
// .setQuery(QueryBuilders.multiMatchQuery("john", "name","info"))
// .setQuery(QueryBuilders.queryStringQuery("name:tom*"))
//.setQuery(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name","tom").boost(3.0f)).should(QueryBuilders.matchQuery("age",18).boost(1.0f)))
//.setQuery(QueryBuilders.termQuery("info", "tom"))
.setFrom(0)
.setSize(10)
.addSort("age", SortOrder.ASC)
.setPostFilter(QueryBuilders.rangeQuery("age").from(18).to(32))
.setExplain(false)
;
SearchResponse searchResponse = builder.get();
SearchHits hits = searchResponse.getHits();
Map<String, Object> map = new HashMap<String, Object>();
SearchHit[] hits2 = hits.getHits();
for (SearchHit searchHit : hits2) {
System.out.println(searchHit.getSource().toString());
}
}
}

ElasticSearch 搜索原理的更多相关文章

  1. Elasticsearch工作原理

    一.关于搜索引擎 各位知道,搜索程序一般由索引链及搜索组件组成. 索引链功能的实现需要按照几个独立的步骤依次完成:检索原始内容.根据原始内容来创建对应的文档.对创建的文档进行索引. 搜索组件用于接收用 ...

  2. 【沽泡学院07】基于ElasticSearch搜索附近的人

    1. 为什么要选择ElasticSearch 1)ElasticSearch 优点: 分布式.实时的.Push replication 完全支持Apache Lucene的接近实时的搜索 处理多租户( ...

  3. Google 以图搜图 - 相似图片搜索原理 - Java实现

    前阵子在阮一峰的博客上看到了这篇<相似图片搜索原理>博客,就有一种冲动要将这些原理实现出来了. Google "相似图片搜索":你可以用一张图片,搜索互联网上所有与它相 ...

  4. Java爬虫搜索原理实现

    permike 原文 Java爬虫搜索原理实现 没事做,又研究了一下爬虫搜索,两三天时间总算是把原理闹的差不多了,基本实现了爬虫搜索的原理,本次实现还是俩程序,分别是按广度优先和深度优先完成的,广度优 ...

  5. Google 以图搜图 - 相似图片搜索原理 - Java实现 (转)

    前阵子在阮一峰的博客上看到了这篇<相似图片搜索原理>博客,就有一种冲动要将这些原理实现出来了. Google "相似图片搜索":你可以用一张图片,搜索互联网上所有与它相 ...

  6. 一次 ElasticSearch 搜索优化

    一次 ElasticSearch 搜索优化 1. 环境 ES6.3.2,索引名称 user_v1,5个主分片,每个分片一个副本.分片基本都在11GB左右,GET _cat/shards/user 一共 ...

  7. ElasticSearch搜索介绍四

    ElasticSearch搜索 最基础的搜索: curl -XGET http://localhost:9200/_search 返回的结果为: { "took": 2, &quo ...

  8. Elasticsearch搜索结果返回不一致问题

    一.背景 这周在使用Elasticsearch搜索的时候遇到一个,对于同一个搜索请求,会出现top50返回结果和排序不一致的问题.那么为什么会出现这样的问题? 后来通过百度和google,发现这是因为 ...

  9. ElasticStack学习(六):ElasticSearch搜索初探

    一.ElasticSearch搜索介绍 1.ElasticSearch搜索方式主要分为以下两种: 1).URI Search:此种查询主要是使用Http的Get方法,在URL中使用查询参数进行查询: ...

随机推荐

  1. MySQL性能优化方法二:表结构优化

    原文链接:http://isky000.com/database/mysql-perfornamce-tuning-schema 很多人都将 数据库设计范式 作为数据库表结构设计“圣经”,认为只要按照 ...

  2. linux 读取物理寄存器

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h ...

  3. top command-linux下用top命令查看cpu利用率超过100%

    1.  这里显示的所有的cpu加起来的使用率,说明你的CPU是多核,你运行top后按大键盘1看看,可以显示每个cpu的使用率,top里显示的是把所有使用率加起来;    2.查看CPU信息; cat ...

  4. Ubuntu忘记root密码的解决方法

    如果是Linux操作系统的话,其实也是很简单 -- 单用户登陆.下面以Ubuntu14.04来简单演示一下具体的操作流程. 1. 开机 2. 此时会有一个选项:Advanced Options for ...

  5. 多名历年高考状元最终选择做中国顶尖黑客 - imsoft.cnblogs

    原标题:揭秘中国顶尖黑客:二分之一是历年高考[微博]状元 “二分之一是各地的历年高考状元,二分之一是数学专业,二分之一来自微软.” Keen这样的团队是坚决与黑产划清界限的,这也是圈内的“道德洁癖”, ...

  6. ZZNU 2125:A + B 普拉斯(傻逼题+大数加法)

    2125: A + B 普拉斯 时间限制: 1 Sec  内存限制: 128 MB 提交: 94  解决: 28 [提交] [状态] [讨论版] [命题人:admin] 题目描述 "别人总说 ...

  7. spfa【模板】

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  8. systemd学习笔记

    一.systemd介绍 systemd即为system daemon,是linux下的一种init软件与多数发行版使用的System V风格init相比,systemd采用了以下新技术: (1) 采用 ...

  9. Apache2.4配置(全)

    http://blog.csdn.net/u012291157/article/details/46492137

  10. hadoop之 Yarn 调度器Scheduler详解

    概述 集群资源是非常有限的,在多用户.多任务环境下,需要有一个协调者,来保证在有限资源或业务约束下有序调度任务,YARN资源调度器就是这个协调者. YARN调度器有多种实现,自带的调度器为Capaci ...