es提供了DSL(domain specific Language 领域专定语言),其实就是一个json体,用http请求的方式向es中获取数据。
DSL分为2类
- leaf query clause:可以理解为简单的查询语句,这种类型的语句比较多,像match,term,range等,表示一些特定功能的查询语句
- compound query clause:组合查询语句,这类由多个leaf query clause 或者 compound query clause 语句组成。例如bool,dis_max等等。
es本身提供很多搜索特性如,过滤,排序,分页,折叠(collapse),滚动查询(scroll query)等等。
1.boolean query
boolean查询,可以由must,should,filter,must_not等字句组成,它们都可以定义多个子句。
must和should中的定义的语句,参与相关性的计算(_score分数),filter和must_not子句在filter阶段,不参与相关性的计算。
must:表示must定义的字句中的条件必须匹配,
should:期望匹配should字句中定义的条件,可以设置should中最少需要匹配的子句minimum_should_match ,在must语句不存在的情况下,如果定义了should语句同时must和filter都不存在的,那么默认至少要匹配一个should子句
must_not:返回的结果不能匹配该语句,和filter语句一样,不会参与相关性的计算。
我们以官方提供的一个索引数据文件举例:
有一个银行员工的信息的索引结构,定义如下:
{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"user_id" : {
"type" : "alias",
"path" : "account_number"
}
}