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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
| PUT /scorpio/user/1 { "name": "王二小", "age": 3, "desc": "一个小孩子", "tags": ["篮球","排球","足球"] }
PUT /scorpio/user/2 { "name": "曹操", "age": 33, "desc": "一个大人", "tags": ["篮球","乒乓球"] }
PUT /scorpio/user/3 { "name": "小乔", "age": 23, "desc": "一个女孩子", "tags": ["跳舞"] }
PUT /scorpio/user/4 { "name": "大乔", "age": 24, "desc": "一个女孩子", "tags": ["跳舞", "唱歌"] }
PUT /scorpio/user/5 { "name": "江大乔", "age": 21, "desc": "一个女孩子", "tags": ["跳舞", "唱歌"] }
# 查询 GET /scorpio/user/1 # 根据默认的规则基础查询 GET /scorpio/user/_search?q=name:王二小 # (排序、分页、高亮、模糊、) # 多条数据_score是分值,匹配度
GET /scorpio/user/_search { "query": { "match": { "name": "大乔" } } }
# 只查出具体字段 sql 指定字段 GET /scorpio/user/_search { "query": { "match": { "name": "大乔" } }, "_source": ["name","age"] }
# 排序 GET /scorpio/user/_search { "query": { "match": { "name": "大乔" } }, "sort":{ "age":{ "order": "desc" } } }
# 分页 from:从第几条数据 size:分页大小 GET /scorpio/user/_search { "query": { "match": { "name": "大乔" } }, "sort":{ "age":{ "order": "desc" } }, "from": 0, "size": 2 }
# must(and) 所有的条件都要符合 GET /scorpio/user/_search { "query": { "bool": { "must": [ { "match": { "name": "大乔" } }, { "match": { "age": "24" } } ] } } }
# should(or) 条件满足其一 GET /scorpio/user/_search { "query": { "bool": { "should": [ { "match": { "name": "大乔" } }, { "match": { "age": 24 } } ] } } }
# must_not(not 不等于) 条件满足其一 GET /scorpio/user/_search { "query": { "bool": { "must_not": [ { "match": { "age": 24 } } ] } } }
# filter 进行范围数据过滤 gte >= 、lte <= GET /scorpio/user/_search { "query": { "bool": { "must": [ { "match": { "name": "大乔" } } ], "filter": [ { "range": { "age": { "gte": 10, "lte": 23 } } } ] } } }
# 匹配多个条件 多个条件空格隔开,只要满足其一就可以查出
GET /scorpio/user/_search { "query": { "match": { "tags": "唱歌 跳舞" } } } # 精确查询 # term查询是通过倒排索引指定的词条进行精确查找的
# 关于分词: # term 直接查询精确的 # match 会使用分词器解析(需要先分析文档) # 两个类型 text keyword # PUT testdb { "mappings": { "properties": { "name": { "type": "text" }, "desc": { "type": "keyword" } } } }
PUT testdb/_doc/1 { "name": "幸福了吗", "desc": "也还行888" }
PUT testdb/_doc/2 { "name": "幸福了吗", "desc": "也还行999" }
PUT testdb/_doc/3 { "name": "幸福了吗", "desc": "也还行99977" }
GET testdb/_search { "query": { "term": { "name": "幸" } } } GET testdb/_search { "query": { "term": { "desc": "也还行999" } } } # 结论:keyword类型的字段不会被分词器解析 # text 类型才会被分词器解析
GET _analyze { "analyzer": "keyword", "text": "幸福了吗" }
GET _analyze { "analyzer": "standard", "text": "幸福了吗" }
PUT testdb/_doc/5 { "t1": "22", "t2": "2020-09-15" }
PUT testdb/_doc/6 { "t1": "23", "t2": "2020-09-15" }
GET testdb/_doc/5
# 多个值匹配精确查询 GET testdb/_search { "query": { "bool": { "should": [ { "term": { "t1": "22" } }, { "term": { "t1": "23" } } ] } } }
# 高亮查询,搜索的结果会增加em标签 GET scorpio/user/_search { "query":{ "match": { "name": "大乔" } }, "highlight":{ "fields": { "name": {} } } }
# 高亮查询,自定义高亮标签 GET scorpio/user/_search { "query":{ "match": { "name": "大乔" } }, "highlight":{ "pre_tags": "<p class='red'>", "post_tags": "</p>", "fields": { "name": {} } } }
|