美文网首页
五、JavaAPI

五、JavaAPI

作者: 茶铺里的水 | 来源:发表于2017-11-20 20:33 被阅读15次

1,新建Client

    @Before
    public void init() throws UnknownHostException {
        client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        start = System.currentTimeMillis();
    }

    @After
    public void end(){
        System.out.println("cost time:"+(System.currentTimeMillis() - start)+"ms");
        client.close();
    }

2,创建索引

    @Test
    public void testCreate(){
        User user = mockUserData(0);
        IndexResponse response = client.prepareIndex(index,type,user.getId())
                .setSource(JSONObject.toJSONString(user))
                .get();
        System.out.println("data is created:"+response.isCreated());
        System.out.println(JSONObject.toJSONString(response));
    }

3,更新索引

    @Test
    public void testUpdate() throws ExecutionException, InterruptedException {
        User user = new User();
        user.setId("1");
        user.setEmail("540*****5@qq.com");
        UpdateResponse response = client.prepareUpdate(index,type,user.getId()).setDoc(JSONObject.toJSONString(user)).get();
        System.out.println(JSONObject.toJSONString(response));
    }

4,删除索引

    @Test
    public void testDelete(){
        DeleteResponse response = client.prepareDelete(index,type,"1").get();
        System.out.println(JSONObject.toJSONString(response));
    }

5,查询

    @Test
    public void testQueryById(){
        QueryBuilder idsQuery = QueryBuilders.idsQuery().addIds("14","19");
        SearchResponse response = client.prepareSearch(index)
                .setTypes(type)
                .setQuery(idsQuery)
                .setExplain(true)
                .execute()
                .actionGet();
        System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
    }

    @Test
    public void testTermQuery(){
        QueryBuilder termQuery = QueryBuilders.termQuery("age","44");
        SearchResponse response = client.prepareSearch(index)
                .setTypes(type)
                .setQuery(termQuery)
                .setFrom(0)
                .setSize(50)
                .setExplain(true)
                .execute()
                .actionGet();
        System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
    }

    @Test
    public void testRangeQuery(){
        QueryBuilder rangeQuery = QueryBuilders.rangeQuery("age").from(10).to(50);
        SearchResponse response = client.prepareSearch(index)
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setTypes(type)
                .setQuery(rangeQuery)
                .setFrom(0)
                .setSize(50)
                .execute()
                .actionGet();
        System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
    }

    // 在某几个列中搜索关键字
    @Test
    public void testMultiMatchQuery(){
        QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("故事","articleTitle","articleContent");
        SearchResponse response = client.prepareSearch("test2")
                .setTypes("article")
                .setHighlighterQuery(queryBuilder)
                .setHighlighterRequireFieldMatch(true)
                .addHighlightedField("articleContent")
                .addHighlightedField("articleTitle")
                .setHighlighterPreTags("<span>")
                .setHighlighterPostTags("</span>")
                .setFrom(0)
                .setSize(50)
                .execute()
                .actionGet();
        SearchHits hits = response.getHits();
        System.out.println("总量"+hits.getTotalHits());
        SearchHit[] hitArray = hits.hits();
        for (SearchHit hit : hitArray) {
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            //打印高亮显示内容
            for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            }
        }
        System.out.println("count is "+response.getHits().getTotalHits() + ", response data is " + JSONObject.toJSONString(response.getHits()));
    }

参考:elasticsearch官网Java API

相关文章

  • 五、JavaAPI

    1,新建Client 2,创建索引 3,更新索引 4,删除索引 5,查询 参考:elasticsearch官网Ja...

  • javaAPI操作HDFS文件

    javaAPI写入HDFS文件。 javaAPI读取HDFS javaAPI删除HDFS

  • JavaAPI

    什么是JDKAPI 字符串的基本操作 StringBuilder及其API 包装类image.png Object...

  • ES奇技淫巧

    实现in操作 脚本查询: javaAPI

  • CCF节日(Java)

    方法一:硬算 方法二:JavaAPI

  • javaAPI(二)

    XML文件 读取XML常用API汇总(通过SAXReader类读取) 从java中写入XML常用API File类...

  • javaAPI三

    字符转换流

  • 生成javaAPI

    命令: javadoc -d api * -encoding UTF-8 -charset UTF-8 1....

  • HBase JavaAPI

    添加依赖 判断表是否存在 旧API 新API 创建表 删除表 新增和修改 查询数据 删除数据

  • Zookeeper JavaAPI

    依赖 连接Zookeeper 连接 重连 原子操作 创建节点 同步 异步 监听节点 判断节点是否存在 查看ACL ...

网友评论

      本文标题:五、JavaAPI

      本文链接:https://www.haomeiwen.com/subject/qcjbvxtx.html