1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #bool查询 #老版本的filtered已经被bool替换 #用bool must should must_not来完成,格式如下: #bool: { #"filter":[], # "must":[], # "should":[], # "must_not":[], #}
#建立测试数据 POST qa/test/_bulk {"index":{"_id":1}} {"salary":10,"title":"Python"} {"index":{"_id":2}} {"salary":20,"title":"Scrapy"} {"index":{"_id":3}} {"salary":30,"title":"Django"} {"index":{"_id":4}} {"salary":30,"title":"Elasticsearch"}
DELETE qa/test #简单过滤查询
#最简单的filter查询 #select * from test where salary = 20 #filtered 薪资为20k的工作 GET qa/test/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "term": { "salary": 20 } } } } }
#指定多个值 GET qa/test/_search { "query": { "bool": { "must":{ "match_all":{} }, "filter": { "terms": { "salary": [ 10, 20 ] } } } } }
#select * from test where title="Python" GET qa/test/_search { "query": { "bool": { "must":{ "match_all":{} }, "filter": { "term": { "title": "python" } } } } }
#查看分析器解析结果 GET _analyze { "analyzer": "ik_max_word", "text": "Python网络" }
#bool过滤查询,可以做组合过滤查询 #select * from test where(salary = 20 OR title = Pyhon) AND (salary != 30) #查询薪资等于20k或者工作为python的工作,排除价格为30K的 GET qa/test/_search { "query": { "bool": { "should": [ {"term": {"salary":20}}, {"term":{"title":"Python"}} ], "must_not": { "term":{"salary":30} } } } }
#嵌套查询 #select * from test where title="Python" or (title="elasticsearch" AND salary=30)
GET qa/test/_search { "query": { "bool": { "should": [ {"term":{"title":"Python"}}, {"bool": { "must": [ {"term":{"title":"elasticsearch"}}, {"term":{"salary":30}} ]} } ] } } }
#过滤空和非空 #建立测试数据 POST qa/test/_bulk {"index":{"_id":"1"}} {"tags":["search"]} {"index":{"_id":"2"}} {"tags":["search","python"]} {"index":{"_id":"3"}} {"tags":["some data"]} {"index":{"_id":"4"}} {"tags":null} {"index":{"_id":"5"}} {"tags":["search",null]}
#处理null空值的方法 #selact tags from test where tags is not NULL GET qa/test/_search { "query": { "bool": { "filter": { "exists": { "field": "tags" } } } } }
|