Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f9ca4f

Browse files
committed
Initial commit
1 parent 7318ccb commit 8f9ca4f

File tree

11 files changed

+701
-0
lines changed

11 files changed

+701
-0
lines changed

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
*.class
2+
.classpath
3+
.project
4+
.settings
5+
target
26

37
# Mobile Tools for Java (J2ME)
48
.mtj.tmp/

‎pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>net.aimeizi</groupId>
6+
<artifactId>spring-data-elasticsearch-example</artifactId>
7+
<packaging>war</packaging>
8+
<version>1.0</version>
9+
10+
<name>spring-data-elasticsearch-example</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<dependencies>
14+
15+
<dependency>
16+
<groupId>javax.servlet</groupId>
17+
<artifactId>servlet-api</artifactId>
18+
<version>2.5</version>
19+
<scope>provided</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>javax.servlet.jsp</groupId>
23+
<artifactId>jsp-api</artifactId>
24+
<version>2.1</version>
25+
<scope>provided</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.data</groupId>
29+
<artifactId>spring-data-elasticsearch</artifactId>
30+
<version>1.2.0.BUILD-SNAPSHOT</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.11</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-test</artifactId>
40+
<version>4.0.8.RELEASE</version>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>2.0.2</version>
51+
<configuration>
52+
<source>1.7</source>
53+
<target>1.7</target>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.tomcat.maven</groupId>
58+
<artifactId>tomcat6-maven-plugin</artifactId>
59+
<version>2.2</version>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.tomcat.maven</groupId>
63+
<artifactId>tomcat7-maven-plugin</artifactId>
64+
<version>2.2</version>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.aimeizi.spring.data.elasticsearch.example;
2+
3+
public class Author {
4+
5+
private String id;
6+
private String name;
7+
8+
public Author() {
9+
}
10+
11+
public Author(String id, String name) {
12+
super();
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public void setId(String id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Author [id=" + id + ", name=" + name + "]";
36+
}
37+
38+
39+
40+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package net.aimeizi.spring.data.elasticsearch.example;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.elasticsearch.annotations.Document;
5+
import org.springframework.data.elasticsearch.annotations.Field;
6+
import org.springframework.data.elasticsearch.annotations.FieldIndex;
7+
import org.springframework.data.elasticsearch.annotations.FieldType;
8+
9+
10+
@Document(indexName= "book", type= "book", indexStoreType = /*"fs"*/"memory", shards = 1, replicas = 0, refreshInterval = "-1")
11+
public class Book{
12+
13+
@Id
14+
private Integer id;
15+
private String name;
16+
private String desc;
17+
@Field(index=FieldIndex.not_analyzed)
18+
private String url;
19+
@Field(index=FieldIndex.not_analyzed)
20+
private String pubdate;
21+
private String pubinfo;
22+
@Field(type=FieldType.Nested)
23+
private Author author;
24+
@Field(type=FieldType.Float,index=FieldIndex.not_analyzed)
25+
private Double price;
26+
27+
public Book() {
28+
29+
}
30+
31+
public Book(Integer id, String name, String desc, String url, String pubdate,
32+
String pubinfo, Author author, Double price) {
33+
super();
34+
this.id = id;
35+
this.name = name;
36+
this.desc = desc;
37+
this.url = url;
38+
this.pubdate = pubdate;
39+
this.pubinfo = pubinfo;
40+
this.author = author;
41+
this.price = price;
42+
}
43+
44+
45+
46+
public Integer getId() {
47+
return id;
48+
}
49+
public void setId(Integer id) {
50+
this.id = id;
51+
}
52+
public String getName() {
53+
return name;
54+
}
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public String getDesc() {
60+
return desc;
61+
}
62+
63+
public void setDesc(String desc) {
64+
this.desc = desc;
65+
}
66+
67+
public String getUrl() {
68+
return url;
69+
}
70+
public void setUrl(String url) {
71+
this.url = url;
72+
}
73+
public String getPubdate() {
74+
return pubdate;
75+
}
76+
public void setPubdate(String pubdate) {
77+
this.pubdate = pubdate;
78+
}
79+
80+
public String getPubinfo() {
81+
return pubinfo;
82+
}
83+
84+
public void setPubinfo(String pubinfo) {
85+
this.pubinfo = pubinfo;
86+
}
87+
88+
public Author getAuthor() {
89+
return author;
90+
}
91+
public void setAuthor(Author author) {
92+
this.author = author;
93+
}
94+
95+
public Double getPrice() {
96+
return price;
97+
}
98+
99+
public void setPrice(Double price) {
100+
this.price = price;
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return "Book [id=" + id + ", name=" + name + ", desc=" + desc
106+
+ ", url=" + url + ", pubdate=" + pubdate + ", pubinfo="
107+
+ pubinfo + ", author=" + author + ", price=" + price + "]";
108+
}
109+
110+
111+
112+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.aimeizi.spring.data.elasticsearch.example.repository;
2+
3+
import java.util.List;
4+
5+
import net.aimeizi.spring.data.elasticsearch.example.Book;
6+
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.data.elasticsearch.annotations.Query;
10+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
11+
import org.springframework.stereotype.Repository;
12+
13+
@Repository
14+
public interface BookRepository extends ElasticsearchRepository<Book, Integer>{
15+
16+
List<Book> findByNameAndPrice(String name, Float price);
17+
18+
List<Book> findByNameOrPrice(String name, Float price);
19+
20+
Page<Book> findByName(String name,Pageable page);
21+
22+
Page<Book> findByNameNot(String name,Pageable page);
23+
24+
Page<Book> findByPriceBetween(Float price,Pageable page);
25+
26+
Page<Book> findByNameLike(String name,Pageable page);
27+
28+
@Query("{\"bool\" : {\"must\" : {\"term\" : {\"price\" : \"?0\"}}}}")
29+
Page<Book> findByPrice(Float price, Pageable pageable);
30+
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.aimeizi.spring.data.elasticsearch.example.service;
2+
3+
import java.util.List;
4+
5+
import net.aimeizi.spring.data.elasticsearch.example.Book;
6+
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public interface BookService {
13+
14+
List<Book> findByNameAndPrice(String name, Float price);
15+
16+
List<Book> findByNameOrPrice(String name, Float price);
17+
18+
Page<Book> findByName(String name,Pageable page);
19+
20+
Page<Book> findByNameNot(String name,Pageable page);
21+
22+
Page<Book> findByPriceBetween(Float price,Pageable page);
23+
24+
Page<Book> findByNameLike(String name,Pageable page);
25+
26+
Page<Book> findByPrice(Float price, Pageable pageable);
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.aimeizi.spring.data.elasticsearch.example.service.impl;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.Pageable;
8+
9+
import net.aimeizi.spring.data.elasticsearch.example.Book;
10+
import net.aimeizi.spring.data.elasticsearch.example.repository.BookRepository;
11+
import net.aimeizi.spring.data.elasticsearch.example.service.BookService;
12+
13+
public class BookServiceImpl implements BookService{
14+
15+
@Autowired
16+
private BookRepository bookRepository;
17+
18+
@Override
19+
public List<Book> findByNameAndPrice(String name, Float price) {
20+
return bookRepository.findByNameAndPrice(name, price);
21+
}
22+
23+
@Override
24+
public List<Book> findByNameOrPrice(String name, Float price) {
25+
return bookRepository.findByNameOrPrice(name, price);
26+
}
27+
28+
@Override
29+
public Page<Book> findByName(String name, Pageable page) {
30+
return bookRepository.findByName(name, page);
31+
}
32+
33+
@Override
34+
public Page<Book> findByNameNot(String name, Pageable page) {
35+
return bookRepository.findByNameNot(name, page);
36+
}
37+
38+
@Override
39+
public Page<Book> findByPriceBetween(Float price, Pageable page) {
40+
return bookRepository.findByPriceBetween(price, page);
41+
}
42+
43+
@Override
44+
public Page<Book> findByNameLike(String name, Pageable page) {
45+
return bookRepository.findByNameLike(name, page);
46+
}
47+
48+
@Override
49+
public Page<Book> findByPrice(Float price, Pageable pageable) {
50+
return bookRepository.findByPrice(price, pageable);
51+
}
52+
53+
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<elasticsearch:transport-client id="client"
9+
cluster-nodes="127.0.0.1:9300" cluster-name="elasticsearch" />
10+
11+
<bean name="elasticsearchTemplate"
12+
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
13+
<constructor-arg name="client" ref="client" />
14+
</bean>
15+
16+
<elasticsearch:repositories
17+
base-package="net.aimeizi.spring.data.elasticsearch.example.repository"/>
18+
</beans>

‎src/main/resources/logback.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="error">
11+
<appender-ref ref="console"/>
12+
</root>
13+
14+
</configuration>

‎src/main/webapp/WEB-INF/web.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
3+
<display-name>spring-data-elasticsearch-example</display-name>
4+
<session-config>
5+
<session-timeout>
6+
30
7+
</session-timeout>
8+
</session-config>
9+
<welcome-file-list>
10+
<welcome-file>index.jsp</welcome-file>
11+
</welcome-file-list>
12+
</web-app>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /