0.创建



PUT s1/doc/1
{
  "name":"张三",
  "age":"23",
  "sex":"男"
}

PUT s1/doc/2
{
  "name":"李四",
  "age":"30",
  "sex":"男"
}



PUT s1/doc/3
{
  "name":"小六",
  "age":"18",
  "sex":"女"
}

GET s1/doc/_search   #查询所有

GET s1/doc/1      #指定查询



 

1.两种条件查询方式

  ①字符串查询



GET s1/doc/_search?q=age:23



 

  ②结构化查询(DSL)



#1.match
GET s1/doc/_search
{
  "query": {
    "match": {
      "age": "23"
    }
  }
}


#2.match_all
GET s18/doc/_search   #查询所有
{
"query": {
"match_all": {}
}
}


#3.match_phrase (短语查询)
①
GET t1/doc/_search 
{ 
  "query": { 
    "match_phrase": { 
      "title": { 
        "query": "中国" 
} } } }


②两个词组之间有了2个词的间隔
GET t1/doc/_search { 
  "query": { 
    "match_phrase": { 
      "title": { 
        "query": "中国世界", 
        "slop": 2 
} } } }


#4.match_phrase_prefix(最左前缀查询)

GET t3/doc/_search { 
  "query": { 
    "match_phrase_prefix": { 
      "desc": "bea" 
} } }
#5.multi_match(多字段查询)(与must效果一致)

GET t3/doc/_search { 
  "query": { 
    "multi_match": { 
      "query": "beautiful", 
      "fields": ["title", "desc"]
 } } }



 

2.修改指定字段



#只修改文档1中的 "age" ,其他不变

POST s1/doc/1/_update
{
  "doc":{
    "age":25
  }
}



 

3.排序sort



#降序

GET s1/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]

}



 



#升序

GET s1/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]

}



 

4.分页



#按索引从1的位置开始查,查2个

GET s1/doc/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 2
}



 

 

5.布尔查询:

  1.should(or),must(and),must_not(not)



#①查询姓名是"张三",或者年龄是"18"的

GET s1/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "张三"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}



 



#②查询出年龄是30的男性

GET s1/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "sex": "男"
          }
        },
        {
          "match": {
            "age": "30"
          }
        }
      ]
    }
  }
}



 



#③查询出既不是男性,又不是30岁的

GET s1/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "sex": "男"
          }
        },
        {
          "match": {
            "age": "30"
          }
        }
      ]
    }
  }
}



 

  2.大于(gt),大于等于(gte),小于(lt),小于等于(lte)



#①大于(其他类似)
查询大于25岁的男性

GET s1/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "sex": "男"
          }
        }
        
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 25
            
          }
        }
      }
    }
  }
}



 



#②查询小于25岁的非男性

GET s1/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "sex": "男"
          }
        }
        
      ],
      "filter": {
        "range": {
          "age": {
            "lt": 25
            
          }
        }
      }
    }
  }
}



 

 

6.高亮查询



PUT s2/doc/1
{
  "name":"wangdi",
  "desc": "骚的打漂"
}


GET s2/doc/_search
{
  "query": {
    "match": {
      "desc": "打漂"
    }
  },
  "highlight": {          #高亮显示查询的结果
    "pre_tags": "<b style='color:red;font-size:20px;' class='wangdi'>", 
    "post_tags": "</b>", 
    "fields": {
      "desc": {}
    }
  }
}



 

7.结果过滤



#①只将结果的姓名显示

GET s1/doc/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "_source": "name"
}



#②将结果的姓名和年龄显示

GET s1/doc/_search
{
  "query": {
    "match": {
      "name": "张三"
    }
  },
  "_source": ["name","age"]
}



 

8.聚合查询(sum,max,min,avg)



求男性的年龄总和

GET s1/doc/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs":{
    "my_sum":{
      "sum": {
        "field": "age"
      }
    }
  }
}



 



求男性的最大值

GET s1/doc/_search
{
  "query": {
    "match": {
      "sex": "男"
    }
  },
  "aggs":{
    "my_max":{
      "max": {
        "field": "age"
      }
    }
  }
}



 



# 求所有人的平均值 avg
GET s1/doc/_search
{
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}



 

