解决Mac下VSCode打开zsh乱码

1.乱码问题

iTerm2终端使用Zsh,并且配置Zsh主题,该主题主题需要安装字体来支持箭头效果,在iTerm2中设置这个字体,但是VSCode里这个箭头还是显示乱码。

阅读更多

Mac使用ssh公钥免密登录服务器

每次登陆服务器都要输入密码,重复无用的操作让人心生厌烦。“懒人是推动社会进步的动力”,我的宗旨就是能自动的就不要手动。
下面就像大家介绍我是如何打造无密码登录服务器:

阅读更多

《Elasticsearch技术解析与实战》Chapter 1.4 Spring Boot整合Elasticsearch

1. spring-boot-starter-data-elasticsearch

1.1 pom.xml和application.yml

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Spring Boot Elasticsearch 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-name: docker-cluster
cluster-nodes: lujiahao.ml:9300

1.2 创建Repository

1
2
3
4
@Repository
public interface PersonEsRepository extends ElasticsearchRepository<Person,Long> {
List<Person> findPersonByName(String name);
}

1.3 文档实体类

1
2
3
4
5
6
7
8
9
10
@Data
@Document(indexName = "person", type = "chinese")
public class Person implements Serializable{
private static final long serialVersionUID = -6804453833406105286L;
@Id
private Long id;
private String name;
private Integer age;
private String address;
}

1.4 增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Service
public class EsStarterService {
@Autowired
private PersonEsRepository repository;
/**
* 增
*/
public Person save(Person person) {
return repository.save(person);
}
/**
* 删
*/
public void delete(Person person) {
repository.delete(person);
}
/**
* 改
*/
public Person update(Person person) {
return repository.save(person);
}
/**
* 查
*/
public Iterable<Person> findAll() {
return repository.findAll();
}
}

1.5 单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsStarterServiceTest {
@Autowired
private EsStarterService esStarterService;
@Test
public void save() {
Person person = new Person();
person.setId(new Random().nextLong());
person.setName("lujiahao");
esStarterService.save(person);
}
@Test
public void delete() {
Person person = new Person();
person.setId(-5264182431891613084L);
person.setName("lujiahao123456");
esStarterService.delete(person);
}
@Test
public void update() {
Person person = new Person();
person.setId(542136934419565287L);
person.setName("lujiahao123456");
esStarterService.update(person);
}
@Test
public void findAll() {
Iterable<Person> all = esStarterService.findAll();
all.forEach(System.out::println);
}
}

2. ElasticsearchTemplate

2.1 pom.xml和application.ym

1
2
3
4
5
6
7
8
9
10
11
12
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-name: docker-cluster
cluster-nodes: lujiahao.ml:9300

2.2 文档实体类

同上

2.3 增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@Service
public class ElasticsearchTemplateService {

@Autowired
public ElasticsearchTemplate elasticsearchTemplate;


private static final String INDEX_NAME = "person";
private static final String TYPE_NAME = "chinese";
/**
* 增
*/
public String save(Person person) {
IndexQuery indexQuery = new IndexQueryBuilder()
.withIndexName(INDEX_NAME)
.withType(TYPE_NAME)
.withId(String.valueOf(person.getId()))
.withObject(person)
.build();
String index = elasticsearchTemplate.index(indexQuery);
System.out.println("xxxxxxxxxxxx " + index);
return index;
}

/**
* 删
*/
public void deleteByName(String name) {
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.matchQuery("name", name));
deleteQuery.setIndex(INDEX_NAME);
deleteQuery.setType(TYPE_NAME);
elasticsearchTemplate.delete(deleteQuery);
}

/**
* 改
*/
public UpdateResponse update(Person person) {
try {
UpdateRequest updateRequest = new UpdateRequest()
.index(INDEX_NAME)
.type(TYPE_NAME)
.id(String.valueOf(person.getId()))
.doc(XContentFactory.jsonBuilder()
.startObject()
.field("name", person.getName())
.endObject());
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withIndexName(INDEX_NAME)
.withType(TYPE_NAME)
.withId(String.valueOf(person.getId()))
.withClass(Person.class)
.withUpdateRequest(updateRequest)
.build();
UpdateResponse update = elasticsearchTemplate.update(updateQuery);
return update;
} catch (Exception e) {
return null;
}
}

/**
* 查
*/
public List<Person> getAll() {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchAllQuery())
.build();
return elasticsearchTemplate.queryForList(searchQuery, Person.class);
}
}

2.4 单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchTemplateServiceTest {
@Autowired
private ElasticsearchTemplateService elasticsearchTemplateService;
@Test
public void save() {
Person person = new Person();
person.setId(new Random().nextLong());
person.setName("haha");
String save = elasticsearchTemplateService.save(person);
System.out.println(save);
}
@Test
public void deleteByName() {
elasticsearchTemplateService.deleteByName("lujiahao");
}
@Test
public void update() {
Person person = new Person();
person.setId(-5264182431891613084L);
person.setName("hahaaaaaaaaa");
UpdateResponse update = elasticsearchTemplateService.update(person);
System.out.println(update);
}
@Test
public void getAll() {
List<Person> all = elasticsearchTemplateService.getAll();
System.out.println(all);
}
}

3. 代码示例

1
https://github.com/lujiahao0708/LearnSeries/tree/master/LearnElasticSerach

Tips

本文同步发表在公众号,欢迎大家关注!😁
后续笔记欢迎关注获取第一时间更新!

《Elasticsearch技术解析与实战》Chapter 1.3 Elasticsearch增删改查

1. 新增文档,建立索引

语法格式:

PUT /index/type/id
{
  "json数据"
}

输入:

PUT /person/chinese/1
{
  "id":12345,
  "name":"lujiahao",
  "age":18
}

输出:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索。

2. 检索文档

格式:

GET /index/type/id

输入:

GET /person/chinese/1

输出:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "id": 12345,
    "name": "lujiahao",
    "age": 18
  }
}

3. 更新文档

3.1 替换方式

格式:

PUT /index/type/id
{
    "json数据"
}

输入:

PUT /person/chinese/1
{
  "name":"lujiahao123"
}

输出:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

查询:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "name": "lujiahao123"
  }
}

替换方式更新文档时,必须带上所有的field,才能去进行信息的修改;如果缺少field就会丢失部分数据。其原理时替换,因此需要全部字段。不推荐此种方式更新文档。

3.1 更新方式

格式:

POST /index/type/id/_update
{
    "doc":{
        "json数据"
    }
}

输入:

POST /person/chinese/1/_update
{
  "doc":{
    "name":"lujiahao10010"
  }
}

输出:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 4,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": false
}

再次查询:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 6,
  "found": true,
  "_source": {
    "id": 12345,
    "name": "lujiahao10010",
    "age": 18
  }
}

4. 删除文档

格式:

DELETE /index/type/id/_update
{
    "doc":{
        "json数据"
    }
}

输入:

DELETE /person/chinese/1

输出:

{
  "found": true,
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "_version": 7,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

再次查询:

{
  "_index": "person",
  "_type": "chinese",
  "_id": "1",
  "found": false
}

5. 小结

本文所有操作都是在kibana的Dev tools中进行的,相较于Elasticsearch-Heade插件,kibana中更加方便与美观(个人观点),推荐大家使用。

Tips

本文同步发表在公众号,欢迎大家关注!😁
后续笔记欢迎关注获取第一时间更新!