elasticsearch常用操作

3.3.1 Preparing a query 准备查询请求

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.SearchHit;

SearchResponse response = client.prepareSearch("library")
.addFields("title", "_source")
.execute().actionGet();
for(SearchHit hit: response.getHits().getHits()) {
     System.out.println(hit.getId());
     if (hit.getFields().containsKey("title")) {
            System.out.println("field.title: "+ hit.getFields().get("title").getValue());
     }
     System.out.println("source.title: " + hit.getSource().get("title"));
}

3.3.2 Building queries 构造查询

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

searchSourceBuilder.query(QueryBuilders.termQuery("sid_s", text));

Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex("webpage")

.build();

try {

SearchResult result = client.execute(search);

}catch(Exception e){

e.printStackTrace();

}

3.3.3 Using the match all documents query 匹配所有文档的查询

queryBuilder = QueryBuilders.matchAllQuery()
.boost(11f).normsField("title");

3.3.4 The match query 匹配查询

queryBuilder = QueryBuilders
.matchQuery("message", "a quick brown fox")
.operator(Operator.AND)
.zeroTermsQuery(ZeroTermsQuery.ALL);

3.3.5 Using the geo shape query 地理位置查询

queryBuilder = QueryBuilders.geoShapeQuery("location",
ShapeBuilder.newRectangle()
.topLeft(13, 53)
.bottomRight(14, 52)
.build());

3.3.6 Paging query 分页查询

SearchResponse response = client.prepareSearch("library")
.setQuery(QueryBuilders.matchAllQuery())
.setFrom(10)   //跳过前10个文档
.setSize(20)   //获取20个文档
.execute().actionGet();

response.getHits().totalHits()可以统计当前匹配到的结果数

3.3.7 Sorting  排序

searchSourceBuilder.fetchSource(null, "content").sort("_score");
		searchSourceBuilder.sort("date", SortOrder.DESC);

SortBuilders.scriptSort(script, type) //使用脚本来实现排序

SortBuilders.geoDistanceSort(fieldName) //根据空间距离来进行排序

提到距离问题,附带一篇博文:关于已知两点经纬度求球面最短距离的公式推导

3.3.8 Filtering 过滤

QueryBuilder filterBuilder = QueryBuilders
         .filteredQuery(
              QueryBuilders.existsQuery("title").queryName("exist"),
              QueryBuilders.termQuery("title", "elastic")
          );  

SearchResponse response = client.prepareSearch("library")
          .setPostFilter(filterBuilder)
          .execute().actionGet();  

3.3.10 Highlighting 高亮

SearchResponse response = client.prepareSearch("wikipedia")
.addHighlightedField("title")
.setQuery(QueryBuilders.termQuery("title", "actress"))
.setHighlighterPreTags("<1>", "<2>")
.setHighlighterPostTags("</1>", "</2>")
.execute().actionGet();
for(SearchHit hit: response.getHits().getHits()) {
    HighlightField hField = hit.getHighlightFields().get("title");
    for (Text t : hField.fragments()) {
           System.out.println(t.string());
    }
}

3.3.11 Suggestions 查询建议

SearchResponse response = client.prepareSearch("wikipedia")
.setQuery(QueryBuilders.matchAllQuery())
.addSuggestion(new TermSuggestionBuilder("first_suggestion")
.text("graphics designer")
.field("_all"))
.execute().actionGet();

for( Entry<? extends Option> entry : response.getSuggest().getSuggestion("first_suggestion").getEntries()) {
     System.out.println("Check for: " + entry.getText() + ". Options:");
     for( Option option : entry.getOptions()) {
          System.out.println("\t" + option.getText());
     }
}

3.3.12 Counting 统计

CountResponse response = client.prepareCount("library")
.setQuery(QueryBuilders.termQuery("title", "elastic"))
.execute().actionGet();

3.3.13 Scrolling 滚动

SearchResponse responseSearch = client.prepareSearch("library")
.setScroll("1m")
.setSearchType(SearchType.SCAN)
.execute().actionGet();
String scrollId = responseSearch.getScrollId();
SearchResponse response = client.prepareSearchScroll(scrollId).execute().actionGet();

