ElasticSearch
一 启动ElasticSearch
点击elasticsearch bin目录下的第一个bat
浏览器输入网址 localhost:9200
二 图形化工具
有两种工具可选elasticsearch-head与kibana
2.1 elasticsearch-head
需要nodejs环境,在elasticsearch-head目录中打开cmd 输入
初次使用需要如下语句:
npm install -g grunt-cli
npm install
启动head:
grunt server
然后访问 http://localhost:9100/,并连接 http://localhost:9200/
如果不能连接,打开elasticsearch中的config/elasticsearch.yml,添加跨域允许
http.cors.enabled: true
http.cors.allow-origin: "*"
2.2 Kibana
下载对应Es版本的Kibana,然后双击bin目录下的kiban.bat
kibana改为中文: kibana.yml末尾增加
i18n.locale: "zh-CN"
三 增删改查
3.1 增
增操作包含:1增加索引 & 2增加文档
3.1.1 一次性生成索引
PUT http://localhost:9200/blog
{
"settings": {
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"type":{"type":"keyword"},
"name":{"type":"text"},
"country":{"type":"keyword"},
"age":{"type":"integer"},
"date":{
"type":"data",
"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
}
3.1.2 先生成空索引,然后在添加在设置mappings
(1)先新建索引
PUT http://ip:port/索引名称
PUT http://localhost:9200/blog
{
"settings": {
"number_of_shards":3,
"number_of_replicas":1
}
}
(2)在设置_mappings
post http://ip:port/索引名称/_mappings
POST localhost:9200/blog/_mappings
{
"properties":{
"id":{
"type":"long",
"store":true
},
"title":{
"type":"text",
"store":true,
"index":true,
"analyzer":"standard"
},
"content":{
"type":"text",
"store":true,
"index":true,
"analyzer":"standard"
}
}
}
3.1.3 向索引库中添加文档
POST http://ip:port/索引名称/_doc/文档ID(可以不填)
POST http://localhost:9200/blog/_doc/1
{
"id":1,
"title":"学习java的全文检索工具",
"content":"Luence是某某某家伙写的,这个东西真的超级不好用,所有又有人写了ElasticSearch"
}
如果不填写ID,那么将自动生成ID
3.2 删
删除操作主要分:1 删除文档 & 2 删除索引
3.2.1 删除文档
delete http://ip:port/索引名称/_doc/_id
3.2.2 删除索引
delete http://ip:port/索引名称
3.3 改
注:更新\修改的原理是先删除后添加,因为Luence是这个原理
3.3.1 修改文档数据
post http://ip:port/索引名称/_update/_id
{
"doc":{
"title":"我去前面探探路"
}
}
实例:修改索引blog中id为3的文档title为 ”我去前面探探路“
POST http://localhost:9200/blog/_update/3
{
"doc":{
"title":"我去前面探探路"
}
}
3.3.2 修改索引
3.4 查
3.4.1 根据ID查询
GET http://ip:port/索引名称/_doc/_id
GET http://localhost:9200/blog/_doc/3
3.4.2 根据关键词查询
仅仅对输入的内容进行查询
post http://ip:port/索引名称/_search/
{
"query":{
"term":{
"title":"java"
}
}
}
3.4.3 queryString查询
根据输入的内容进行分析后查询
post http://ip:port/索引名称/_search/
{
"query":{
"query_string":{
"default_field":"title",
"query":"java"
}
}
}
3.4.4 IK分词器
3.4.4.1分词器效果查看
注意:在7.X版本后分词器写法有更改如果在7.X使用以下写法:
get http://localhost:9200/_analyze?analyzer=standard&text=variously spelled wildcard or wild-card,alse known as at-large berth
返回结果报错为:
{
"error": {
"root_cause": [
{
"type": "parse_exception",
"reason": "request body or source parameter is required"
}
],
"type": "parse_exception",
"reason": "request body or source parameter is required"
},
"status": 400
}
正确写法:
get http://localhost:9200/_analyze
{
"analyzer":"standard",
"text":"variously spelled wildcard or wild-card,alse known as at-large berth"
}
3.4.4.2 安装IK插件
1> https://github.com/medcl/elasticsearch-analysis-ik 下载对应版本IK
下载的版本一定要与ES的版本对应,如果不是闲得慌,千万不要尝试最新版的ES和IK
2> 解压后放到plugins目录,重启即可!
3.4.4.3 重新构建以IK为基础的索引
put http://localhost:9200/blog
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {"type": "long", "store": "true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"},
"title": {"type": "text","store":"true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"},
"content": {"type": "text","store":"true","analyzer": "ik_max_word", "search_analyzer": "ik_smart"}
}
}
}
新建时报错:
{ "type": "access_control_exception",
"reason": "access denied (\"java.io.FilePermission\" \"D:\\Program%20Files%20(x86)\\programer\\elasticsearch-7.4.0\\plugins\\elasticsearch-analysis-ik-7.4.0\\config\\IKAnalyzer.cfg.xml\" \"read\")"}
原因:elasticsearch安装路径存在空格
更改路径后继续报错:
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "Mapping definition for [id] has unsupported parameters: [analyzer : ik_smart]"
}
],
"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [_doc]: Mapping definition for [id] has unsupported parameters: [analyzer : ik_smart]",
"caused_by": {
"type": "mapper_parsing_exception",
"reason": "Mapping definition for [id] has unsupported parameters: [analyzer : ik_smart]"
}
},
"status": 400
}
原因:ID不能用IK,删掉ID那一行的analyzer和search_analyzer即可
3.4.4.3 对查询结果高亮
post http://localhost:9200/blog/_search
{
"query" : { "match" : { "content" : "校车" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
结果:
"hits": [
{
"_index": "blog",
"_type": "_doc",
"_id": "2",
"_score": 0.2876821,
"_source": {
"id": "2",
"title": "外国人永久居留条例!",
"content": "美国留给伊拉克的是个烂摊子吗!公安部:各地校车将享最高路权"
},
"highlight": {
"content": [
"公安部:各地<tag1>校车</tag1>将享最高路权"
]
}
}
]
四 评分机制
4.1 tfidf算法