做陌陌网站什么做搜索引擎优化的具体操作
全文搜索引擎
全文搜索引擎是目前广泛应用的主流搜索引擎,也称为全文检索。它的工作原理是计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。这个过程类似于通过字典中的检索字表查字的过程。
Springboot整合Elasticsearch:
步骤:
-
创建一个SpringBoot工程,选择以下模块:Lombok、Web、Spring Data Elasticsearch
-
编辑application.yml文件
spring:elasticsearch:uris: http://localhost:9200
3.基本操作
使用Spring Data Elasticsearch提供的工具类:ElasticsearchRestTemplate
常用注解:
-
@Document 标记实体类为文档对象
-
@Id 标记为文档id
-
@Field 标记为文档字段
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "student") //标记实体类为文档对象,并指定索引名称
public class Student implements Serializable {@Id@Field(type = FieldType.Keyword) //标记为文档id,不分词private Integer id;@Field(type = FieldType.Text) // 标记为文档字段,分词private String name;@Field(type = FieldType.Integer) //标记为文档字段,不分词private Integer age;@Field(type = FieldType.Text)private String sex;@Field(type = FieldType.Text)private String school;
}
编写创建索引Controller:
@RestController
@RequiredArgsConstructor
@RequestMapping("index")
public class IndexController {private final ElasticsearchRestTemplate restTemplate;/*** 创建索引*/@GetMapping("/create")public String create(){IndexOperations indexOperations = restTemplate.indexOps(Student.class);indexOperations.create();return "OK!";}
}
编写存入,并且获取文档Controller
@RestController
@RequiredArgsConstructor
@RequestMapping("doc")
public class DocController {private final ElasticsearchRestTemplate restTemplate;@GetMapping("/create")public String create(){restTemplate.save(new Student(1,"zhangsan",18,"男","Beijing"));return "success……";}@GetMapping("/batch")public String batch(){ArrayList<Student> list = new ArrayList<>();list.add(new Student(2,"Lisi",19,"男","北京大学"));list.add(new Student(3,"Wanger",29,"男","清华大学"));list.add(new Student(4,"Mazi",39,"男","本稻田大学"));list.add(new Student(5,"Waibozi",15,"女","东京大学"));list.add(new Student(6,"Yuzhanao",18,"男","北京大学"));restTemplate.save(list);return "batch====>success……";}@RequestMapping("/{id}")public Student findById(@PathVariable Integer id){Student student = restTemplate.get(id.toString(),Student.class);return student;}@RequestMapping("/all")public List<Student> findAll(){SearchHits<Student> hits = restTemplate.search(Query.findAll(),Student.class);System.out.println(hits.getTotalHits()); //总个数List<Student> list = hits.stream().map(hit -> hit.getContent()).collect(Collectors.toList());return list;}
}