沙师弟,师父的充电器掉了

java使用HTTP Rest client 客户端Jest连接操作es,功能很强大

kl 发表了文章 • 6 个评论 • 27238 次浏览 • 2016-03-28 23:30 • 来自相关话题

前言

在了解jest框架前,楼主一直尝试用官方的Elasticsearch java api连接es服务的,可是,不知何故,一直报如下的异常信息,谷歌了很久,都说是jvm版本不一致导致的问题,可我是本地测试的,jvm肯定是一致的,这个问题现在都木有解决,but,这怎么能阻止我探索es的脚步呢,so,让我发现了jest 这个框架   


org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream Caused by: org.elasticsearch.transport.TransportSerializationException: Failed to deserialize exception response from stream
我的测试代码是参考官方api实例的,官方api地址:Elasticsearch java api,代码如下:



Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300)); QueryBuilder queryBuilder = QueryBuilders.termQuery("content", "搜"); SearchResponse searchResponse = client.prepareSearch("indexdata").setTypes("fulltext") .setQuery(queryBuilder) .execute() .actionGet(); SearchHits hits = searchResponse.getHits(); System.out.println("查询到记录数:" + hits.getTotalHits()); SearchHit[] searchHists = hits.getHits(); for(SearchHit sh : searchHists){ System.out.println("content:"+sh.getSource().get("content")); } client.close();
如果有人知道怎么回事,告诉一下楼主吧,让楼主坑的明白,感激不尽了,我的es版本是2.2.0


进入正题

了解jest

jest是一个基于 HTTP Rest 的连接es服务的api工具集,功能强大,能够使用es java api的查询语句,项目是开源的,github地址:https://github.com/searchbox-io/Jest




我的测试用例

分词器:ik,分词器地址:https://github.com/medcl/elasticsearch-analysis-ik ,es的很多功能都是基于插件提供的,es版本升级都2.2.0后,安装插件的方式不一样了,如果你安装ik分词插件有问题,请点击右上角的qq联系博主

新建索引

curl -XPUT http://localhost:9200/indexdata


创建索引的mapping,指定分词器

curl -XPOST http://localhost:9200/indexdata/fulltext/_mapping

{
  "fulltext": {
    "_all": {
      "analyzer": "ik_max_word",
      "search_analyzer": "ik_max_word",
      "term_vector": "no",
      "store": "false"
    },
    "properties": {
      "content": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      },
      "description": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      },
      "title": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      },
      "keyword": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "include_in_all": "true",
        "boost": 8
      }
    }
  }
}

mapping信息可以用head插件查看,如下


导入数据和查询,看代码吧


