博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch的JavaAPI
阅读量:6470 次
发布时间:2019-06-23

本文共 8515 字,大约阅读时间需要 28 分钟。

获取客户端对象

public class App {    private TransportClient client;    //获取客户端对象    @Before    public void getClinet() throws UnknownHostException {        Settings settings = Settings.builder().put("cluster.name", "my-application").build();        //获得客户端对象        client = new PreBuiltTransportClient(settings);        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.11"), 9300));        System.out.println(client.toString());    }

 ## 创建索引

//创建索引@Testpublic void createIndex() {    client.admin().indices().prepareCreate("blog1").get();    client.close();}//删除索引@Testpublic void deleteIndex() {    client.admin().indices().prepareDelete("blog").get();    client.close();}

 新建文档

//新建文档@Testpublic void createIndexByJson() {    //创建文档内容    String json = "{" + "\"id\":\"1\"," + "\"title\":\"基于Lucene的搜索服务器\","            + "\"content\":\"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口\"" + "}";    //创建    IndexResponse response = client.prepareIndex("blog", "article", "1").setSource(json).execute().actionGet();    //打印返回值    System.out.println("索引" + response.getIndex());    System.out.println("类型" + response.getType());    System.out.println("id" + response.getId());    System.out.println("结果" + response.getResult());    client.close();}//创建文档以hashmap@Testpublic void createIndexBymap() {    HashMap
json = new HashMap
(); json.put("id", "2"); json.put("title", "hph"); json.put("content", "博客 hph.blog"); IndexResponse response = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet(); //打印返回值 System.out.println("索引" + response.getIndex()); System.out.println("类型" + response.getType()); System.out.println("id" + response.getId()); System.out.println("结果" + response.getResult()); client.close();}//创建文档以hashmap @Test public void createIndexBymap() { HashMap
json = new HashMap
(); json.put("id", "2"); json.put("title", "hph"); json.put("content", "博客 hph.blog"); IndexResponse response = client.prepareIndex("blog", "article", "2").setSource(json).execute().actionGet(); //打印返回值 System.out.println("索引" + response.getIndex()); System.out.println("类型" + response.getType()); System.out.println("id" + response.getId()); System.out.println("结果" + response.getResult()); client.close(); } //创建文档已bulder @Test public void createIndexbyBulider() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("id", "4").field("title", "博客").field("content", "hphblog.cn").endObject(); IndexResponse response = client.prepareIndex("blog", "article", "3").setSource(builder).execute().actionGet(); //打印返回值 System.out.println("索引" + response.getIndex()); System.out.println("类型" + response.getType()); System.out.println("id" + response.getId()); System.out.println("结果" + response.getResult()); client.close(); }

 查询索引

//单个索引查询@Testpublic void queryIndex() {    //查询    GetResponse response = client.prepareGet("blog", "article", "2").get();    //打印    System.out.println(response.getSourceAsString());    //关闭资源    client.close();}//多个索引查询@Testpublic void queryMultiIndex() {    //查询    MultiGetResponse response = client.prepareMultiGet().add("blog", "article", "3")            .add("blog", "article", "1")            .add("blog", "article", "2").get();    for (MultiGetItemResponse multiGetItemResponse : response) {        GetResponse response1 = multiGetItemResponse.getResponse();        //判断是否存在        if (response1.isExists()) {            System.out.println(response1.getSourceAsString());        }    }}//更改数据@Testpublic void update() throws ExecutionException, InterruptedException, IOException {    UpdateRequest updateRequest = new UpdateRequest("blog", "article", "2");    updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("id", "2")            .field("title", "hphblog").field("content", "大数据博客学习技术分享").endObject()    );    UpdateResponse response = client.update(updateRequest).get();    //打印返回值    System.out.println("索引" + response.getIndex());    System.out.println("类型" + response.getType());    System.out.println("id" + response.getId());    System.out.println("结果" + response.getResult());    client.close();}

