elasticSearch基础- query 查询

# 基本查询
PUT /lib3
{
  "settings":{
    "number_of_shards": 3, 
    "number_of_replicas": 0
  }, 
  "mappings": {
      "properties":{
        "name":{"type":"text", "analyzer":"ik_max_word"},
        "address":{"type":"text", "analyzer": "ik_max_word"},
        "age":{"type":"integer"},
        "interests":{"type":"text", "analyzer":"ik_max_word"},
        "birthday":{"type":"date"}
    }
  }
}

PUT /lib3/user/1
{
  "name":"赵六",
  "address":"黑龙江省铁岭",
  "age": 50,
  "birthday": "1970-12-12",
  "interests": "喜欢喝酒,锻炼,说相声"
}

PUT /lib3/user/2
{
  "name":"赵明",
  "address":"北京海淀清河",
  "age": 20,
  "birthday": "1998-10-12",
  "interests": "喜欢喝酒,锻炼,唱歌"
}

PUT /lib3/user/3
{
  "name":"lisi",
  "address":"北京海淀清河",
  "age": 23,
  "birthday": "1998-10-12",
  "interests": "喜欢喝酒,锻炼,说相声"
}

PUT /lib3/user/4
{
  "name":"王五",
  "address":"北京海淀清河",
  "age": 26,
  "birthday": "1995-10-12",
  "interests": "喜欢编程,听音乐,旅游"
}

PUT /lib3/user/5
{
  "name":"张三",
  "address":"北京海淀清河",
  "age": 29,
  "birthday": "1988-10-12",
  "interests": "喜欢摄影,听音乐,跳舞"
}
#------------------start term-------------------------
#查询name含有赵关键词
GET /lib3/user/_search/
{
  "from": 0,
  "size": 1,
  "query":{
    "term": {"name": "赵"}
  }
}

#查询interests含有"喝酒","唱歌"关键词, 差不出来!!!
GET /lib3/_search/
{
  "query":{
    "terms": 
      {"interests": ["喝酒","唱歌"]}
  }
}

#from size
GET /lib3/_search/
{
  "from": 0,
  "size": 1,
  "query":{
    "term": {"name": "赵"}
  }
}

#from size
GET /lib3/_search/
{
  "from": 0,
  "size": 1,
   "version":true,
  "query":{
    "term": {"name": "赵"}
  }
}

#------------------start match-------------------------
#match 知道分词器的存在
GET /lib3/_search/
{
  "query":{
    "match": {"name": "赵六"}
  }
}

GET /lib3/_search/
{
  "query":{
    "match_all": {}
  }
}

GET /lib3/_search/
{
  "query":{
    "multi_match": 
    {
      "query": "唱歌",
      "fields": ["interests", "name"]
    }
  }
}

#match_phrase 短语匹配查询
GET /lib3/_search/
{
  "query":{
    "match_phrase": 
    {
      "interests": "锻炼,说相声"
    }
  }
}
#_source 控制返回字段
GET /lib3/_search/
{
  "query":{
    "match_all": {}
  },
  "_source": {
    "includes":["name","address"],
    "excludes":["age","birthday"]
  }
}

GET /lib3/user/_search/
{
  "query":{
    "match_all": {}
  },
  "_source": {
    "includes":["add**"],
    "excludes":["age","birthday"]
  }
}

# 排序
GET /lib3/_search/
{
  "query":{
    "match_all": {}
  },
  "sort":[
    {
     "age": {"order":"asc"}
    }
  ]
}

# 前缀匹配 match_phrase_prefix
GET /lib3/_search/
{
  "query":{
    "match_phrase_prefix": 
    {
      "name": {
        "query": "赵"
      }
    }
  }
}

# 范围查询 range
GET /lib3/_search/
{
  "query":{
   "range":{
     "age": {
       "gte": 20,
       "lte": 25
     }
   }
  }
}

# whildcard 查询
#?一个
GET /lib3/_search/
{
  "query":{
   "wildcard":{
    "name": "li?i"
   }
  }
}
# *多个
GET /lib3/_search/
{
  "query":{
   "wildcard":{
    "name": "l*i"
   }
  }
}

# fuzzy实现模糊查询
GET /lib3/_search/
{
  "query":{
   "fuzzy":{
    "name": "赵"
   }
  }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值