ES 查询语法
程序员文章站
2022-07-06 15:47:35
...
文章目录
数据类型说明
text keyword text存储的时候会分词,keyword不会
样例对象
@Document(indexName = "bu")
public class Person {
private @Id
String id;
@Field(type = FieldType.Keyword)
private String name;
private int age;
@Field(type = FieldType.Text)
private String country;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
1.精确查询
{
"from": 0,
"size": 10,
"query": {
"term": {
"name": {
"value": "shanghai"
}
}
}
}
java
// An highlighted block
QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "shanghai");
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder);
List<Person> people = elasticsearchTemplate.queryForList(nativeSearchQuery, Person.class);
2.模糊查找
{
"query": {
"match": {
"country": {
"query": "china"
}
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.matchQuery("country", "china");
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder);
3.多词匹配
会匹配多个词语,
操作符 | 含义 |
---|---|
or | 只要有一个词匹配就返回 |
and | 两个词都有匹配 |
{
"query": {
"match": {
"country": {
"query": "china,japan",
"operator": "and"
}
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.matchQuery("country", "china japan").operator(Operator.AND);
4.多列匹配相同的值
{
"from": 0,
"size": 10,
"query": {
"multi_match": {
"fields": [
"country",
"name"
],
"query": "shanghai"
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("china","country","name");
5.must should用法
must 条件必须满足
should 条件可以满足,也可以不满足,会根据shoud里面的匹配情况打分
“minimum_should_match”: 1 就是必须要满足里面的一个值
表达式 country里必须有japan&&id=10||country里必须有japan&&id=11
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match": {
"country": {
"query": "japan"
}
}
}
],
"minimum_should_match": 1,
"should": [
{
"terms": {
"id": [
10,
11
]
}
}
]
}
}
}
java
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("country", "japan")).should(QueryBuilders.termsQuery("id", Arrays.asList(10, 11)));
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(boolQueryBuilder);
nativeSearchQuery.setMinScore(1);
6.must_not用法
must_not不满足
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match": {
"country": {
"query": "japan"
}
}
}
],
"must_not": [
{
"ids": {
"values": [10]
}
}
]
}
}
}
java
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("country", "japan"))
.mustNot(QueryBuilders.termQuery("id", 10));
7.范围查询排序
{
"from": 0,
"size": 10,
"query": {
"range": {
"id": {
"gte": 10,
"lt": 16
}
}
},
"sort": [
{
"id.keyword": {
"order": "asc"
}
}
]
}
java
RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gte(10).lt(12);
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(id);
nativeSearchQuery.addSort(new Sort(Sort.Direction.ASC,"id.keyword"));
8.去重
{
"_source": "name",
"collapse": {
"field": "name.keyword"
}
}
9.group by count
{
"size": 0,
"aggs": {
"count_name": {
"terms": {
"field": "name.keyword"
}
}
}
}
10.group by sum
上一篇: activiti获取下一个节点信息
下一篇: ES查询-基本查询