@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ElasticSearchTestApplication.class) public class JestTestApplicationTests { @Autowired private KlarticleDao klarticleDao; //得到JestClient实例 public JestClient getClient()throws Exception{ JestClientFactory factory = new JestClientFactory(); factory.setHttpClientConfig(new HttpClientConfig .Builder("http://127.0.0.1:9200&quot;) .multiThreaded(true) .build()); return factory.getObject(); } /** * 导入数据库数据到es * @throws Exception */ @Test public void contextLoads() throws Exception{ JestClient client=getClient(); Listlists=klarticleDao.findAll(); for(Klarticle k:lists){ Index index = new Index.Builder(k).index("indexdata").type("fulltext").id(k.getArcid()+"").build(); System.out.println("添加索引----》"+k.getTitle()); client.execute(index); } //批量新增的方式,效率更高 Bulk.Builder bulkBuilder = new Bulk.Builder(); for(Klarticle k:lists){ Index index = new Index.Builder(k).index("indexdata").type("fulltext").id(k.getArcid()+"").build(); bulkBuilder.addAction(index); } client.execute(bulkBuilder.build()); client.shutdownClient(); } //搜索测试 @Test public void JestSearchTest()throws Exception{ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("content", "搜索")); Search search = new Search.Builder(searchSourceBuilder.toString()) // multiple index or types can be added. .addIndex("indexdata") .build(); JestClient client =getClient(); SearchResult result= client.execute(search); // List> hits = result.getHits(Klarticle.class); Listarticles = result.getSourceAsObjectList(Klarticle.class); for(Klarticle k:articles){ System.out.println("------->:"+k.getTitle()); } } }下面是依赖的jar,maven项目<!--jest依赖--> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>2.0.0</version> </dependency> <!--jest 日志依赖--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.2.0</version> </dependency> </dependencies>
去我的博客查看原文:http://www.kailing.pub/article/index/arcid/84.html

大家聊一聊使用的什么版本的Elasticsearch,看看Elasticsearch版本变化

kl 发表了文章 • 4 个评论 • 7260 次浏览 • 2016-03-28 19:12 • 来自相关话题

我是最近从lucene过渡Elasticsearch的,直接用的最新的2.2.0版本的。发现离线安装插件的方式和以前不一样了,一些配置也有改变,最大的问题是java client api 连接报了如下的异常,我是参照官方api测试的,地址:https://www.elastic.co/guide/e ... .html
org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream
谷歌都说是服务和客户端的jvm不一致,我是本机环境测试的,所以,现在这个问题都还没解决,有遇到过的么,还是和版本有关系啊

ES处理大数据插入运行几个小时候出现性能问题

helloes 回复了问题 • 2 人关注 • 1 个回复 • 12044 次浏览 • 2016-03-28 17:09 • 来自相关话题

拼音搜索+中文搜索

niweiyi1314 回复了问题 • 11 人关注 • 5 个回复 • 22780 次浏览 • 2018-04-26 14:08 • 来自相关话题

从mysql同步数据到es方法大讨论

sunyizhen 回复了问题 • 5 人关注 • 3 个回复 • 13737 次浏览 • 2016-08-04 17:23 • 来自相关话题

ElasticsearchTimeoutException问题

helloes 回复了问题 • 2 人关注 • 1 个回复 • 9692 次浏览 • 2016-03-24 02:10 • 来自相关话题

如何使用自定义的 Similarity插件

smile_sunshine 回复了问题 • 2 人关注 • 2 个回复 • 6381 次浏览 • 2016-03-24 21:04 • 来自相关话题

elasticsearch 如何计算 TP50 TP95 TP99 这样的值?

medcl 回复了问题 • 3 人关注 • 2 个回复 • 8965 次浏览 • 2016-03-24 10:38 • 来自相关话题

使用spark向elasticsearch中写入数据异常

joe23_2006 回复了问题 • 6 人关注 • 6 个回复 • 21122 次浏览 • 2018-01-31 13:36 • 来自相关话题

Elasticsearch 2.2 集群配置

Jea 回复了问题 • 6 人关注 • 9 个回复 • 5351 次浏览 • 2017-04-14 13:45 • 来自相关话题

aggregation 统计用户一天之内登录次数大于n次的用户列表, 这个应该怎么写聚合json

ggchangan 回复了问题 • 4 人关注 • 2 个回复 • 6238 次浏览 • 2016-03-22 15:32 • 来自相关话题

es多字段匹配 按字段匹配情况自定义得分

Jea 回复了问题 • 1 人关注 • 3 个回复 • 6257 次浏览 • 2016-03-24 08:01 • 来自相关话题

elasticsearch 文件句柄问题

elastic 回复了问题 • 3 人关注 • 2 个回复 • 5215 次浏览 • 2016-03-24 10:31 • 来自相关话题

es索引模版配置不当导致的aggs聚合查询字段显示错误的问题

Max 发表了文章 • 0 个评论 • 8953 次浏览 • 2016-03-18 16:51 • 来自相关话题

今天在es中对http日志的状态码status进行aggs搜索出现字段内容显示不正常的问题,记录过程:

http日志的情况:
1、http日志从logstash写入es时,状态码配置为status,其内容为 200 ,302 ,400 ,404等。
2、使用kibana对该日志的索引进行查询,在discover页面中显示的status内容跟logstash的内容一致,是正常的。

出现问题的场景:
(我这里使用的是kibana的sense插件进行的查询,如果直接使用curl python-ES也是一样的)
查询该索引:
POST http-2016.03.18/_search
{
  "fields": ["status"],
          "query":{
            "bool":{
              "must": [
                {
                  "range" : {
                    "@timestamp" : {"gte" : "now-5m"}
                  }
                }
              ]
            }
          },
          "_source": "false",
          "size": 0,
          "aggs": {
            "status_type": {
              "terms":{"field":"status"}
            }
          }
}

查询返回的结果中aggregations部分的内容:
"aggregations" : {
    "status_type" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : -56,
        "doc_count" : 376341
      }, {
        "key" : 46,
        "doc_count" : 51439
      }, {
        "key" : 45,
        "doc_count" : 5543
      }, {
        "key" : 48,
        "doc_count" : 1669
      }, {
        "key" : -108,
        "doc_count" : 1068
      }, {
        "key" : -50,
        "doc_count" : 11
      }, {
        "key" : -109,
        "doc_count" : 8
      }, {
        "key" : -112,
        "doc_count" : 4
      } 

寻找原因:
起先先去掉了查询的aggs部分,单独查询query的内容:
POST http-2016.03.18/_search
{
  "fields": ["status"],
          "query":{
            "bool":{
              "must": [
                {
                  "range" : {
                    "@timestamp" : {"gte" : "now-5m"}
                  }
                }
              ]
            }
          }
}

返回的结果中,hits显示的status字段内容是正常的:
"hits": {
    "total": 1242104,
    "max_score": 1,
    "hits": [
      {
        "_index": "http-2016.03.18",
        "_type": "log",
        "_id": "AVOI3EiwidwPAhB1e7gQ",
        "_score": 1,
        "fields": {
          "status": [
            "200"
          ]
        }
      }
    ......

然后查询了http索引的索引信息和模版配置:
GET /http-2016.03.18/
GET /_template/http
发现其中http的status的属性type类型的内容是byte :
        "properties": {
          "@timestamp": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
        ......
        ......
          "status": {
            "type": "byte"
          },
        ......
        ......

原因:
在aggs查询中发现了status字段显示错误的情况,status的type类型在es模版中定义成了byte类型,当status的值超过127后将出现溢出的情况,因此修改为short后,恢复了正常。
(对于http的状态码status,其type类型使用short已经足够了,如果使用integer,long或默认的string类型也是可以的,这里影响的是存储空间占用的大小。)
 
 

elasticsearch nested 会增加doc数目吗? 原理是什么

helloes 回复了问题 • 2 人关注 • 1 个回复 • 6293 次浏览 • 2016-03-18 11:42 • 来自相关话题