查询分为两种:字符串查询和DSL查询
1、字符串查询详细总结,此种方法一般用于简单测试。想要更加灵活,功能更加强大的查询功能还是得用DSL。但是这个字符串查询用起来还是挺方便的。切记不可将这种查询语句给你的客户。除非这个客户是你非常信任的。要不然这种语句会给你的集群带来致命的危险!!!!!
1>GET /index/tweet/_search?q=tweet:elasticsearch #查询index索引 (相当于数据库)、tweet类型(相当于数据库中的表)、tweet字段中包含elasticsearch关键词的文章。
2>GET /index/tweet/_search?q=%2Bname%3Ajohn+%2Btweet%3Amary #+name:john +tweet:mary查询name字段包含john 同时tweet字段包含mary。%2B 是“+”,%3A是“:”
"+"前缀表示语句匹配条件必须被满足。类似的"-"前缀表示条件必须不被满足。所有条件如果没有+或-表示是可选的——匹配越多,相关的文档就越多。
3>更复杂的例子:
name字段包含"mary"或"john"
date晚于2014-09-10
_all字段包含"aggregations"或"geo"
+name:(mary john) +date:>2014-09-10 +(aggregations geo)
编码后的查询字符串变得不太容易阅读:
GET /index/tweet/_search?q=%2Bname%3A(mary+john)+%2Bdate%3A%3E2014-09-10+%2B(aggregations+geo)
4>其他常用参数使用举例
GET /index/tweet/_search?q=%2Bdate%3A%3E2014-09-10+%2Bname1%3A(A+B+C)+%2Dname2%3A(D+E+F)+name3%3A(G+H+I)&from=0&size=3##查询index索引tweet类型中date字段日期在2014-09-10以后,同时name1字段必须含有A或者B或者C中的一个,同时name2字段不能包含有D或者E或者F中的任意一个,
如果name3字段中包含了G或者H或者I那么他就会排在前边,包含的G、H、I、越多排名越靠前。并且只给我返回从0-3的3个结果。
附加:更加详细得字符串查询参数请参看:https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-uri-request.html
“+”号“-”号和其他符号URL对照表参见:http://www.cnblogs.com/kobe8/p/4030396.html
##########################################################
##################################################################
2、DSL查询总结,此处才是主要的。一定要多看几遍并且尽量用项目来练习,不然不能灵活使用。多看查询和过滤的语句,较常用。
英文好的参看官方DSL语句。参看网址(权威):https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-match-query.html
偷懒的看下边:个人的使用经验,和参考别人的。仅供参考
下边内容多摘自网站(下方列出了较常用的查询语句,其他更为专有的查询语句请自行查询。):https://es.xiaoleilu.com/080_Structured_Search/10_compoundfilters.html
最重要的查询过滤语句
term 过滤
term主要用于精确匹配哪些值,比如数字,日期,布尔值或not_analyzed的字符串(未经分析的文本数据类型):
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}例子:注意我用的是ik分词器。你最好先用英文来搞。查一个单词
GET /ik2_index/ik_type6/_search
{
"query": {
"term": {
"content1": "AAA"
}
}
}
terms 过滤
terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配:
+
{
"terms": {
"tag": [ "search", "full_text", "nosql" ]
}
}例子:注意我用的是ik分词器。你最好先用英文来搞。查一个单词
GET /ik2_index/ik_type6/_search
{
"query": {
"terms": {
"content1": [ "AAA", "BBB", "美国" ]
}
}
}
range 过滤
range过滤允许我们按照指定范围查找一批数据:
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}例子:
GET /ik2_index/ik_type6/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
}
围操作符包含:
gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于
bool 过滤
bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。
这些参数可以分别继承一个过滤条件或者一个过滤条件的数组:还可以继续嵌套
{
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": { "term": { "tag": "spam" }},
"should": [
{ "term": { "starred": true }},
{ "term": { "unread": true }}
]
}
}
例子:
GET /ik2_index/ik_type6/_search
{
"query": {
"bool": {
"must": {
"term": {
"folder": "inbox"
}
},
"must_not": {
"term": {
"tag": "spam"
}
},
"should": [
{
"term": {
"starred": true
}
},
{
"term": {
"unread": true
}
}
]
}
}
}
附加:更多过滤参看网址:https://segmentfault.com/a/1190000004429689
match 查询
match查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它。
如果你使用 match 查询一个全文本字段,它会在真正查询之前用分析器先分析match一下查询字符:
{
"match": {
"tweet": "About Search"
}
}
如果用match下指定了一个确切值,在遇到数字,日期,布尔值或者not_analyzed 的字符串时,它将为你搜索你给定的值:
{ "match": { "age": 26 }}
{ "match": { "date": "2014-09-01" }}
{ "match": { "public": true }}
{ "match": { "tag": "full_text" }}
提示: 做精确匹配搜索时,你最好用过滤语句,因为过滤语句可以缓存数据。
不像我们在《简单搜索》中介绍的字符查询,match查询不可以用类似"+usid:2 +tweet:search"这样的语句。 它只能就指定某个确切字段某个确切的值进行搜索,而你要做的就是为它指定正确的字段名以避免语法错误。
例子1:
GET /ik2_index/ik_type6/_search
{
"query": {
"match": {
"age": 26
}