ElasticSearch(4)------查询文档
前言
了解了ElasticSearch的基本常识以后,我们来看一下怎么查询我们保存好的文档数据.
正文
1. 基本查询
格式:GET /index/type/_search?q=属性名:值
如:GET /index/type/_search?q=name:wangwu
表示查询文档中name属性为wangwu的文档.
也可以在查询结果中进行排序
格式:GET /index/type/_search?q=属性名:值&sort=属性名:desc
如:GET /index/type/_search?q=name:lisi&sort=age:desc
表示查询结果按照age字段降序.
2. term查询和terms查询
term查询并不知道分词器的存在,所以它会按照我们去搜索的关键词去查找,比较合适keyword或者date这种字段.
term查询会去查询某个字段里含有某个关键词的文档
#查询name中包含lisi的
GET /index1/user/_search
{
"query":{
"term":{"name":"lisi"}
}
}
terms可以查询某个字段里含有多个关键词的文档
#以下查询会查询出addrs字段中包含beijing或者shanghai的文档,如果两者都有,也会查询出来
GET /index1/user/_search
{
"query":{
"terms":{
"addrs":["beijing","shanghai"]
}
}
}
也可以在查询的时候控制返回结果的数量,用2个字段来表示:
from: 从哪一个文档开始
size: 需要返回的文档个数
GET /index1/user/_search
{
"from":0,
"size":2,
"query":{
"terms":{
"addrss":["beijing","shanghai"]
}
}
}
还可以在返回的查询结果中显示当前文档的版本号:
GET /index1/user/_search
{
"version":true, #设置为true,会返回文档的版本号
"query":{
"term":{
"addrss":["beijing","shanghai"]
}
}
}
match查询
match查询和term不一样,它知道分词器的存在,它会对查询的字段进行分词操作,然后再去查询.
#会把addrss字段的值拆分成beijing和shanghia 分别去查含有这两个值的文档
GET /index1/user/_search
{
"query":{
"match"{
"addrss":"beijing shanghai"
}
}
}
match_all可以查询所有的文档:
GET /lib3/user/_search
{
"query": {
"match_all": {}
}
}
multi_match:指定从多个字段查询
{
"query":{
"multi_match": {
"query": "lvyou",
"fields": ["interests","name"]
}
}
}
match_phrase:短语匹配查询,查询时结果要匹配整个短语,顺序不能乱.
{
"query":{
"match_phrase":{
"interests": "duanlian,shuoxiangsheng"
}
}
}
还可以指定查询结果中返回需要的字段:
GET /lib3/user/_search
{
"_source": ["address","name"],
"query": {
"match": {
"interests": "changge"
}
}
}
也可以对查询返回的字段进行筛选:
{
"query": {
"match_all": {}
},
"_source": {
"includes": ["name","address"],
"excludes": ["age","birthday"]
}
}
排序
对查询结果可以用sort来实现排序,desc降序,asc升序
GET /lib3/user/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order":"asc"
}
}
]
}
前缀匹配查询
#查询出name字段中前缀是zhao的文档
GET /lib3/user/_search
{
"query": {
"match_phrase_prefix": {
"name": {
"query": "zhao"
}
}
}
}
范围查询
可以用range实现范围查询,它有以下参数:
- from: 开始位置
- to: 结束位置
- include_lower: 是否包含范围的左边界,默认为true
- include_ipper: 是否包含范围的右边界,默认true
#查询生日在90年-18年的文档
GET /lib3/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
#包含20,不包含25
GET /lib3/user/_search
{
"query": {
"range": {
"age": {
"from": 20,
"to": 25,
"include_lower": true,
"include_upper": false
}
}
}
}
通配符查询
可以使用*
和?
来进行查询,*代表0个或者多个字符,?代表任意一个字符
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
}
GET /lib3/user/_search
{
"query": {
"wildcard": {
"name": "li?i"
}
}
}
模糊查询
可以用fuzzy来实现模糊查询
GET /lib3/user/_search
{
"query": {
"fuzzy": {
"interests": "chagge"
}
}
}
高亮查询结果
可以用highlight来指定查询结果的高亮字段.
GET /lib3/user/_search
{
"query":{
"match":{
"interests": "changge"
}
}, #查询结果的interests高亮显示
"highlight": {
"fields": {
"interests": {}
}
}
}
Filter查询
filter查询不计算相关性,且可以cache,速度要比query快
简单过滤查询:
#过滤出价格为40的文档
GET /lib4/items/_search
{
"post_filter": {
"term": {
"price": 40
}
}
}
bool组合过滤查询
#格式:
{
"bool": {
"must": [], #必须满足的条件---and
"should": [], #可以满足也可以不满足---or
"must_not": [] #不需要满足的条件---not
}
}
GET /lib4/items/_search
{
"post_filter": {
"bool": {
"should": [
{"term": {"itemID": "id100123"}},
{
"bool": {
"must": [
{"term": {"itemID": "id100124"}},
{"term": {"price": 40}}
]
}
}
]
}
}
}
范围过滤:
- gt: >
- lt: <
- gte: >=
- lte: <=
GET /lib4/items/_search
{
"post_filter": {
"range": {
"price": {
"gt": 25,
"lt": 50 #大于25小于50的价格
}
}
}
}
非空过滤:
GET /lib4/items/_search
{
"query" : {
"constant_score" : {
"filter": {
"exists" : { "field" : "price" }
}
}
}
}
总结
除了以上的这些查询,还有各种聚合查询等方式,都可以像数据库一样来操作文档中的数据,具体的使用不再赘述,可以直接查看官方文档.