#初始化准备(创建库和user)
#创建表
PUT /libzyh
{
“settings”:{
“number_of_shards” : 3,
“number_of_replicas” : 0
},
“mappings”:{
“user”:{
“properties”:{
“name”: {“type”:“text”},
“address”: {“type”:“text”},
“age”: {“type”:“integer”},
“interests”: {“type”:“text”},
“birthday”: {“type”:“date”}
}
}
}
}
#添加user
PUT /libzyh/user/1
{
“name”:“zhangsan”,
“address”:“bei jing chang ping qu sha yang lu”,
“age”:19,
“birthday”:“2000-10-12”,
“interests”:“xi huan hejiu,duanlian,lvyou”
}
PUT /libzyh/user/2
{
“name”:“lisi”,
“address”:“bei jing chang ping qu sha yang lu”,
“age”:18,
“birthday”:“2001-10-12”,
“interests”:“xi huan tingyinyue,changge,tiaowu”
}
PUT /libzyh/user/3
{
“name”:“wangwu”,
“address”:“bei jing chang ping qu sha yang lu”,
“age”:33,
“birthday”:“1986-10-12”,
“interests”:“xi huan meinv,meinv,meinv”
}
PUT /libzyh/user/4
{
“name”:“zhaoliu”,
“address”:“bei jing chang ping qu sha yang lu”,
“age”:31,
“birthday”:“1988-10-12”,
“interests”:“xi huan hejiu,wanyouxi,shuijiao”
}
PUT /libzyh/user/6
{
“name”:“lvyou”,
“address”:“bei jing chang ping qu sha yang lu,lvyou”,
“age”:32,
“birthday”:“1987-10-12”,
“interests”:“xi huan chifan,shuijiao,dadoudou,changge”
}
#查询所有
GET /libzyh/_search
#查询name为lisi的
GET /libzyh/_search?q=name:lisi
#查询年龄为18的
GET /libzyh/_search?q=age:18
#获取interests字段中有changge的
GET /libzyh/user/_search/
{
“query”: {
“term”: {“interests”: “changge”}
}
}
#from size 相当于sql语句中的limit
GET /libzyh/user/_search/
{
“from”: 0,
“size”: 1,
“query”: {
“term”: {“interests”: “changge”}
}
}
#显示数据的版本
GET /libzyh/user/_search/
{
“from”: 0,
“size”: 1,
“version”: true,
“query”: {
“term”: {“interests”: “changge”}
}
}
#匹配name为lisi的
GET /libzyh/user/_search/
{
“from”: 0,
“size”: 1,
“version”: true,
“query”: {
“match”: {
“name”: “lisi”
}
}
}
#查询user表中的所有
GET /libzyh/user/_search/
{
“version”: true,
“query”: {
“match_all”: {}
}
}
#多个匹配,query:匹配词语,field在哪个区域查询
GET /libzyh/user/_search
{
“query”:{
“multi_match”: {
“query”: “lvyou”,
“fields”: [“interests”,“name”]
}
}
}
#中的所有分词,并且保证各个分词的相对位置不变:,匹配interests字段中同时含有这两个词的user
GET libzyh/user/_search
{
“query”:{
“match_phrase”:{
“interests”: “duanlian,shuoxiangsheng”
}}}
#返回指定字段address,和name
GET /libzyh/user/_search
{
“_source”: [“address”,“name”],
“query”: {
“match”: {
“interests”: “changge”
}
}
}
#控制加载字段
GET /libzyh/user/_search
{
“query”: {
“match_all”: {}
},
“_source”: {
“includes”: [“name”,“address”],
“excludes”: [“age”,“birthday”]
}
}
#还可以使用通配符*
GET /libzyh/user/_search
{
“_source”: {
“includes”: “addr*”,
“excludes”: [“name”,“bir*”]
},
“query”: {
“match_all”: {}
}
}
DELETE lib3
#按照年龄的升序排序
GET /libzyh/user/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”:“asc”
}
}
]
}
#前缀查询,查询所有name以zhao开头
GET /libzyh/user/_search
{
“query”: {
“match_phrase_prefix”: {
“name”: {
“query”: “zhao”
}
}
}
}
#查询生日在这两个之间的
GET /libzyh/user/_search
{
“query”: {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
#查询n年龄在20-25之间的user include_lower是否包含最小值,
#"include_upper"是否包含最大值
GET /libzyh/user/_search
{
“query”: {
“range”: {
“age”: {
“from”: 1,
“to”: 25,
“include_lower”: true,
“include_upper”: false
}
}
}
}
#wildcard通配查询,*代表多个字符,与sql查询中的%类似
#?代表单个字符,与sql查询中_相似只能匹配一个字符
GET /libzyh/user/_search
{
“query”: {
“wildcard”: {
“name”: “zhao*”
}
}
}
#查询name为lvyo?的user
GET /libzyh/user/_search
{
“query”: {
“wildcard”: {
“name”: “lvyo?”
}
}
}
#fuzzy实现模糊查询 就算有一点不一样也可以匹配的到
GET /libzyh/user/_search
{
“query”: {
“fuzzy”: {
“interests”: “chagge”
}
}
}
亮的区域, 般和查询的位置 致
GET /libzyh/user/_search
{
“query”:{
“match”:{
“interests”: “changge”
}
},
“highlight”: {
“fields”: {
“interests”: {}
}
}
}
#Filter查询
#filter是不计算相关性的,同时可以cache。因此,filter速度要快于query。测试,先创建一个表
DELETE /lib12
POST /lib12/items/_bulk
{“index”: {"_id": 1}}
{“price”: 40,“itemID”: “ID100123”}
{“index”: {"_id": 2}}
{“price”: 50,“itemID”: “ID100124”}
{“index”: {"_id": 3}}
{“price”: 25,“itemID”: “ID100124”}
{“index”: {"_id": 4}}
{“price”: 30,“itemID”: “ID100125”}
{“index”: {"_id": 5}}
{“price”: null,“itemID”: “ID100127”}
#过滤出价格为40的所有结果
GET /lib12/items/_search
{
“post_filter”: {
“term”: {
“price”: 40
}
}
}
#过滤出价格是25或40的所有结果
GET /lib12/items/_search
{
“post_filter”: {
“terms”: {
“price”: [25,40]
}
}
}
#因为itemid被分词索引了,如果使 term.需要加.keyword
GET /lib12/items/_search
{
“post_filter”: {
“term”: {
“itemID.keyword”: “ID100123”
}
}
}
#同上而match则不用
GET /lib12/items/_search
{
“post_filter”: {
“match”: {
“itemID”: “ID100123”
}
}
}
#查看分词索引
GET /lib12/_mapping