Elasticsearch's Hello (Spring data consolidation)

Source: Internet
Author: User
Tags createindex

1. Writing pom.xml files

<dependencies><dependency><groupid>org.springframework.data</groupid><artifactid >spring-data-elasticsearch</artifactid><version>2.0.4.release</version></dependency ><dependency> <groupId>org.springframework.data</groupId> <artifactId>  Spring-data-elasticsearch</artifactid> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-test</ artifactid> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId> junit</groupid> <artifactId>junit</artifactId> <version>4.12</version> </ dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-log4j12</ Artifactid> <version>1.7.12</version> </dependency></dependencies>

2.applicationcontext.xml file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:elasticsearch= "Http://www.springframework.org/schema/data/elasticsearch" xsi:schemalocation= "http// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp:// Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp:// www.springframework.org/schema/data/elasticsearchhttp://www.springframework.org/schema/data/elasticsearch/ Spring-elasticsearch-1.0.xsd "> <!--Scan DAO package automatically created by <elasticsearch:repositories base-package=" Com.baidu.dao "/> <!--Scan Service Pack--<context:component-scan base-package=" Com.baidu.service "/> <! --Configure Elasticsearch connection--<elasticsearch:transport-client id= "client" cluster-nodes= "localhost:9300"/> <! --SPRing data elasticsearch DAO must rely on elasticsearchtemplate---<bean id= "elasticsearchtemplate" class= "Org.springframe Work.data.elasticsearch.core.ElasticsearchTemplate "> <constructor-arg name=" Client "ref=" client "/> </ Bean> </beans>

  

Demo01.java

Package Com.baidu.test01;import Org.elasticsearch.client.client;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.domain.pagerequest;import Org.springframework.data.domain.sort;import Org.springframework.data.domain.sort.direction;import Org.springframework.data.elasticsearch.core.elasticsearchtemplate;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.springjunit4classrunner;import Com.baidu.domain.article;import Com.baidu.service.ArticleService, @RunWith (springjunit4classrunner.class) @ContextConfiguration (locations = " Classpath:applicationContext.xml ") public class Demo01 {@Autowiredprivate articleservice articleservice;@ Autowiredprivate client client; Based on native api@autowiredprivate Elasticsearchtemplate elasticsearchtemplate, @Test public void CreateIndex () {// Create index Elasticsearchtemplate.createindex (article.class);//Create Map ElasticsearchtempLate.putmapping (Article.class);} @Test//Create document public void Createdoc () {for (int i = 2; i <; i++) {Article ac=new article (); Ac.setid (i); Ac.settitle ("t Estsave "+i"; Ac.setcontent ("Just testing Test" +i);//Document Operation Articleservice.save (AC);}} @Test//Query document public void Searchdoc () {//document operation Articleservice.findall ();} @Test//Query document public void SearchDoc01 () {//Document operation Articleservice.findbyid (1);} @Test//Query document public void SearchDoc02 () {//Fuzzy query Articleservice.findbytitle ("Testsave");} @Test//Query document public void SearchDoc03 () {pagerequest pr=new pagerequest (0, 4,new sort (DIRECTION.DESC, "id"));//paged query sorted in descending order Articleservice.findall (PR);}}

  

Articleservice.java file (interface)
Package Com.baidu.service;import Org.springframework.data.domain.pagerequest;import com.baidu.domain.Article; public interface Articleservice {void Save (article AC); void FindAll (); void FindByID (Integer i); void FindAll (Pagerequest PR); void Findbytitle (String string);}
Articleservice Implementation Class
Package Com.baidu.service.imp;import Org.elasticsearch.index.query.querybuilder;import Org.elasticsearch.index.query.wildcardquerybuilder;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.domain.page;import Org.springframework.data.domain.pagerequest;import Org.springframework.data.domain.sort;import Org.springframework.data.domain.sort.direction;import Org.springframework.data.elasticsearch.core.query.nativesearchquery;import Org.springframework.data.elasticsearch.core.query.searchquery;import Org.springframework.stereotype.Service; Import Com.baidu.dao.articledao;import Com.baidu.domain.article;import com.baidu.service.articleservice;@ Servicepublic class Articleserviceimp implements articleservice{@Autowiredprivate Articledao articledao;public void Save (article AC) {articledao.save (AC);} public void FindAll () {iterable<article> all = Articledao.findall (), for (article Article:all) {System.out.println (article);}} public void FindByID (IntEger i) {Article Article = Articledao.findbyid (i); SYSTEM.OUT.PRINTLN (article);} public void FindAll (pagerequest pr) {page<article> Page = Articledao.findall (pr); System.out.println (Page.getcontent ());} public void Findbytitle (string string) {//needs to be queried based on the content of a word breaker QueryBuilder query= new Wildcardquerybuilder ("title", "*" + string+ "*"); SearchQuery SearchQuery = new Nativesearchquery (query); Pagerequest pr=new pagerequest (0, 4,new Sort (DIRECTION.DESC, "id")); searchquery.setpageable (PR); page<article> page = Articledao.search (searchQuery); System.out.println (Page.getcontent ());}}

  

Articledao.java
Package Com.baidu.dao;import Org.springframework.data.elasticsearch.repository.elasticsearchrepository;import Com.baidu.domain.article;public interface Articledao extends Elasticsearchrepository<article, Integer> { Article FindByID (Integer i);}

  

Elasticsearch's Hello (Spring data consolidation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.