elasticsearch是一个高度可扩展的开源全文搜索和分析引擎,可以快速的、近实时地对大数据进行存储、搜索和分析,用来支撑复杂的数据搜索需求和企业级应用,主要用于海量系统日志的数据查询与分析。
{ "name" : "YNFljfo", "cluster_name" : "my-application", "cluster_uuid" : "iuLpWK05SJGSqp5evk7Gpg", "version" : { "number" : "6.2.4", "build_hash" : "ccec39f", "build_date" : "2018-04-12T20:37:28.497551Z", "build_snapshot" : false, "lucene_version" : "7.2.1", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
head插件可以很方便的操作es,head插件与es的关系就像navicat与 MySQL 数据库的关系,不过head插件的界面是通过访问网址浏览的。
npm install -g grunt -cli
。 npm install
http.cors.enabled: true http.cors.allow-origin: "*" 将cluster.name: my-application的注释取消
运行head:
在cmd命令窗口进入head插件解压后的根目录,然后执行 grunt server
,如下图就启动成功。
.png&originHeight=114&originWidth=472&size=5531&status=done&width=472)
localhost:9100
看到如下画面就安装成功 .png&originHeight=349&originWidth=990&size=28420&status=done&width=990)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
spring.data.elasticsearch.cluster-name=my-application spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
@Data @Document(indexName = "person") @AllArgsConstructor @NoArgsConstructor public class Person implements Serializable { @Id private String skuId; private String name; private String address; private Integer age; private String sex; private String city; }
@Data @AllArgsConstructor @NoArgsConstructor public class Response { private Integer code; private String message; private Object data; public static Response successMsg(){ Response response=new Response(); response.setCode(200); response.setMessage("请求成功"); return response; } public static Response failed404(){ Response response=new Response(); response.setCode(404); response.setMessage("请求失败"); return response; } }
创建es的Service
自定义接口实现ElasticsearchRepository,类似于与数据库交互的dao层,提供了对es的基本操作。
public interface PersonRepository extends ElasticsearchRepository<Person,String> { } @NoRepositoryBean public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> { <S extends T> S index(S var1); Iterable<T> search(QueryBuilder var1); Page<T> search(QueryBuilder var1, Pageable var2); Page<T> search(SearchQuery var1); Page<T> searchSimilar(T var1, String[] var2, Pageable var3); void refresh(); Class<T> getEntityClass(); }
public interface PersonService { long count(); Person save(Person person); void delete(Person person); Iterable<Person> getAll(); List<Person> getByName(String name); Page<Person> pageQuery(Integer pageNo, Integer pageSize, String kw); }
@Service public class PersonServiceImpl implements PersonService { @Autowired private PersonRepository personRepository; @Override public long count() { return personRepository.count(); } @Override public Person save(Person person) { return personRepository.save(person); } @Override public void delete(Person person) { personRepository.delete(person); } @Override public Iterable<Person> getAll() { return personRepository.findAll(); } @Override public List<Person> getByName(String name) { List<Person> personList=new ArrayList<>(); MatchQueryBuilder queryBuilder=new MatchQueryBuilder("name",name); Iterable<Person> personIterable=personRepository.search(queryBuilder); personIterable.forEach(personList::add); return personList; } @Override public Page<Person> pageQuery(Integer pageNo, Integer pageSize, String kw) { SearchQuery searchQuery=new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchPhraseQuery("name",kw)).withPageable(PageRequest.of(pageNo,pageSize)).build(); return personRepository.search(searchQuery); } }
Springboot提供了ElasticsearchTemplate,也可实现的ES的数据操作。
@RunWith(SpringRunner.class) @SpringBootTest public class SpringEsApplicationTests { @Autowired public PersonService personService; @Autowired ElasticsearchTemplate elasticsearchTemplate; @Test public void contextLoads() { Person person=new Person("123456","zlc","西湖区",18,"男","hangzhou"); personService.save(person); Person person1=new Person("98712","张三","余杭区",18,"男","hangzhou"); IndexQuery indexQuery=new IndexQueryBuilder().withObject(person1).build(); elasticsearchTemplate.index(indexQuery); } }
@RestController @RequestMapping("/es") public class EsController { @Autowired PersonService personService; /** * 向es添加数据 */ @RequestMapping("/insert") public Object insert(@RequestBody Person person){ JSONObject object=null; try { Person savePerson= personService.save(person); object= (JSONObject) JSON.toJSON(savePerson); } catch (Exception e) { e.printStackTrace(); } return object; } /** * 从es删除数据 * @param person * @return */ @RequestMapping("/delete") public Object delete(@RequestBody Person person){ try { personService.delete(person); } catch (Exception e) { e.printStackTrace(); return Response.failed404(); } return Response.successMsg(); } /** * 查询操作 * @param pageNo * @param pageSize * @param key * @return */ @RequestMapping("/query") public Object query(@RequestParam(value = "pageNo",required = false)Integer pageNo,@RequestParam(value = "pageSize",required = false)Integer pageSize, @RequestParam(value = "key",required = false)String key){ Response response=new Response(); try { Page<Person> personPage= personService.pageQuery(pageNo,pageSize,key); response.setData(personPage); } catch (Exception e) { e.printStackTrace(); return Response.failed404(); } return response; } }
通过Postman测试查询结果如下:
.png&originHeight=858&originWidth=1098&size=38028&status=done&width=1098)
failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available:
初步判定,应该是spring-boot-starter-data-elasticsearch与Elasticsearch 7.x版本不兼容造成的。
通过测试发现,将Elasticsearch降级到6.x版本,即可解决。
2.修改elasticsearch.yml后双击elasticsearch.bat闪退
原因是elasticsearch.yml默认是utf-8编码,所以添加的属性保存时也得是utf-8编码的。
https://github.com/liankaizha...
参考文献:
1. https://cloud.tencent.com/developer/article/1461537