可视化工具(Postman)操作
创建空索引
PUT localhost:9200/索引库名称
{
"settings":{
"index":{
"number_of_shards":3, //分片数量
"number_of_replicas":0 //副本数量
}
}
}
修改副本数量
PUT /my_index/_settings
{
"number_of_replicas": 1
}
创建映射(插入数据)
格式:POST localhost:9200/{索引库名称}/{文档类型}/{id}
POST localhost:9200/myindex/doc/qaeTWnEB5aBC1lRt3B7i (id可不写,自动生成)
{
"name" : "如梦令",
"description" : "昨夜雨疏风骤,浓睡不消残酒,试问卷帘人,却道海棠依旧,知否,知否,应是绿肥红瘦",
"font" : "123"
}
覆盖更新
格式:PUT localhost:9200/{索引库名称}/{文档类型}/{id}
{
"name" : "如梦令覆盖了",
"description" : "覆盖了",
"studymodel" : "123覆盖了"
}
这种方式没有覆盖的部分会没有数据
局部更新
格式:POST localhost:9200/{索引库名称}/{文档类型}/{id}/_update
{
"doc":{
"name" : "如梦令覆盖了"
}
}
批量添加
格式:POST localhost:9200/{索引库名称}/{文档类型}/_bulk
{"create":{"_index":"索引库名称","_type":"文档类型","_id":"1002"}}
{"id":"1002","name":"李四","age":"22","sex":"男"}
{"create":{"_index":"索引库名称","_type":"文档类型","_id":"1001"}}
{"id":"1001","name":"王五","age":"22","sex":"男"}
删除数据
格式:DELETE localhost:9200/{索引库名称}/{文档类型}/{id}
如果删除一条不存在的数据,会404,删除并不会立即从磁盘移除
搜索
搜索全部:GET localhost:9200/{索引库名称}/{文档类型}/_search(默认返回10条数据)
根据id搜索:GET localhost:9200/{索引库名称}/{文档类型}/qaeTWnEB5aBC1lRt3B7i
查询自定义字段:GET localhost:9200/{索引库名称}/{文档类型}/{id}/_source=name,id
只查询元数据:GET localhost:9200/{索引库名称}/{文档类型}/{id}/_source
只查询原数据指定字段:GET localhost:9200/{索引库名称}/{文档类型}/{id}/_source?_source=name,id
判断文档是否存在:HEAD localhost:9200/{索引库名称}/{文档类型}/{id}
DSL(Domain Specific Language:特定领域语言)
是ES提出的基于json的搜索方式,在搜索时传入特定的json格式的数据来完成不同的搜索需求。
以下查询可结合Kibana使用
根据字段值搜索匹配
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"match"{
"name":"zhangsan"
}
}
}
搜索匹配需要的字段
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"match_all": {}
},
"_source" : ["name","studymodel"]//_source:source源过虑设置,指定结果中所包括的字段有哪些。
}
分页查询
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"from" : 0, "size" : 1,
"query": {
"match_all": {}
},
"_source" : ["name","studymodel"]
}
Term Query精确查询
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"term" : {
"name": "spring"
}
},
"_source" : ["name","studymodel"]
}
根据多个id精确匹配
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"ids" : {
"type" : "doc",
"values" : ["3", "4", "100"]
}
}
}
聚合
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"aggs": {
"all_interests" : {
"terms" : {
"filed":"age"
}
}
}
}
match Query即全文检索(表示只要有一个词匹配上就得分)
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"match" : {
"description" : {
"query" : "spring开发",
"operator" : "or"
}
}
}
}
multi Query匹配多个字段
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"multi_match" : {
"query" : "spring css",
"minimum_should_match": "50%",
"fields": [ "name", "description" ]
}
}
}
提高boost,name中包括关键字的文档排在前边
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"query": {
"multi_match" : {
"query" : "spring框架",
"minimum_should_match": "50%",
"fields": [ "name^10", "description" ]
}
}
}
布尔查询
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"_source" : [ "name", "studymodel", "description"],
"from" : 0, "size" : 1,
"query": {
"bool" : {
"must":[
{
"multi_match" : {
"query" : "spring框架",
"minimum_should_match": "50%",
"fields": [ "name^10", "description" ]
}
},
{
"term":{
"studymodel" : "201001"
}
}
]
}
}
}
过滤器(注意:range和term一次只能对一个Field设置范围过虑。)
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"_source" : [ "name", "studymodel", "description","price"],
"query": {
"bool" : {
"must":[
{
"multi_match" : {
"query" : "spring框架",
"minimum_should_match": "50%",
"fields": [ "name^10", "description" ]
}
}
],
"filter": [
{ "term": { "studymodel": "201001" }},
{ "range": { "price": { "gte": 60 ,"lte" : 100}}}
]
}
}
}
排序(过虑价格范围的文档,并对结果进行排序,先按studymodel降序,再按价格升序)
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"_source" : [ "name", "studymodel", "description","price"],
"query": {
"bool" : {
"filter": [
{ "range": { "price": { "gte": 0 ,"lte" : 100}}}
]
}
},
"sort" : [
{
"studymodel" : "desc"
},
{
"price" : "asc"
}
]
}
高亮显示
POST localhost:9200/{索引库名称}/{文档类型}/_search
{
"_source" : [ "name", "studymodel", "description","price"],
"query": {
"bool" : {
"must":[
{
"multi_match" : {
"query" : "开发框架",
"minimum_should_match": "50%",
"fields": [ "name^10", "description" ],
"type":"best_fields"
}
}
],
"filter": [
{ "range": { "price": { "gte": 0 ,"lte" : 100}}}
]
}
},
"sort" : [
{
"price" : "asc"
}
],
"highlight": {
"pre_tags": ["<tag1>"],
"post_tags": ["</tag2>"],
"fields": {
"name": {},
"description":{}
}
}
}