ES 数据库

ES 数据库

  • 通过 API 查询
  • 通过 JSON 查询

熟悉 es 的同学都知道 es 一般有两种查询方式

1,在 java 中构建查询对象,调用 es 提供的 api 做查询
2,使用 json 调用接口做查询

查询语句无非是将足够的信息丢给数据库,但是它却和 SQL 不一样有自己独立的查询方式

通过 API 查询

模糊查询

BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();

//Elasticsearch 中文会把汉字分词,“王大”会匹配到like“王”和like“大”,要在字段后面接keyword
boolBuilder.must(QueryBuilders.wildcardQuery("userName.keyword","*王大*"));

等于、不等于

BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//等于  must
boolBuilder.must(QueryBuilders.termQuery("age","30"));
//不等于  mustNot
boolBuilder.mustNot(QueryBuilders.termQuery("sex","1"));

大于、小于

BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//大于
boolBuilder.must(QueryBuilders.rangeQuery("createTime").gt(1609430400000));
//小于
boolBuilder.must(QueryBuilders.rangeQuery("createTime").lt(1672502400000));

es 也是有层级的,下面演示 and 、or 同时使用

BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
 
boolBuilder.must(QueryBuilders.termQuery("a",1));
 
QueryBuilder queryBuilder1 = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("b", 2))
                    .must(QueryBuilders.termQuery("c", 3))
                    .mustNot(QueryBuilders.termQuery("d", 4));
 
QueryBuilder queryBuilder2 = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("e", 4))
                    .must(QueryBuilders.termQuery("f", 5));
 
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .should(queryBuilder1)
                    .should(queryBuilder2);
 
boolBuilder.must(queryBuilder);

等同与这个 sql

select * from user where a=1 and ((b=2 and c=3 and d !=4) or (e=4 and f=5))

通过 JSON 查询

模糊查询

{
    "query": {
        "bool": {
            "adjust_pure_negative": true,
            "must": [
                {
                    "wildcard": {
                        "name": {
                            "boost": 1.0,
                            "wildcard": "*小李*"
                        }
                    }
                }
            ],
            "boost": 1.0
        }
    }
}

等值查询,查询 name = 小李的数据

{
    "query": {
        "bool": {
            "adjust_pure_negative": true,
            "must": [
                {
                    "term": {
                        "name": {
                            "boost": 1.0,
                            "value": "小李"
                        }
                    }
                }
            ],
            "boost": 1.0
        }
    }
}

范围查询,查询年龄大于等于18,并且小于等于50的数据

{
    "query": {
        "range": {
            "age": {
                "include_lower": true,
                "include_upper": true,
                "from": 18,
                "boost": 1.0,
                "to": 50
            }
        }
    }
}

多条件查询,查询姓名为小李,并且年龄在10-50之间的文档

{
    "query": {
        "bool": {
            "adjust_pure_negative": true,
            "must": [
                {
                    "term": {
                        "name": {
                            "boost": 1.0,
                            "value": "小李"
                        }
                    }
                },
                {
                    "range": {
                        "age": {
                            "include_lower": true,
                            "include_upper": true,
                            "from": 10,
                            "boost": 1.0,
                            "to": 50
                        }
                    }
                }
            ],
            "boost": 1.0
        }
    }
}

你可能感兴趣的:(elasticsearch,数据库,大数据)