美文网首页
3-Elasticsearch查询API

3-Elasticsearch查询API

作者: 唐影若凡 | 来源:发表于2017-03-23 11:39 被阅读0次

    声明:原创文章,转载请注明出处。http://www.jianshu.com/u/e02df63eaa87

    在上一节2-Elasticsearch集群数据批量导入中,我们向ES集群中批量导入了1000条数据。这一节通过Java API查询文档。

    1、全文查询

    查询文档中包含Beijing关键字的doc,不管该关键字出现在哪个字段中,只要包含都会返回。

    public class QueryMain {
        private static final Logger logger = LogManager.getLogger(QueryMain.class);
    
        public static QueryBuilder createQuery() {
            return QueryBuilders.queryStringQuery("Beijing");
        }
    
        public static void main(String[] args) {
            try {
                ESConfig esConfig = new ESConfig()
                        .setEsClusterName("elasticsearch")
                        .setEsClusterAddress("127.0.0.1:9300")
                        .setEsIndex("person")
                        .setEsType("infos");
    
                // 创建客户端
                Client client = new ESClient().getElasticsearchClient(esConfig);
                // 查询
                SearchResponse response = client.prepareSearch().setQuery(createQuery()).execute().actionGet();
                // 返回结果
                SearchHits hits = response.getHits();
                for (SearchHit hit : hits) {
                    System.out.println(hit.getSourceAsString());
                }
            } catch (Exception e) {
                logger.error("exception:", e);
            }
        }
    }
    

    返回结果:

    {"pid":93,"age":28,"sex":true,"name":"Lily","addr":"Beijing"}
    {"pid":168,"age":16,"sex":false,"name":"Tom","addr":"Beijing"}
    {"pid":214,"age":35,"sex":false,"name":"Tom","addr":"Beijing"}
    {"pid":226,"age":77,"sex":true,"name":"Jenny","addr":"Beijing"}
    {"pid":276,"age":19,"sex":false,"name":"Bill","addr":"Beijing"}
    {"pid":384,"age":62,"sex":false,"name":"Brown","addr":"Beijing"}
    {"pid":459,"age":69,"sex":true,"name":"Jenny","addr":"Beijing"}
    {"pid":531,"age":38,"sex":false,"name":"Brown","addr":"Beijing"}
    {"pid":536,"age":77,"sex":true,"name":"Lily","addr":"Beijing"}
    {"pid":574,"age":59,"sex":true,"name":"Lily","addr":"Beijing"}
    

    2、按字段条件查询

    如果需要查询(addr = Beijing) && (sex = false) && (10 < age < 20)的doc:

        public static QueryBuilder createQuery() {
            BoolQueryBuilder query = QueryBuilders.boolQuery();
            // addr = Beijing
            query.must(new QueryStringQueryBuilder("Beijing").field("addr"));
            // sex = falese
            query.must(new QueryStringQueryBuilder("false").field("sex"));
            // age ∈ (10, 20)
            query.must(new RangeQueryBuilder("age").gt(10).lt(20));
            return query;
        }
    

    返回结果:

    {"pid":168,"age":16,"sex":false,"name":"Tom","addr":"Beijing"}
    {"pid":276,"age":19,"sex":false,"name":"Bill","addr":"Beijing"}
    {"pid":565,"age":16,"sex":false,"name":"Brown","addr":"Beijing"}
    {"pid":73,"age":13,"sex":false,"name":"David","addr":"Beijing"}
    

    3、查询API

    3.1 基本查询
    QueryBuilder query = QueryBuilders.matchPhraseQuery("addr", "Beijing");
    

    只要doc字段中含有Beijing的短语都会返回。

    3.2 多词条查询
    QueryBuilder query = QueryBuilders.termsQuery("addr", "Beijing", "Shanghai");
    

    Beijing、Shanghai在addr字段中进行查询,只要满足一个就会返回。但是,并不会返回二者的并集。

    3.3 matchAll查询
    QueryBuilder query = QueryBuilders.matchAllQuery();
    
    3.4 常用词查询
    QueryBuilder query = QueryBuilders.commonTermsQuery("addr", "Beijing");
    
    3.5 multiMatch查询
    QueryBuilder query = QueryBuilders.multiMatchQuery("Beijing", "addr", "name");
    

    在addr、name字段中查找关键字为Beijing的doc。

    3.6 queryString查询
    QueryBuilder query = QueryBuilders.queryStringQuery("Beijing");
    
    3.7 simpleQueryString查询
    QueryBuilder query = QueryBuilders.simpleQueryStringQuery("Beijing");
    
    3.8 前缀查询
    QueryBuilder query = QueryBuilders.prefixQuery("addr", "Bei");
    
    3.9 模糊查询
    QueryBuilder query = QueryBuilders.fuzzyQuery("addr", "BeiJing");
    
    3.10 通配符查询
    QueryBuilder query = QueryBuilders.wildcardQuery("addr", "Bei*");
    
    3.11 Range查询
    // 闭区间
    QueryBuilder query = QueryBuilders.rangeQuery("age").from(10).to(20);
    // 开区间
    QueryBuilder query = QueryBuilders.rangeQuery("age").gt(10).lt(20);
    

    相关文章

      网友评论

          本文标题:3-Elasticsearch查询API

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