elasticsearch(一) 基本知识

来源:互联网 发布:2016大学生就业率数据 编辑:程序博客网 时间:2024/05/17 22:15

1.请求类型

增/改    put
删         delete
查         get/post

因为有些不支持get带请求体的场景,所以可以用post做查询。用post做查询请求注意连接最后要带有_search,否则将成为存储数据请求

2.重要概念

   ES 关系数据库

_index 索引---------------库

_type类型-----------------表

_id文档id------------------主键

3.ES查询两种类型Filter和Query

 filter从字面就知道,就是起过滤作用,解决的二值问题,匹配还是不匹配。没有评分过程
  query是匹配程度问题,有屏风过程
  filter和query还有性能上差异,一方面es底层对filter做了很多优化,会对过滤结果进行缓存。同事filter没有评分,也就是不用做相关性计算过程。

4.查询语法结构

使用query查询:{"query":{..........}}

使用filter查询:{"filter":{................}}

query和filter同时使用:{"query":{"filtered":{.............}}}

5.多个条件组合查询使用bool包裹

must:[.........]                   内部多个条件都满足

must_not:[.........]           内部多个条件都不满足

should:[...............]          内部多个条件只要满足一个

这里注意,must、must_not、should后是[]数组,其中包含多个条件

6.日期

ES默认日期识别的UTC格式的日期,即如果把UTC格式日期字符串存储到es,es会自定把该字段识别为日期格式,但是这种格式不是

我们常用的格式,这种格式带有时区容易出错。所以我们可以通过给type设置mapping来指定日期格式

@Testpublic void adminMapping() throws IOException{//首先创建索引IndicesAdminClient indices = client.admin().indices();indices.prepareCreate("dev").execute().actionGet();//构建mappingXContentBuilder jsonBuilder = XContentFactory.jsonBuilder();jsonBuilder .startObject()             .startObject("employee")    //类型         .startObject("properties")                     .startObject("name")                  .field("type", "string")              .endObject()                               .startObject("age")                  .field("type", "integer")              .endObject()                             .startObject("address")                  .field("type", "string")             .endObject()              .startObject("test_birthday")                  .field("type", "date")                .field("format","yyyy-MM-dd HH:mm:ss") //指定日期格式           .endObject()                      .endObject()            .endObject()          .endObject();//创建mappingPutMappingRequestBuilder putMappingRequestBuilder = client.admin().indices().preparePutMapping("dev").setType("employee").setSource(jsonBuilder);PutMappingResponse actionGet = putMappingRequestBuilder.execute().actionGet();System.out.println(actionGet.toString());}


7.常用重要查询

http://lxwei.github.io/posts/Elasticsearch-%E7%AE%80%E4%BB%8B.html

0 0
原创粉丝点击