http://192.168.15.38:9200/film/_mapping/dongzuosql
请求方式:postapp
{"properties":{"title":{"type":"keyword"},"publishDate":{"type":"date"},"director":{"type":"keyword"},"price":{"type":"float"},"desc":{"type":"text"}}}
1.查询指定索引指定类型下全部数据post
http://192.168.15.38:9200/[索引名]/[类型]/_search/
例如:code
http://192.168.15.38:9200/film/dongzuo/_search/ 查询film索引下类型为dongzuo的全部数据
2.分页查询排序
http://192.168.15.38:9200/[索引名]/[类型]/_search/
请求方式:POST索引
参数:get
{ "from": [起始位置], "size": [每页数量] }
例如:it
查询前两条数据 http://192.168.15.38:9200/film/dongzuo/_search/ { "from": 0, "size": 2 }
3.排序查询配置
http://192.168.15.38:9200/[索引名]/[类型]/_search/
请求方式:POSTdate
参数:
{ "sort": [ { "[排序字段]": { "order": "[排序类型]" //desc降序 asc升序 } } ] }
例如:
按照发布日期降序 http://192.168.15.38:9200/film/dongzuo/_search/ { "sort": [ { "publishDate": { "order": "desc" } } ] }
4.数据列的过滤
http://192.168.15.38:9200/[索引名]/[类型]/_search/
请求方式:POST
参数:
{ "_source": { "include": [ [查询包括的列] ] } }
例如:
查询前三行数据,只查询标题、导演和价格 { "from": 0, "size": 3, "_source": { "include": [ "title", "director", "price" ] } }
5.简单条件查询
请求方式:POST
http://192.168.15.38:9200/[索引名]/[类型]/_search/
参数: { "query": { "match": { "[配置的字段]": "[匹配的值]" } } }
例如
匹配地址带有山东的数据
注意问题:假如匹配"山"是匹配不到的,由于默认分词器把“山东”分为一个词,而不是"山"
http://192.168.15.38:9200/film/dongzuo/_search/ { "query": { "match": { "address": "山东" } } }
6.查询结果高亮显示 请求方式:POST
http://192.168.15.38:9200/[索引名]/[类型]/_search/
参数: { "highlight": { "fields": { "[高亮显示的字段]": {} } } }
例如:
标题title高亮显示
注:高亮显示的只能是在查询中的字段
http://192.168.15.38:9200/film/dongzuo/_search/ { "query": { "match": { "title": "战狼2" } }, "highlight": { "fields": { "title": {} } } }
7.多条件组合查询
首先了解bool过滤器: 主要有三部分:
{ "bool" : { "must" : [], "should" : [], "must_not" : [], } }
must:全部语句必须匹配 和and等价
must_not:全部语句必须不能匹配 和not等价
should:至少一个匹配 和or等价
(1)单个条件模糊匹配
{ "query": { "bool": { "must": { "match": { "name": "李四" } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(2)多个条件匹配
{ "query": { "bool": { "must": [ { "match": { "name": "李四" } }, { "match": { "address": "山东" } } ] } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(3)must与must_not组合使用
查询地址含有山东,姓名不含有张三的用户 { "query": { "bool": { "must": [ { "match": { "address": "山东" } } ], "must_not": { "match": { "name": "张三" } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(4)使用should查询
should查询至关于sql里面的or条件,在ES里面还有个功能就是提升_score参数,是的匹配更精确
{ "query": { "bool": { "must": [ { "match": { "address": "山东" } } ], "should": [ { "match": { "name": "张三" } }, { "range": { "age": { "gte": 21 } } } ] } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }
(5)filter过滤 fileter做用是过滤符合条件的查询
例如:过滤掉年龄小于21的用户
gt: 大于 gte:大于等于 lt:小于 lte:小于等于
{ "query": { "bool": { "must": [ { "match": { "address": "山东" } } ], "filter": { "range": { "age": { "gte": 21 } } } } }, "_source": { "include": [ "name", "age", "address" ] }, "highlight": { "fields": { "name": {} } } }