9.分组



#根据年龄,10-20,,20-30, 30-50 进行分组


GET s1/doc/_search
{
  "aggs": {
    "my_group":{
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 50
          }
        ]
      }
    }
  }
}



 



对分组结果进行求和


GET s1/doc/_search
{

  "aggs": {
    "my_group":{
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 50
          }
        ]
      },
      "aggs":{
        "my_sum":{
          "sum":{
              "field":"age"
        }
      }
    }
    }
    
  }
}



 

 

10.映射(mappings)

  自定义表结构



简单类型,如文本(text)、关键字(keyword)、日期(data)、整形(long)、
双精度(double)、布尔(boolean)或ip。

可以是支持JSON的层次结构性质的类型,如对象或嵌套。

或者一种特殊类型,如geo_point、geo_shape或completion。



 

  ①基本的映射(可添加新字段,es自动生成)



PUT s3
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text"
        },
          "age":{
            "type":"long"
          },
          "desc":{
            "type":"text"
          }
        }
      }
    }
}


GET s3/_mapping

GET s3



 

  ②dynamic的三种状态



#① true 状态(默认状态,可以添加新字段,可建立新映射关系,)

PUT s4
{
  "mappings": {
    "doc":{
      "dynamic": true,
      "properties":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}



 

 



② false 状态(可建立新字段,不能建立新的映射关系,不能做查询的条件,伴随主查询条件出现)

PUT s5
{
  "mappings": {
    "doc":{
      "dynamic": false,
      "properties":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}



 

 

 



③ strict 状态(严格类型,不可添加新字段)

PUT s6
{
  "mappings": {
    "doc":{
      "dynamic":"strict",
      "properties":{
        "name":{
          "type":"text"
        }
      }
    }
  }
}



 

 

  ③ ignore_above(限制字节数)



超过时不能进行搜索(索引),但会插入数据

PUT s7
{
  "mappings": {
    "doc":{
      "properties":{
        "title":{
          "type":"keyword",
          "ignore_above": 10
        }
      }
    }
  }
}



 

  ④ index()



# true:可以对该字段(作为主查询)进行搜索  (默认)
# false: 不能对该字段作为搜索条件 进行搜索


PUT s8
{
  "mappings": {
    "doc":{
      "properties":{
        "t1":{
          "type":"text",
          "index": true
        },
        "t2":{
          "type":"text",
          "index": false
        }
      }
    }
  }
}



 

  ⑤copy_to 复制属性



该属性允许我们将多个字段的值复制到组字段中,然后将组字段作为单个字段进行查询



 



建立映射

PUT s9
{
  "mappings": {
    "doc":{
      "properties":{
        "t1":{
          "type":"text",
          "copy_to":"full_name"
        },
        "t2":{
          "type":"text",
          "copy_to":"full_name"
        },
        "full_name":{
          "type":"text"
        }
      }
    }
  }
}



 



插入数据

PUT s9/doc/1
{
  "t1":"xxx",
  "t2":"ooo"
}



 



进行查询两种方式

GET s9/doc/_search
{
  "query": {
    "match": {
      "t1": "xxx"
    }
  }
}


GET s9/doc/_search
{
  "query": {
    "match": {
      "full_name": "xxx"
    }
  }
}



 

  ⑥多层嵌套(内套在加peoperties)



PUT w1
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"long"
        },
        "info":{
          "properties":{
            "addr":{
              "type":"text"
            },
            "tel":{
              "type":"long"
            }
          }
        }
      }
    }
  }
}



 



插入数据

PUT w1/doc/2
{
  "name":"tom",
  "age":"18",
  "info":{
    "addr":"北京",
    "tel":"10010"
  }
}



 



查询嵌套的条件

GET w1/doc/_search
{
  "query": {
    "match": {
      "info.tel": "10010"
    }
  }
}



 

  ⑦settings(设置主,副分片)



PUT w2
{
  "mappings": {
    "doc":{
      "properties":{
        "title":{
          "type":"text"
        }
      }
    }
  }, 
  "settings": {
    "number_of_shards": 3,     #主
    "number_of_replicas": 3  #副
  }
}