- 浏览: 146058 次
文章分类
最新评论
-
x_looking:
Client client = new TransportCl ...
ELASTICSEARCH常见问题 -
辣de冷wmyes:
ElasticSearch视频教程百度网盘地址:http:// ...
一、Elasticsearch安装使用教程
http://outofmemory.cn/code-snippet/3780/Java-Client-call-ElasticSearch-do-quanwen-search-code-example
另外一个例子:
package com.es; import java.io.IOException; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentFactory; public class Test { public static void main(String[] args) throws ElasticsearchException, IOException { Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", "elasticsearch_useraudit").build(); Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("192.168.41.128", 9301)); IndexResponse response = client.prepareIndex("comment_index", "comment_ugc", "comment_123674") .setSource( XContentFactory.jsonBuilder() .startObject() .field("author", "569874") .field("author_name", "riching") .field("mark", 232) .field("body", "北京不错,但是人太多了") .field("createDate", "20130801175520") .field("valid", true) .endObject()) .setTTL(8000) .execute().actionGet(); System.out.println(response.getId()); } }
另外一个例子:
package com.es; import java.util.ArrayList; import java.util.List; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 上午11:34:04 * * @function: TODO * */ public class ElasticSearchHandler { private Client client; public ElasticSearchHandler(){ //使用本机做为节点 this("192.168.41.128"); } public ElasticSearchHandler(String ipAddress){ //集群连接超时设置 /* Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.ping_timeout", "10s").build(); client = new TransportClient(settings); */ // client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("192.168.41.128", 9301)); Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", "elasticsearch_useraudit").build(); client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("192.168.41.128", 9301)); } /** * 建立索引,索引建立好之后,会在elasticsearch-0.20.6\data\elasticsearch\nodes\0创建所以你看 * @param indexName 为索引库名,一个es集群中可以有多个索引库。 名称必须为小写 * @param indexType Type为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。 * @param jsondata json格式的数据集合 * * @return */ public void createIndexResponse(String indexname, String type, List<String> jsondata){ //创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据 IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type).setRefresh(true); for(int i=0; i<jsondata.size(); i++){ requestBuilder.setSource(jsondata.get(i)).execute().actionGet(); } } /** * 创建索引 * @param client * @param jsondata * @return */ public IndexResponse createIndexResponse(String indexname, String type,String jsondata){ IndexResponse response = client.prepareIndex(indexname, type) .setSource(jsondata) .execute() .actionGet(); return response; } /** * 执行搜索 * @param queryBuilder * @param indexname * @param type * @return */ public List<Medicine> searcher(QueryBuilder queryBuilder, String indexname, String type){ List<Medicine> list = new ArrayList<Medicine>(); SearchResponse searchResponse = client.prepareSearch(indexname).setTypes(type) .setQuery(queryBuilder) .execute() .actionGet(); SearchHits hits = searchResponse.getHits(); System.out.println("查询到记录数=" + hits.getTotalHits()); SearchHit[] searchHists = hits.getHits(); if(searchHists.length>0){ for(SearchHit hit:searchHists){ Integer id = (Integer)hit.getSource().get("id"); String name = (String) hit.getSource().get("name"); String function = (String) hit.getSource().get("funciton"); list.add(new Medicine(id, name, function)); } } return list; } public static void main(String[] args) { ElasticSearchHandler esHandler = new ElasticSearchHandler(); List<String> jsondata = DataFactory.getInitJsonData(); String indexname = "indexdemo"; String type = "typedemo"; esHandler.createIndexResponse(indexname, type, jsondata); //查询条件 // QueryBuilder queryBuilder = QueryBuilders.inQuery("name", "感冒"); QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("id", 1)); List<Medicine> result = esHandler.searcher(queryBuilder, indexname, type); for(int i=0; i<result.size(); i++){ Medicine medicine = result.get(i); System.out.println("(" + medicine.getId() + ")药品名称:" +medicine.getName() + "\t\t" + medicine.getFunction()); } } }
package com.es; import java.util.ArrayList; import java.util.List; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 上午11:38:15 * * @function: TODO * */ public class DataFactory { public static DataFactory dataFactory = new DataFactory(); private DataFactory(){ } public DataFactory getInstance(){ return dataFactory; } public static List<String> getInitJsonData(){ List<String> list = new ArrayList<String>(); String data1 = JsonUtil.obj2JsonData(new Medicine(1,"银花 感冒 颗粒","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。")); String data2 = JsonUtil.obj2JsonData(new Medicine(2,"感冒 止咳糖浆","功能主治:感冒止咳糖浆,解表清热,止咳化痰。")); String data3 = JsonUtil.obj2JsonData(new Medicine(3,"感冒灵颗粒","功能主治:解热镇痛。头痛 ,清热。")); String data4 = JsonUtil.obj2JsonData(new Medicine(4,"感冒 灵胶囊","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。")); String data5 = JsonUtil.obj2JsonData(new Medicine(5,"仁和 感冒 颗粒","功能主治:疏风清热,宣肺止咳,解表清热,止咳化痰。")); list.add(data1); list.add(data2); list.add(data3); list.add(data4); list.add(data5); return list; } }
package com.es; import java.io.IOException; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 上午11:34:56 * * @function: TODO * */ public class JsonUtil { /** * 实现将实体对象转换成json对象 * @param medicine Medicine对象 * @return */ public static String obj2JsonData(Medicine medicine){ String jsonData = null; try { //使用XContentBuilder创建json数据 XContentBuilder jsonBuild = XContentFactory.jsonBuilder(); jsonBuild.startObject() .field("id",medicine.getId()) .field("name", medicine.getName()) .field("funciton",medicine.getFunction()) .endObject(); jsonData = jsonBuild.string(); System.out.println(jsonData); } catch (IOException e) { e.printStackTrace(); } return jsonData; } }
package com.es; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private Integer id; private String name; private String function; public Medicine() { super(); } public Medicine(Integer id, String name, String function) { super(); this.id = id; this.name = name; this.function = function; } //getter and setter () public String getFunction() { return function; } public void setFunction(String function) { this.function = function; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
发表评论
-
使用 Scripted Metric Aggregation 遇到的问题
2016-02-19 14:12 2403使用Scripted Metric Aggregation进行 ... -
ElasticSearch性能优化策略
2016-02-17 19:42 3489ElasticSearch性能优化主要分为4个方面的优化。 一 ... -
Elasticsearch 数据建模 - 处理关联关系
2016-02-17 14:40 1021数据建模(Modeling Your Data ... -
数据类型转换错误
2016-02-15 18:28 1555异常: Invalid shift value (64) in ... -
elasticsearch 排序异常,关键字冲突
2016-02-03 12:35 5806org.elasticsearch.action.search ... -
ElasticSearch不同类型下同名字段排序错误
2016-02-03 11:18 1895虽然之前知道elasticsearch不同类型下同名字段要慎用 ... -
ElasticSearch immense term错误
2016-02-02 19:07 2570在使用ElasticSearch的过程中遇到了一个immens ... -
ELASTICSEARCH常见问题
2015-09-22 15:39 31581. ELASTICSEARCH建索引过程中崩溃问题追查 by ... -
routing实例1
2015-09-21 18:51 0package com.eg.part1; import j ... -
routing实例
2015-09-21 18:49 0创建索引: PUT /useraudit_v1 创建索引别名: ... -
elasticsearch 精确,模糊查询实例
2015-09-19 23:55 62518实例 http://www.bubuko.com/infode ... -
[维护]Elasticsearch零停机时间更新索引配置或迁移索引
2015-09-18 10:16 751另外一篇文章:http://blog.csdn.net/dm_ ... -
elasticsearch 的mapping定义
2015-09-18 10:16 1164elasticsearch 的mapping 例子一: 订单 ... -
ElasticSearch基础杂烩-配置-索引-优化
2015-09-19 23:56 600http://blog.csdn.net/huwei2003/ ... -
Elasticsearch安装中文分词插件ik
2015-09-18 10:15 1987安装步骤: 1、到github ... -
ElasticSearch的各种服务的URL
2015-09-18 10:15 492前言 elasticsearch 将各种功能、配置、服务都以A ... -
ElasticSearch集群搭建
2015-09-19 23:56 540http://www.linuxidc.com/Linux/2 ... -
elasticsearch 配置
2015-09-18 10:16 665elasticsearch.conf 主要是设置一些java运 ... -
elasticsearch中文分词集成
2015-09-17 09:35 565elasticsearch官方只提供smartcn这个中文分词 ... -
elasticsearch的Mapping定义
2015-09-17 09:35 1369Mapping,就是对索引库中 ...
相关推荐
消费kafka数据,然后批量导入到Elasticsearch,本例子使用的kafka版本0.10,es版本是6.4,使用bulk方式批量导入到es中,也可以一条一条的导入,不过比较慢。 <groupId>org.elasticsearch <artifactId>elastic...
NULL 博文链接:https://m635674608.iteye.com/blog/2231884
Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。该Demo包含了大部分的java应用ES语法......
使用过Elasticsearch RestFul API的都知道,在Java端使用是ES服务需要创建Java Client,但是每一次连接都实例化一个client,对系统的消耗很大,而且最令人头疼的是它的连接非常慢。所以为了解决上述问题并提高client...
这是最近在学习ElasticSearch的过程中,自己练习和积累的一些Java代码,使用ElasticSearch5.1.1版本 + IntelliJ IDEA开发,在代码中调用了ElasticSearch5.1.1中的最新的API。
基于elasticsearch使用Java创建索引、更新索引、删除索引、查询索引的一个小例子 http://blog.csdn.net/lom9357bye/article/details/52841711
本实例属于简单易上手并且能够拿来就用的SpringBoot ES 项目,全文使用的是ElasticsearchTemplate进行开发。 本实例涵盖ES中的各类操作,如索引操作、CRUD操作、批处理、结果排序、分页查询、检索查询、关键字查询、...
本压缩包有elasticsearch-6.2.4.tar、elasticsearch-analysis-ik-6.2.4、kibana-6.2.4-windows-x86_64压缩包和详细的安装教程安装以及启动后错误的修改以及配置等,还包括结合java进行操作的源码及教程,有任何问题...
本实例为博主原创,属于简单易上手并且能够拿来就用的SpringBoot ES 项目,全文使用的是ElasticsearchTemplate进行开发。 本实例涵盖ES中的各类操作,如索引操作、CRUD操作、批处理、结果排序、分页查询、检索查询、...
ElasticSearch的基本示例,使用JavaAPI制作的demo,能够实现ElasticSearch的基本功能,内含Google的protobuf消息方法方式的基本示例,均已测试通过。
使用过Elasticsearch RestFul API的都知道,在Java端使用是ES服务需要创建Java Client,但是每一次连接都实例化一个client,对系统的消耗很大,而且最令人头疼的是它的连接非常慢。所以为了解决上述问题并提高client...
自己实现的elasticsearch实例。根据java api,实现了创建索引和搜索,以后还进行软件的重构和优化,最后希望能把他当成一个组件来使用
用于创建使用Elasticsearch的集成测试的小型实用程序。 它不使用Node而是下载指定版本的Elasticsearch并在单独的过程中启动它。 它还允许您安装所需的插件,这在使用NodeBuilder时是不可能的。 该实用程序已使用1.x...
Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。 PHP基于ElasticSearch做搜索 在做搜索...
Spring配置支持使用基于Java的@Configuration类或ES客户端实例的XML名称空间。 ElasticsearchRestTemplate帮助程序类,可提高执行常规ES操作的效率。 包括文档和POJO之间的集成对象映射。 与Spring的转换服务集成...
这是一个使用Spring Boot,Elastic Search和Java Jest实现搜索的小项目。 要运行此演示,请按照以下步骤操作 安装Java并设置JAVA_HOME和PATH... 通过在Windows上运行bin \ elasticsearch.bat来启动Elasticsearch实例
主要介绍了java基于OpenGL ES实现渲染,实例分析了OpenGL渲染操作的相关技巧,需要的朋友可以参考下
es_log_manage Java推送ES相关API整合使用实例 说明 本项目为通过esclient与ES交互的常用API操作 模板创作 index创建/获取 数据推送 数据获取
Elasticsearch5.5.1使用JAVA客户端Jest操作的一些示例代码,详细介绍请参考文章:http://blog.csdn.net/u011781521/article/details/77852861