Elasticsearch

一、Elasticsearch入门

image-20220401164205283

①配置集群名称(cluster.name)

打开config里的elasticsearch.yml,在里面配置

cluster.name: nowcoder

②配置path.data

path.data: c:\work\data\elasticsearch\data

③配置path.logs

path.logs: c:\work\data\elasticsearch\logs

④配置环境变量

image-20220401170111447

⑤安装中文分词插件

打开GitHub

搜索 elasticsearch ik(分词器的版本要与es的版本对应)

image-20220401170614202

下载对应版本的ik分词器,解压到elasticsearch的plugins文件夹中的ik文件夹中

image-20220401170732746

1.命令行访问

输入命令

curl -X GET "localhost:9200/_cat/health?v"

image-20220401171505413

查看节点

curl -X GET "localhost:9200/_cat/nodes?v"

image-20220401171612207

查看索引

curl -X GET "localhost:9200/_cat/indices?v"

image-20220401171724249

建立索引(其中test1指代的是索引名称)

curl -X PUT "localhost:9200/test1"	

image-20220401171929000

删除索引

curl -X DELETE "localhost:9200/test1"

image-20220401172110833

2.使用测试软件操作es

在test1索引中插入数据

image-20220401172840762

查询数据

image-20220401173036237

删除数据

image-20220401173117339

3.测试

①先插入几条数据

localhost:9200/test1/_doc/1
localhost:9200/test1/_doc/2
localhost:9200/test1/_doc/3
{
    "title":"互联网",
    "content":"寻求一份运营的工作"
}

{
    "title":"互联网招聘",
    "content":"招聘一名资深程序员"
}

{
    "title":"实习生推荐",
    "content":"本人在一家互联网公司任职,可推荐实习开发岗位!"
}

②搜索test1的全部数据

localhost:9200/test1/_search

结果:

{
	"took": 418,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "1",
				"_score": 1,
				"_source": {
					"title": "互联网",
					"content": "寻求一份运营的工作"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "2",
				"_score": 1,
				"_source": {
					"title": "互联网招聘",
					"content": "招聘一名资深程序员"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "3",
				"_score": 1,
				"_source": {
					"title": "实习生推荐",
					"content": "本人在一家互联网公司任职,可推荐实习开发岗位!"
				}
			}
		]
	}
}

③搜索title中包括 “互联网” 三个字关键词的数据

localhost:9200/test1/_search?q=title:互联网

结果:

{
	"took": 41,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.6264062,
		"hits": [
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "1",
				"_score": 2.6264062,
				"_source": {
					"title": "互联网",
					"content": "寻求一份运营的工作"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "2",
				"_score": 2.063605,
				"_source": {
					"title": "互联网招聘",
					"content": "招聘一名资深程序员"
				}
			}
		]
	}
}

④搜索content中带有 “运营实习” 关键字的

localhost:9200/test1/_search?q=content:运营实习

结果:

{
	"took": 4,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.6661782,
		"hits": [
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "1",
				"_score": 2.6661782,
				"_source": {
					"title": "互联网",
					"content": "寻求一份运营的工作"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "3",
				"_score": 1.6920671,
				"_source": {
					"title": "实习生推荐",
					"content": "本人在一家互联网公司任职,可推荐实习开发岗位!"
				}
			}
		]
	}
}

⑤多字段匹配,要通过body请求体去提交

localhost:9200/test1/_search

编写请求体:

{
    "query":{
        "multi_match":{
            "query":"互联网",
            "fields":["title","content"]
        }
    }
}

结果:

{
	"took": 10,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 2.3507023,
		"hits": [
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "3",
				"_score": 2.3507023,
				"_source": {
					"title": "实习生推荐",
					"content": "本人在一家互联网公司任职,可推荐实习开发岗位!"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "1",
				"_score": 1.6130526,
				"_source": {
					"title": "互联网",
					"content": "寻求一份运营的工作"
				}
			},
			{
				"_index": "test1",
				"_type": "_doc",
				"_id": "2",
				"_score": 1.3265235,
				"_source": {
					"title": "互联网招聘",
					"content": "招聘一名资深程序员"
				}
			}
		]
	}
}

二、Springboot整合Elasticsearch

image-20220401180825652

1.引入依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.配置es

# ElasticsearchProperties
spring.data.elasticsearch.cluster-name=nowcoder
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

3.解决冲突

因为redis和es都基于netty所以启动的时候容易发送冲突,在启动类下加入此代码

	@PostConstruct
	public void init(){
		//解决netty启动冲突问题
		//see Netty4Utils.setAvailableProcessors()
		System.setProperty("es.set.netty.runtime.available.processors","false");
	}

4.在实体类中编辑分词器

@ToString
@Data
@Document(indexName = "discusspost",type = "_doc",shards= 6,replicas = 3)
public class DiscussPost {

    @Id
    private int id;

    @Field(type = FieldType.Integer)
    private int userId;

    //互联网校招
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_smart")
    private String title;

    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_smart")
    private String content;

    @Field(type = FieldType.Integer)
    private int type;

    @Field(type = FieldType.Integer)
    private int status;

    @Field(type = FieldType.Date)
    private Date createTime;

    @Field(type = FieldType.Integer)
    private int commentCount;

    @Field(type = FieldType.Double)
    private double score;
}

Q.E.D.


窝似嫩叠