3.3.14 Bulk 批量操作

BulkResponse response = client.prepareBulk()
.add(client.prepareIndex("library", "book", "5")
.setSource("{ \"title\" : \"Solr Cookbook\"}")
.request())
.add(client.prepareDelete("library", "book", "2").request()).execute().actionGet();

3.3.16 Multi GET  多GET

MultiGetResponse response = client.prepareMultiGet()
.add("library", "book", "1", "2")
.execute().actionGet();

3.3.16 Multi Search 多搜索

MultiSearchResponse response = client.prepareMultiSearch()
.add(client.prepareSearch("library", "book").request())
.add(client.prepareSearch("news").
.setPostFilter(QueryBuilders.termQuery("tags", "important")))
.execute().actionGet();

3.3.17 Building JSON queries and documents 构造JSON格式的查询和文档

IndexResponse response = client
.prepareIndex("library", "book", "2")
.setSource("{ \"title\": \"Mastering ElasticSearch\"}")
.execute().actionGet();

Map<String, Object> m = Maps.newHashMap();
m.put("1", "Introduction");
m.put("2", "Basics");
m.put("3", "And the rest");
XContentBuilder json = XContentFactory.jsonBuilder().prettyPrint()
.startObject()
.field("id").value("2123")
.field("lastCommentTime", new Date())
.nullField("published")
.field("chapters").map(m)
.field("title", "Mastering ElasticSearch")
.array("tags", "search", "ElasticSearch", "nosql")
.field("values")
.startArray()
.value(1)
.value(10)
.endArray()
.endObject();

3.4 The administration API

3.4.1.1 The cluster and indices health API 集群和索引健康状态

ClusterHealthResponse response = client.admin().cluster()
.prepareHealth("library")
.execute().actionGet();

3.4.1.2 The cluster state API 集群状态

ClusterStateResponse response = client.admin().cluster()
.prepareState()
.execute().actionGet();

3.4.1.3 The update settings API 设置更新

Map<String, Object> map = Maps.newHashMap();
map.put("indices.ttl.interval", "10m");
ClusterUpdateSettingsResponse response = client.admin().cluster()
.prepareUpdateSettings()
.setTransientSettings(map)
.execute().actionGet();

3.4.1.4 The reroute API 重新路由

ClusterRerouteResponse response = client.admin().cluster()
.prepareReroute()
.setDryRun(true)   //阻止分配命令的运行,仅允许下面两个给定命令(move命令 + cacel命令)的执行
.add(new MoveAllocationCommand(new ShardId("library", 3), "G3czOt4HQbKZT1RhpPCULw","PvHtEMuRSJ6rLJ27AW3U6w"),
     new CancelAllocationCommand(new ShardId("library", 2), "G3czOt4HQbKZT1RhpPCULw",true))
.execute().actionGet();

3.4.1.5 The nodes information API 节点信息

NodesInfoResponse response = client.admin().cluster()
.prepareNodesInfo()
.setHttp(true)  //响应中包括http信息
.setPlugins(true)  //响应中包括插件信息
.execute().actionGet();

3.4.1.6 The node statistics API 节点统计

NodesStatsResponse response = client.admin().cluster()
.prepareNodesStats()
.all()
.execute().actionGet();

3.4.1.7 The nodes hot threads API 节点热点线程

NodesHotThreadsResponse response = client.admin().cluster()
.prepareNodesHotThreads()
.execute().actionGet();

3.4.1.9 The search shards API 查询分片

//输出哪些节点将处理路由值为12的查询
ClusterSearchShardsResponse response = client.admin().cluster()
.prepareSearchShards()
.setIndices("library")
.setRouting("12")
.execute().actionGet();

3.4.2 The Indices administration API

3.4.2.1 The index existence API 索引存在

IndicesExistsResponse response = client.admin().indices()
.prepareExists("books", "library")
.execute().actionGet();

3.4.2.2 The Type existence API 类型存在

TypesExistsResponse response = client.admin().indices()
.prepareTypesExists("library")
.setTypes("book")
.execute().actionGet();

