当前位置:网站首页>Es query syntax
Es query syntax
2022-06-29 07:22:00 【wending-Y】
List of articles
Data type description
text keyword text Word segmentation is used when storing ,keyword Can't
Sample object
@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. Precise query
{
"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. Fuzzy search
{
"query": {
"match": {
"country": {
"query": "china"
}
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.matchQuery("country", "china");
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder);
3. Multi word matching
Will match multiple words ,
| The operator | meaning |
|---|---|
| or | As long as there is a word match, it returns |
| and | Both words match |
{
"query": {
"match": {
"country": {
"query": "china,japan",
"operator": "and"
}
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.matchQuery("country", "china japan").operator(Operator.AND);
4. Multiple columns match the same value
{
"from": 0,
"size": 10,
"query": {
"multi_match": {
"fields": [
"country",
"name"
],
"query": "shanghai"
}
}
}
java
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("china","country","name");
5.must should usage
must Conditions must be met
should Conditions can be met , You can't be satisfied , Will be based on shoud Score the matching condition inside
“minimum_should_match”: 1 It is necessary to satisfy one of the values
expression country There must be japan&&id=10||country There must be 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 usage
must_not dissatisfaction
{
"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. Range query sort
{
"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. duplicate removal
{
"_source": "name",
"collapse": {
"field": "name.keyword"
}
}
9.group by count
{
"size": 0,
"aggs": {
"count_name": {
"terms": {
"field": "name.keyword"
}
}
}
}
java
ElasticsearchTemplate elasticsearchTemplate = run.getBean(ElasticsearchTemplate.class);
TermsAggregationBuilder field = AggregationBuilders.terms("ss").field("id.keyword");
NativeSearchQuery build = new NativeSearchQueryBuilder().addAggregation(field).build();
AggregatedPage<Person> objects = elasticsearchTemplate.queryForPage(build, Person.class);
Terms terms = (Terms) objects.getAggregations().asList().get(0);
terms.getBuckets().stream().forEach(x -> {
System.out.println(x.getKeyAsString() + "," + x.getDocCount());
});
10.group by sum
{
"aggs": {
"colors": {
"terms": {
"field": "name.keyword",
"order": {
"sum_value": "asc"
}
},
"aggs": {
"sum_value": {
"sum": {
"field": "age"
}
}
}
}
}
}
java
ElasticsearchTemplate elasticsearchTemplate = run.getBean(ElasticsearchTemplate.class);
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("a").field("name.keyword")
.subAggregation(AggregationBuilders.sum("sumAge").field("age"));
NativeSearchQuery build = new NativeSearchQueryBuilder().addAggregation(termsAggregationBuilder).build();
AggregatedPage<Person> objects = elasticsearchTemplate.queryForPage(build, Person.class);
Terms terms = (Terms) objects.getAggregations().asList().get(0);
terms.getBuckets().stream().forEach(x -> {
Sum sum = (Sum) x.getAggregations().getAsMap().get("sumAge");
System.out.println(x.getKeyAsString() + "," + sum.getValue());
});
边栏推荐
- JDBC connects to the database and socket sends the client.
- YGG pilipinas: typhoon Odette disaster relief work update
- Who is the main body of the waiting insurance record? Record in the local network security, right?
- uva10859
- QT serial port programming
- 测试人员需要了解的工具有哪些
- Qt 串口编程
- 利用IPv6實現公網訪問遠程桌面
- Differences between JSON objects and JSON strings
- 【软件测试】接口——基本测试流程
猜你喜欢

Qt QFrame详解

Redis (4) of NoSQL database: redis new data type

How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli

Domestic code hosting center code cloud

Error: GPG check FAILED Once install MySQL

A hybrid model of machine learning notes time series

项目中 if else 的代替写法

YGG cooperated with Web3 platform leader to empower the creative community with Dao tools and resources

Introduction to Ceres Quartet

详解Autosar Arxml中的CANFD报文及格式
随机推荐
国内代码托管中心- 码云
[answer all questions] CSDN question and answer function evaluation
[translation] [Chapter II ①] mindshare PCI Express technology 3.0
利用IPv6实现公网访问远程桌面
关于数据库,你应该知道的事情
In vscade, how to use eslint to lint and format
关联性——相关性分析
Domestic code hosting center code cloud
When the soft keyboard appears, it makes my EditText field lose focus
NoSQL数据库之Redis(一):安装 & 简介
Markdown 技能树(2):段落及强调
JDBC connects to the database and socket sends the client.
【翻译】e-Cloud。使用KubeEdge的大规模CDN
Relevance - correlation analysis
Using IPv6 to access remote desktop through public network
机器学习笔记 - 时间序列的混合模型
节流的两种写法-最近看到的这种写法
Suggestions on digital transformation of large chemical enterprises
Spark RDD case: Statistics of daily new users
示波器 符号