Java 操作 Elasticsearch(高级查询)

在点击测评前,请在右侧命令行手动启动 Elasticsearch。

su es
/opt/install/elasticsearch-6.5.4/bin/elasticsearch`

第1关:常见基本查询

package com.test;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ElasticsearchBasicQuery {
    public final static String HOST = "127.0.0.1";

    public final static int PORT = 9300;

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));

        /********** Begin **********/

            // 构建查询条件
            QueryBuilder builder =QueryBuilders.queryStringQuery("+淘宝 +i7 +联想");

            // 执行查询
            SearchResponse sResponse = client.prepareSearch("blog3")  // 替换为你的索引名称
                    .setQuery(builder)
                    .get();

            // 获取查询结果
            SearchHits hits = sResponse.getHits();

            // 输出符合条件的文档数据
            for (SearchHit hit : hits) {
                // 打印内容
                System.out.println(hit.getSourceAsString());
            }
  
        /********** End **********/
        client.close();
    }
}

第2关:组合查询(复合查询)

package com.test;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ElasticsearchSeniorQuery {
    public final static String HOST = "127.0.0.1";

    public final static int PORT = 9300;

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
        /********** Begin **********/
        QueryBuilder builder = QueryBuilders.boolQuery()
                .must(QueryBuilders
                .rangeQuery("book_price")
                .gte(30))
                .mustNot(QueryBuilders
                .matchQuery("book_genre", "长"))
                .filter(QueryBuilders
                .rangeQuery("release_date")
                .gte("1900-01-01")
                .format("yyyy-MM-dd"));
        SearchResponse sResponse = client.prepareSearch("book")
        .setQuery(builder)
        .get();
        SearchHits hits = sResponse.getHits();
        for(SearchHit hit:hits) {
            System.out.println(hit.getSourceAsString());
        }
        
        /********** End **********/
        client.close();
    }
}

第3关:聚合查询

package com.test;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ElasticsearchAggQuery {
    public final static String HOST = "127.0.0.1";

    public final static int PORT = 9300;

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new TransportAddress(InetAddress.getByName(HOST), PORT));
        /********** Begin **********/
QueryBuilder builder = QueryBuilders.boolQuery()
                .must(QueryBuilders
                .rangeQuery("book_price")
                .gte(20))
                .mustNot(QueryBuilders
                .matchQuery("book_genre", "长"));
        AggregationBuilder agg= AggregationBuilders.extendedStats("book_stats").field("book_price");
        SearchResponse sResponse = client.prepareSearch("book")
        .setQuery(builder)
        .addAggregation(agg)
        .get();
        ExtendedStats book_stats= sResponse.getAggregations().get("book_stats");
        double min = book_stats.getMin();
        double max = book_stats.getMax();
        double avg = book_stats.getAvg();
        double sum = book_stats.getSum();
        long count = book_stats.getCount();
        
        System.out.println("最小值:"+min);
        System.out.println("最大值:"+max);
        System.out.println("平均值:"+avg);
        System.out.println("总和:"+sum);
        System.out.println("数量为:"+count);
        /********** End **********/
        client.close();
    }
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值