3.4.2.3 The indices stats API 索引统计

IndicesStatsResponse response = client.admin().indices()
.prepareStats("library")
.all()
.execute().actionGet();

3.4.2.4 Index status 索引状态

IndicesStatsResponse response = client.admin().indices()
.prepareStatus("library")
.setRecovery(true)
.execute().actionGet();

3.4.2.5 Segments information API 索引段信息

IndicesSegmentResponse response = client.admin().indices()
.prepareSegments("library")
.execute().actionGet();

3.4.2.6 Creating an index API 创建索引

CreateIndexResponse response = client.admin().indices()
.prepareCreate("news")
.setSettings(Settings.settingsBuilder()
.put("number_of_shards", 1))  //分片数为1
.addMapping("news", XContentFactory.jsonBuilder()
.startObject()
.startObject("news")
.startObject("properties")
.startObject("title")
.field("analyzer", "whitespace")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

3.4.2.7 Deleting an index 删除索引

DeleteIndexResponse response = client.admin().indices()
.prepareDelete("news")
.execute().actionGet();

3.4.2.8 Closing an index 关闭索引

CloseIndexResponse response = client.admin().indices()
.prepareClose("library")
.execute().actionGet();

3.4.2.9 Opening an index 打开索引

OpenIndexResponse response = client.admin().indices()
.prepareOpen("library")
.execute().actionGet();

3.4.2.10 The Refresh API  刷新索引

RefreshResponse response = client.admin().indices()
.prepareRefresh("library")
.execute().actionGet();

3.4.2.11 The Flush API 清空缓冲区

FlushResponse response = client.admin().indices()
.prepareFlush("library")
.setFource(false)
.execute().actionGet();

3.4.2.12 The Optimize API 索引优化

OptimizeResponse response = client.admin().indices()
.prepareOptimize("library")
.setMaxNumSegments(2)  //最大索引段为2
.setFlush(true)
.setOnlyExpungeDeletes(false)
.execute().actionGet();

3.4.2.13 The put mapping API 设置映射

PutMappingResponse response = client.admin().indices()
.preparePutMapping("news")
.setType("news")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.startObject("news")
.startObject("properties")
.startObject("title")
.field("analyzer", "whitespace")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

3.4.2.16 The aliases API 别名

IndicesAliasesResponse response = client.admin().indices()
.prepareAliases()
.addAlias("news", "n")
.addAlias("library", "elastic_books",
QueryBuilders.termQuery("title", "elasticsearch"))
.removeAlias("news", "current_news") //移除news索引的别名current_news
.execute().actionGet();

3.4.2.17 The get aliases API 获取别名

GetAliasesResponse response = client.admin().indices()
.prepareGetAliases("elastic_books", "n")
.execute().actionGet();

3.4.2.18 The aliases exists API 别名存在

AliasesExistResponse response = client.admin().indices()
.prepareAliasesExist("elastic*", "unknown")  //以elastic开头 || 为unknown 的别名
.execute().actionGet();

3.4.2.19 The clear cache API 清空缓存

ClearIndicesCacheResponse response = client.admin().indices()
.prepareClearCache("library")
.setFieldDataCache(true)
.setFields("title")   //清空标题缓存
.setQueryCache(true)  //清空过滤器缓存
.execute().actionGet();

3.4.2.20 The update settings API  更新设置

UpdateSettingsResponse response = client.admin().indices()
.prepareUpdateSettings("library")
.setSettings(Settings.builder()
.put("index.number_of_replicas", 2))  //将副本数更新为2
.execute().actionGet();

3.4.2.21 The analyze API 分析API

//主要是用来检查在library索引中使用whitespace分词器 + nGram过滤器处理“ElasticSerch Servers”短语的分析处理
AnalyzeResponse response = client.admin().indices()
.prepareAnalyze("library", "ElasticSearch Servers")
.setTokenizer("whitespace")
.setTokenFilters("nGram")
.execute().actionGet();

3.4.2.22 The put template API 设置模板

PutIndexTemplateResponse response = client.admin().indices()
.preparePutTemplate("my_template")  //my_template模板名称
.setTemplate("product*")   //可以被任何以product开头的索引使用
.setSettings(Settings.builder()
.put("index.number_of_replicas", 2)  //2个副本
.put("index.number_of_shards", 1))   //一个分片
.addMapping("item", XContentFactory.jsonBuilder()
.startObject()
.startObject("item")
.startObject("properties")
.startObject("title")
.field("type", "string")
.endObject()
.endObject()
.endObject()
.endObject())
.execute().actionGet();

3.4.2.23 The delete template API 删除模板

DeleteIndexTemplateResponse response = client.admin().indices()
.prepareDeleteTemplate("my_*") //删除以my_开头的模板
.execute().actionGet();
转自: http://study121007.iteye.com/blog/2296556
时间: 2024-08-29 21:39:52

elasticsearch常用操作的相关文章

Elasticsearch常用操作:映射篇

[TOC] 其实就是es的字段类型是由es来做自动检测还是由我们自己来指定,因此会分为动态映射和静态映射. 1 动态映射 1.1 映射规则 JSON格式的数据 自动推测的字段类型 null 没有字段被添加 true or false boolean类型 浮点类型数字 float类型 数字 long类型 JSON对象 object类型 数组 由数组中第一个非空值决定 string 有可能是date类型(开启日期检测).double或long类型.text类型.keyword类型 1.2 日期检测

ElasticSearch常用操作:查询与聚合篇

[TOC] 0 说明 基于es 5.4和es 5.6,列举的是个人工作中经常用到的查询(只是工作中使用的是Java API),如果需要看完整的,可以参考官方相关文档https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search.html. 1 查询 先使用一个快速入门来引入,然后后面列出的各种查询都是用得比较多的(在我的工作环境是这样),其它没怎么用的这里就不列出了. 1.1 快速入门 1.1.1 查询全部 GET index

ElasticSearch常用操作:文档篇

[TOC] 1 新建文档 1.1 指定id PUT my_blog/article/1 { "id":1, "title":"elasticsearch", "posttime":"2017-05-01", "content":"elasticsearch is helpfull!" } 返回: { "_index": "my_blog&

Elasticsearch常用操作API

1. 查询所有的索引 [[email protected] cx]# curl '10.0.0.5:9200/_cat/indices?v' health status index    pri rep docs.count docs.deleted store.size pri.store.size  yellow open   customer   5   1          2            0      6.6kb          6.6kb  yellow open   b

Python 字典的特点和常用操作

一.字典帮助文档 >>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt

postgresql的ALTER常用操作

postgresql版本:psql (9.3.4) 1.增加一列ALTER TABLE table_name ADD column_name datatype; 2.删除一列 ALTER TABLE table_name DROP column_name; 3.更改列的数据类型 ALTER TABLE table_name ALTER column_name TYPE datatype; 4.表的重命名 ALTER TABLE table_name RENAME TO new_name; 5.更

Mysql数据库常用操作

1.备份数据库 [[email protected] ~]# mysqldump -h 192.168.0.8 -uroot  -p'123456'  user >user.sql 2.查看mysql数据库字符集设置 mysql> show variables like 'character_set_%';+--------------------------+----------------------------+| Variable_name            | Value    

多路径软件常用操作(MPIO)

一:查看存储盘的路径 1. 查看MPIO的存储盘的路径 # lspath (适用于所有存储的MPIO路径查询) # mpio_get_config -Av (适用于DS3K/DS4K的MPIO路径查询) 2. 查看RDAC存储盘的路径 # fget_config -Av (适用于DS3K/DS4K的RDAC路径查询) 3.查看SDDPCM存储盘的路径 # pcmpath query device (适用于DS6K/DS8K和v7000的SDDPCM路径查询) 4. 查看当前操作系统自带的支持IB

Python学习笔记五:字符串常用操作,字典,三级菜单实例

字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count("x") 输出字符,不够的使用指定的字符补上,字符居中:a_str.center(50,"-") 判断字符串以什么结尾:a_str.endwith("xx") 将字符串中的tab转换为指定数目的空格:a_str.expandtabs(tabsize=30) 查找指定字符