玩转CRDU

创建索引

PUT course
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1,
    "analysis": {
      "analyzer": {
        "main_analyzer": {
          "char_filter": [
            "html_strip",
            "abbr"
          ],
          "tokenizer": "standard",
          "stopwords": [
            "a",
            "an",
            "the",
            "的",
            "乃",
            "呢",
            "么",
            "呀",
            "啊",
            "嗯",
            "哦"
          ],
          "filter": [
            "lowercase"
          ]
        },
        "pinyin_analyzer": {
          "tokenizer": "pinyin_tokenizer"
        }
      },
      "char_filter": {
        "abbr": {
          "type": "mapping",
          "mappings": [
            "Elasticsearch=>es",
            "Golang=>go"
          ]
        }
      },
      "tokenizer": {
        "pinyin_tokenizer": {
          "type": "pinyin",
          "keep_separate_first_letter": true,
          "keep_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 16,
          "lowercase": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword",
        "doc_values": false
      },
      "course_type": {
        "type": "keyword",
        "doc_values": false
      },
      "teacher_id": {
        "type": "keyword",
        "doc_values": false
      },
      "title": {
        "type": "text",
        "analyzer": "main_analyzer",
        "search_analyzer": "main_analyzer",
        "fields": {
          "pinyin": {
            "type": "text",
            "analyzer": "pinyin_analyzer",
            "search_analyzer": "pinyin_analyzer"
          }
        }
      },
      "tag": {
        "type": "text",
        "analyzer": "main_analyzer",
        "search_analyzer": "main_analyzer",
        "fields": {
          "keyword": {
            "type": "keyword",
            "doc_values": false
          }
        }
      },
      "weight": {
        "type": "short",
        "doc_values": true
      },
      "desc": {
        "type": "text",
        "analyzer": "main_analyzer",
        "search_analyzer": "main_analyzer"
      },
      "mtime": {
        "type": "long",
        "doc_values": true
      },
      "ctime": {
        "type": "long",
        "doc_values": false
      }
    }
  }
}

创建

单条写

POST course/_doc/1
{
  "id": 1,
  "teacher_id":1,
  "course_type":1,
  "title": "Golang + Elasticsearch仿慕课网实现企业级搜索实战",
  "tag": [
    "go",
    "es"
  ],
  "weight": 10,
  "desc": "go and es 实战课程",
  "mtime": 1639728191,
  "ctime": 1639728191
}

批量写bulk

POST course/_bulk
{"create":{"_index":"course","_id":2}}
{"id":2,"teacher_id":1,"course_type":2,"title":"go&es","tag":["java","es"],"weight":10,"desc":"go and es 实战课程","mtime":1639728191,"ctime":1639728191}

批量覆盖写

POST course/_bulk
{"index":{"_index":"course","_id":2}}
{"id":2,"teacher_id":1,"course_type":2,"title":"go&es","tag":["java","es"],"weight":10,"desc":"go and es 实战课程","mtime":1639728191,"ctime":1639728191}

修改

单条修改

#更新文档方式1,注意:此种方式必须传递文档的所有参数
POST course/_doc/1
{
  "id": 1,
  "teacher_id":1,
  "course_type":1,
  "title": "Golang + Elasticsearch仿慕课网实现企业级搜索实战",
  "tag": [
    "go",
    "es"
  ],
  "weight": 10,
  "desc": "go and es 实战课程",
  "mtime": 1639728191,
  "ctime": 1639728191
}
#更新文档2,此种方式只需要传递待更新的字段
POST course/_update/1
{
  "doc": {
    "title": "Golang+Elasticsearch实战课程"
  }
}
# update_by_query
POST course/_update_by_query
{
  "script": {
    "source": "ctx._source.title='通过_update_by_query修改'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "id": 1
    }
  }
}

通过bulk修改

POST _bulk
{ "update" : {"_id" : "1", "_index" : "course"} }
{ "doc" : {"title" : "通过bulk修改"} }

通过script方式修改

POST course/_update/1
{
  "script": {
    "source": "ctx._source.title=params.title",
    "lang": "painless",
    "params": {
      "title": "通过script修改"
    }
  }
}

查询

直接通过文档id查询

查询单条结果

GET course/_doc/1

查询多条结果

GET /_mget
{
  "docs": [
    {
      "_index": "course",
      "_id": "1"
    },
    {
      "_index": "course",
      "_id": "2"
    }
  ]
}

复合查询

GET course/_search
{
	"from": 0,
	"highlight": {
		"fields": {
			"desc": {},
			"tag": {},
			"title": {},
			"title.pinyin": {
			}
		}
	},
	"query": {
		"bool": {
			"minimum_should_match": "1",
			"must": {
				"term": {
					"course_type": 1
				}
			},
			"should": [{
				"match": {
					"title": {
						"boost": 10,
						"operator": "and",
						"query": "shizhan"
					}
				}
			}, {
				"match_phrase": {
					"title.pinyin": {
						"boost": 5,
						"slop": 0,
						"query": "shizhan"
					}
				}
			}]
		}
	},
	"size": 20
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

少林码僧

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值