 更新文档

//更新文档updaset@Testpublic void upsert() throws IOException, ExecutionException, InterruptedException {    //没有就创建    IndexRequest indexRequest = new IndexRequest("blog", "article", "5");    indexRequest.source(XContentFactory.jsonBuilder().startObject().field("id", "5")            .field("title", "大数据技术分享的学习").field("content", "大数据技术的分享与学习希望和大家多多交流").endObject                    ());    //有文档内容就更新    UpdateRequest updateRequest = new UpdateRequest("blog", "article", "5");    updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("id", "5")            .field("title", "hphblog").field("content", "技术分享、技术交流、技术学习").endObject()    );    updateRequest.upsert(indexRequest);    UpdateResponse response = client.update(updateRequest).get();    //打印返回值    System.out.println("索引" + response.getIndex());    System.out.println("类型" + response.getType());    System.out.println("id" + response.getId());    System.out.println("结果" + response.getResult());    client.close();}

 删除文档

//删除文档@Testpublic void delete() {    client.prepareDelete("blog", "article", "5").get();    client.close();}

 查询所有

//查询所有@Testpublic void matchAllquery() {    //1.执行查询    SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.matchAllQuery()).get();    SearchHits hits = response.getHits();    System.out.println("查询的结果为" + hits.getTotalHits());    Iterator
iterator = hits.iterator(); while (iterator.hasNext()) { SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } client.close();}//分词查询@Testpublic void query() { //1.执行查询 SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.queryStringQuery("学习全文")).get(); SearchHits hits = response.getHits(); System.out.println("查询的结果为" + hits.getTotalHits()); Iterator
iterator = hits.iterator(); while (iterator.hasNext()) { SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } client.close();}

 通配符查询

//通配符查询@Testpublic void wildcardQuery() {    //1.执行查询    SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.wildcardQuery("content", "分")).get();    SearchHits hits = response.getHits();    System.out.println("查询的结果为" + hits.getTotalHits());    Iterator
iterator = hits.iterator(); while (iterator.hasNext()) { SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } client.close();}

 模糊查询

//模糊查询@Testpublic void fuzzyQuery() {    //1.执行查询    SearchResponse response = client.prepareSearch("blog").setTypes("article").setQuery(QueryBuilders.fuzzyQuery("title", "LuceNe")).get();    SearchHits hits = response.getHits();    System.out.println("查询的结果为" + hits.getTotalHits());    Iterator
iterator = hits.iterator(); while (iterator.hasNext()) { SearchHit next = iterator.next(); System.out.println(next.getSourceAsString()); } client.close();}//映射相关的操作@Testpublic void createMapping() throws Exception { // 1设置mapping XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startObject("article") .startObject("properties") .startObject("id1") .field("type", "string") .field("store", "yes") .endObject() .startObject("title2") .field("type", "string") .field("store", "no") .endObject() .startObject("content") .field("type", "string") .field("store", "yes") .endObject() .endObject() .endObject() .endObject();

 添加mapping

// 2 添加mapping    PutMappingRequest mapping = Requests.putMappingRequest("blog1").type("article").source(builder);    client.admin().indices().putMapping(mapping).get();    // 3 关闭资源    client.close();}

 原文:http://hphblog.cn/2018/12/16/Elasticsearch%E7%9A%84JavaAPI/

 

转载于:https://www.cnblogs.com/fmgao-technology/p/10410298.html

你可能感兴趣的文章
利用canvas合并图片
查看>>
python接口测试(二)
查看>>
我的友情链接
查看>>
C++浓缩(五)
查看>>
C++浓缩(二)
查看>>
C#网络示例
查看>>
nginx相关
查看>>
jpetstore 分页功能
查看>>
微信基础服务肯定不收费
查看>>
Osgi, 语义,需求与设计
查看>>
oracle 11g expdp 备份解决空表无法备份
查看>>
Windows 7的相关小技巧
查看>>
【简明 Python 教程】学习笔记【Python简介及基本概念】
查看>>
利用ipmi更改本机自带的idrac
查看>>
Appium问题集合
查看>>
error libGL.so: cannot open shared object file: No
查看>>
继续折腾树莓派
查看>>
正确使用 Volatile
查看>>
Java中使用Map统计对象数目
查看>>
什么是机器学习
查看>>