ElasticSearch的常规操作-结构化查询

可视化工具(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":{}
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double@加贝

我这么欠揍,你来打赏我啊!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值