文档中包含语句
1,索引(新增 查询 删除)
2, mapping 创建
3,文档(新增,修改,删除,批量新增)
4,文档查询(基本查询,高级查询,分页,高亮,排序)
1,使用kibana 新增 查询 删除索引:
新增请求:
PUT /goodsindex
返回:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "goodsindex"
}
查询请求:
GET /goodsindex
返回:
{
"goodsindex" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1590992920969",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "FHD9se3qSY-0XOD2t6HprQ",
"version" : {
"created" : "6080099"
},
"provided_name" : "goodsindex"
}
}
}
}
删除请求:
DELETE /goodsindex
返回:
{
"acknowledged" : true
}
2,新增mapping映射
//先创建索引
PUT /xiaohui
//创建mapping
PUT /xiaohui/_mapping/goods //或 PUT /xiaohui/goods/_mapping
{
"properties": {
"title":{
"type":"text",
"analyzer":"ik_max_word"
},
"subtilte":{
"type":"text",
"analyzer":"ik_max_word"
},
"images":{
"type":"keyword",
"index":"false"//表示不分词
},
"price":{
"type":"float"
}
}
}
一次性创建索引以及mapping映射:
PUT /xiaohui2
{
"settings": {},
"mappings": {
"goods": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"subtilte": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
}
其中字段中的属性相关解释如下:
store:是否独立存储,默认数据都存储在_source下,个别独立出来的查询效率较高,默认false
3,文档操作
添加文档:
方式一:自动生成主键id:
POST /xiaohui/goods
{
"title":"小米手机",
"subtilte":"手机描述",
"images":"http://www.shouji.com/sss.jpg",
"price":2342
}
方式二 指定id
POST /xiaohui/goods/1001
{
"title":"小米手机2",
"subtilte":"手机描述",
"images":"http://www.shouji.com/sss.jpg",
"price":2342
}
文档查询:get /xiaohui/goods/1001
修改文档:
PUT /xiaohui/goods/1001
{
"title":"红米手机2",
"subtilte":"手机描述",
"images":"http://www.shouji.com/sss.jpg",
"price":2342
}
删除文档:
删除1 根据id删除
DELETE /xiaohui/goods/1001
//删除2 根据查询条件删除
POST /xiaohui/_delete_by_query
{
"query":{
"match":{
"title":"红米手机"
}
}
}
删除3 全部删除:
POST /xiaohui/_delete_by_query
{
"query":{
"match_all":{}
}
}
批量新增(部分kibana版本可能不支持,不能进行格式化后执行)
POST /xiaohui/goods/_bulk
{"index":{}}{"title":"大米手机","images":"http://www.baidu.com/jjj.jpg","price":21222}
{"index":{}}{"title":"小米手机","images":"http://www.baidu.com/jjj.jpg","price":21222}
{"index":{}}{"title":"小米手机4A","images":"http://www.baidu.com/jjj.jpg","price":21222}
4,文档查询:
1,查询全部
POST /xiaohui/_search
{
"query": {
"match_all": {}
}
}
2,部分关键词查询
POST /xiaohui/_search
{
"query": {
"match": {
"title": "小米电视4A"
}
}
}
查询指定operator
operator:and 分词后都必须包含,or 包含其一就可以。
POST /xiaohui/_search
{
"query": {
"match": {
"title":{
"query":"小米手机4A",
"operator":"and"
}
}
}
}
多字段查询:
POST /xiaohui/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": ["title","subtilte"]
}
}
}
精确查询 term (可查询那些数字 日期 布尔值 以及未分词的字符串)
POST /xiaohui/_search
{
"query":{
"term": {
"price": {
"value": "5999"
}
}
}
}
同一字段 两个值精确查询
POST /xiaohui/_search
{
"query":{
"terms": {
"price": ["5999","21222"]
}
}
}
只返回需要的字段(左右等同)
POST /xiaohui/_search { "_source": ["title","price"], "query":{ "terms": { "price": ["5999"] } } } | POST /xiaohui/_search { "_source": { "includes":["title","price"] }, "query":{ "terms": { "price": ["5999"] } } } |
剔除不需要的字段
POST /xiaohui/_search
{
"_source": {
"excludes":["title"]
},
"query":{
"terms": {
"price": ["5999"]
}
}
}
高级查询:
布尔组合bool
POST /xiaohui/_search
{
"query":{
"bool":{
"must":{"match":{"title":"小米"}},
"must_not":{"match":{"title":"4A"}},
"should":{"match":{"subtitle":"华为"}}
}
}
}
范围查询(range):gt 大于 gte 大于等于 lt小于 lte小于等于
POST /xiaohui/_search
{
"query":{
"range": {
"price": {
"gte": 1000,
"lte": 6000
}
}
}
}
模糊查询(fuzzy):
POST /xiaohui/_search
{
"query":{
"fuzzy": {
"title":{
"value": "app22",
"fuzziness":2 //容错个数
}
}
}
}
排序(soft)
单字段排序
POST /xiaohui/_search
{
"query":{
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
多字段排序:
POST /xiaohui/_search
{
"query":{
"match":{
"title":"小米"
}
},
"sort": [
{"_score": {"order": "desc"}},
{"price": {"order": "desc"}}
]
}
高亮展示(highlight)
POST /xiaohui/_search
{
"query":{
"match":{
"title": "小米"
}
},
"highlight": {
"pre_tags": "<front color='red'>",
"post_tags": "</front>",
"fields": {"title": {}}
}
}
分页(from size)
POST /xiaohui/_search
{
"query":{
"match_all": {}
},
"from": 0,
"size": 2
}