Add documentation and tests

This commit is contained in:
Rizwan Idrees 2013-01-25 17:37:02 +00:00
parent 75189a656d
commit e7b097caee
12 changed files with 2000 additions and 723 deletions

263
pom.xml
View File

@ -6,7 +6,7 @@
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<inceptionYear>2012</inceptionYear>
<url>http://github.com/biomedcentral/spring-data-elasticsearch</url>
<url>https://github.com/BioMedCentralLtd/spring-data-elasticsearch</url>
<name>Spring Data Elasticsearch</name>
<description>Spring Data module providing support for Elasticsearch repositories</description>
<organization>
@ -56,10 +56,6 @@
</license>
</licenses>
<!--issueManagement>
<system>JIRA</system>
<url>https://jira.biomedcentral.com/browse/DATAELASTICSEARCH</url>
</issueManagement-->
<scm>
<url>https://github.com/biomedcentral/spring-data-elasticsearch</url>
@ -68,10 +64,10 @@
</scm>
<!--ciManagement>
<system>Bamboo</system>
<url>http://build.springsource.org/browse/DATAELASTICSEARCH</url>
</ciManagement-->
<ciManagement>
<system>Travis</system>
<url>https://travis-ci.org/BioMedCentralLtd/spring-data-elasticsearch</url>
</ciManagement>
<dependencies>
<!-- SPRING -->
@ -312,7 +308,7 @@
</plugins>
</build>
<!--profiles>
<profiles>
<profile>
<id>jar-with-dependencies</id>
<build>
@ -326,139 +322,124 @@
</plugin>
</plugins>
</build>
</profile-->
<!--<profile>-->
<!--<id>distribute</id>-->
<!--<build>-->
<!--<plugins>-->
</profile>
<profile>
<id>distribute</id>
<build>
<plugins>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-javadoc-plugin</artifactId>-->
<!--<version>2.8</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<goals>-->
<!--<goal>javadoc</goal>-->
<!--</goals>-->
<!--<phase>package</phase>-->
<!--</execution>-->
<!--</executions>-->
<!--<configuration>-->
<!--<breakiterator>true</breakiterator>-->
<!--<header>Spring Data Elasticsearch</header>-->
<!--<source>1.5</source>-->
<!--<quiet>true</quiet>-->
<!--&lt;!&ndash; copies doc-files subdirectory which contains image resources &ndash;&gt;-->
<!--<docfilessubdirs>true</docfilessubdirs>-->
<!--<links>-->
<!--<link>http://static.springframework.org/spring/docs/3.1.x/javadoc-api</link>-->
<!--<link>http://download.oracle.com/javase/1.5.0/docs/api</link>-->
<!--<link>http://static.springsource.org/spring-data/data-commons/docs/${spring.data-commons.version}/api</link>-->
<!--</links>-->
<!--</configuration>-->
<!--</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<goals>
<goal>javadoc</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<breakiterator>true</breakiterator>
<header>Spring Data Elasticsearch</header>
<source>${project.version}</source>
<quiet>true</quiet>
<!-- copies doc-files subdirectory which contains image resources -->
<docfilessubdirs>true</docfilessubdirs>
<links>
<link>http://static.springframework.org/spring/docs/3.1.x/javadoc-api</link>
<link>http://download.oracle.com/javase/1.5.0/docs/api</link>
<link>http://static.springsource.org/spring-data/data-commons/docs/${spring.data-commons.version}/api</link>
</links>
</configuration>
</plugin>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
<version>2.0.7</version>
<executions>
<execution>
<goals>
<goal>generate-html</goal>
<goal>generate-pdf</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.docbook</groupId>
<artifactId>docbook-xml</artifactId>
<version>4.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<configuration>
<includes>index.xml</includes>
<xincludeSupported>true</xincludeSupported>
<foCustomization>${project.basedir}/src/docbkx/resources/xsl/fopdf.xsl</foCustomization>
<htmlStylesheet>css/html.css</htmlStylesheet>
<chunkedOutput>false</chunkedOutput>
<htmlCustomization>${project.basedir}/src/docbkx/resources/xsl/html.xsl</htmlCustomization>
<useExtensions>1</useExtensions>
<highlightSource>1</highlightSource>
<entities>
<entity>
<name>version</name>
<value>${project.version}</value>
</entity>
</entities>
<postProcess>
<copy todir="${project.basedir}/target/site/reference">
<fileset dir="${project.basedir}/target/docbkx">
<include name="**/*.html" />
<include name="**/*.pdf" />
</fileset>
</copy>
<copy todir="${project.basedir}/target/site/reference/html">
<fileset dir="${project.basedir}/src/docbkx/resources">
<include name="**/*.css" />
<include name="**/*.png" />
<include name="**/*.gif" />
<include name="**/*.jpg" />
</fileset>
</copy>
<move file="${project.basedir}/target/site/reference/pdf/index.pdf" tofile="${project.basedir}/target/site/reference/pdf/${project.artifactId}-reference.pdf" failonerror="false" />
</postProcess>
</configuration>
</plugin>
<!--<plugin>-->
<!--<groupId>com.agilejava.docbkx</groupId>-->
<!--<artifactId>docbkx-maven-plugin</artifactId>-->
<!--<version>2.0.7</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<goals>-->
<!--<goal>generate-html</goal>-->
<!--<goal>generate-pdf</goal>-->
<!--</goals>-->
<!--<phase>package</phase>-->
<!--</execution>-->
<!--</executions>-->
<!--<dependencies>-->
<!--<dependency>-->
<!--<groupId>org.docbook</groupId>-->
<!--<artifactId>docbook-xml</artifactId>-->
<!--<version>4.4</version>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<!--</dependencies>-->
<!--<configuration>-->
<!--<includes>index.xml</includes>-->
<!--<xincludeSupported>true</xincludeSupported>-->
<!--<foCustomization>${project.basedir}/src/docbkx/resources/xsl/fopdf.xsl</foCustomization>-->
<!--<htmlStylesheet>${project.basedir}/src/docbkx/resources/css/html.css</htmlStylesheet>-->
<!--<chunkedOutput>false</chunkedOutput>-->
<!--<htmlCustomization>${project.basedir}/src/docbkx/resources/xsl/html.xsl</htmlCustomization>-->
<!--<highlightSource>1</highlightSource>-->
<!--<entities>-->
<!--<entity>-->
<!--<name>version</name>-->
<!--<value>${project.version}</value>-->
<!--</entity>-->
<!--</entities>-->
<!--<postProcess>-->
<!--<copy todir="${project.basedir}/target/site/reference">-->
<!--<fileset dir="${project.basedir}/target/docbkx">-->
<!--<include name="**/*.html" />-->
<!--<include name="**/*.pdf" />-->
<!--</fileset>-->
<!--</copy>-->
<!--<copy todir="${project.basedir}/target/site/reference/html">-->
<!--<fileset dir="${project.basedir}/src/docbkx/resources">-->
<!--<include name="**/*.css" />-->
<!--<include name="**/*.png" />-->
<!--<include name="**/*.gif" />-->
<!--<include name="**/*.jpg" />-->
<!--</fileset>-->
<!--</copy>-->
<!--<move file="${project.basedir}/target/site/reference/pdf/index.pdf" tofile="${project.basedir}/target/site/reference/pdf/${project.artifactId}-reference.pdf" failonerror="false" />-->
<!--</postProcess>-->
<!--</configuration>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<artifactId>maven-assembly-plugin</artifactId>-->
<!--<version>2.2.1</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>distribution</id>-->
<!--<goals>-->
<!--<goal>single</goal>-->
<!--</goals>-->
<!--<phase>package</phase>-->
<!--<configuration>-->
<!--<descriptors>-->
<!--<descriptor>${project.basedir}/src/main/assembly/distribution.xml</descriptor>-->
<!--</descriptors>-->
<!--<appendAssemblyId>false</appendAssemblyId>-->
<!--</configuration>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!--</plugins>-->
<!--</build>-->
<!--</profile>-->
<!--</profiles>-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>distribution</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/distribution.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!--<developers>-->
<!--<developer>-->
<!--<id>christophstrobl</id>-->
<!--<name>Christoph Strobl</name>-->
<!--<timezone>+1</timezone>-->
<!--</developer>-->
<!--<developer>-->
<!--<id>ogierke</id>-->
<!--<name>Oliver Gierke</name>-->
<!--<timezone>+1</timezone>-->
<!--</developer>-->
<!--<developer>-->
<!--<id>mpollack</id>-->
<!--<name>Mark Pollack</name>-->
<!--<timezone>-5</timezone>-->
<!--</developer>-->
<!--</developers>-->
<developers>
<developer>
<id>biomedcentral</id>
<name>BioMed Central Development Team</name>
<timezone>+0</timezone>
</developer>
</developers>
<!--<repositories>-->
<!--<repository>-->
<!--<id>spring-releases</id>-->
<!--<url>http://repo.springsource.org/libs-release</url>-->
<!--</repository>-->
<!--</repositories>-->
</project>

View File

@ -3,25 +3,17 @@
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<book xmlns:xi="http://www.w3.org/2001/XInclude">
<bookinfo>
<title>Spring Data Solr</title>
<title>Spring Data Elasticsearch</title>
<authorgroup>
<author>
<firstname>Christoph</firstname>
<surname>Strobl</surname>
<firstname>Rizwan</firstname>
<surname>Idrees</surname>
</author>
<author>
<firstname>Oliver</firstname>
<surname>Gierke</surname>
<firstname>Mohsin</firstname>
<surname>Husen</surname>
</author>
<author>
<firstname>Mark</firstname>
<surname>Pollack</surname>
</author>
<author>
<firstname>Thomas</firstname>
<surname>Risberg</surname>
</author>
</authorgroup>
</authorgroup>
<legalnotice>
<para>
Copies of this document may be made for your own use and for
@ -35,7 +27,7 @@
</legalnotice>
<copyright>
<year>2012</year>
<year>2013</year>
<holder>The original author(s)</holder>
</copyright>
</bookinfo>
@ -51,7 +43,7 @@
<xi:fallback href="../../../spring-data-commons/src/docbkx/repositories.xml" />
</xi:include>
<xi:include href="reference/data-solr.xml" />
<xi:include href="reference/data-elasticsearch.xml" />
<xi:include href="reference/misc.xml" />
</part>

View File

@ -3,13 +3,13 @@
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<preface id="preface">
<title>Preface</title>
<para>The Spring Data Solr project applies core Spring concepts to
<para>The Spring Data Elasticsearch project applies core Spring concepts to
the
development of solutions using the Apache Solr Search Engine.
We
provide a "template" as a high-level abstraction for storing and
querying documents. You will notice similarities to the mongodb
support in the Spring Framework.
development of solutions using the Elasticsearch Search Engine.
We have povided a "template" as a high-level abstraction for
storing,querying,sorting and faceting documents. You will notice similarities
to the Spring data solr and
mongodb support in the Spring Framework.
</para>
<section id="project">
<title>Project Metadata</title>
@ -17,56 +17,18 @@
<listitem>
<para>
Version Control -
<ulink url="git://github.com/SpringSource/spring-data-solr.git">git://github.com/SpringSource/spring-data-solr.git
<ulink url="git://github.com/BioMedCentralLtd/spring-data-elasticsearch.git">git://github.com/BioMedCentralLtd/spring-data-elasticsearch.git
</ulink>
</para>
</listitem>
<listitem>
<para>
Bugtacker -
<ulink url="https://jira.springsource.org/browse/DATASOLR">https://jira.springsource.org/browse/DATASOLR
</ulink>
</para>
</listitem>
<listitem>
<para>
Release repository -
<ulink url="http://repo.springsource.org/libs-release">http://repo.springsource.org/libs-release</ulink>
</para>
</listitem>
<listitem>
<para>
Milestone repository -
<ulink url="http://repo.springsource.org/libs-milestone">http://repo.springsource.org/libs-milestone
</ulink>
</para>
</listitem>
<listitem>
<para>
Snapshot repository -
<ulink url="http://repo.springsource.org/libs-snapshot">http://repo.springsource.org/libs-snapshot</ulink>
</para>
</listitem>
</itemizedlist>
</section>
<section id="requirements">
<title>Requirements</title>
<para>
Requires
<ulink url="http://lucene.apache.org/solr/">Apache Solr</ulink>
3.5 and above or optional dependency
<programlisting language="xml">&lt;dependency&gt;
&lt;groupId&gt;org.apache.solr&lt;/groupId&gt;
&lt;artifactId&gt;solr-core&lt;/artifactId&gt;
&lt;version&gt;${solr.version}&lt;/version&gt;
&lt;/dependency&gt;</programlisting>
<note>
If you tend to use the Embedded Version of Solr
Server 4.x you will also have to add a version of servlet-api and
check your &lt;lockType&gt; as well as &lt;unlockOnStartup&gt;
settings.
</note>
</para>
<ulink url="http://www.elasticsearch.org/download/">Elasticsearch</ulink>
0.20.2 and above or optional dependency
</para>
</section>
</preface>

View File

@ -0,0 +1,599 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<chapter id="solr.repositories">
<title>Solr Repositories</title>
<abstract>
<para>This chapter includes details of the Solr repository
implementation.
</para>
</abstract>
<section id="elasticsearch.introduction">
<title>Introduction</title>
<section id="elasticsearch.namespace">
<title>Spring Namespace</title>
<para>
The Spring Data Elasticsearch module contains a custom namespace allowing
definition of repository beans as well as elements for instantiating
a
<classname>ElasticsearchServer</classname>
.
</para>
<para>
Using the
<code>repositories</code>
element looks up Spring Data repositories as described in
<xref linkend="repositories.create-instances" />
.
</para>
<example>
<title>Setting up Elasticsearch repositories using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"&gt;
&lt;elasticsearch:repositories base-package="com.acme.repositories" /&gt;
&lt;/beans&gt;</programlisting>
</example>
<para>
Using the
<code>Transport Client</code>
or
<code>Node Client</code>
element registers an instance of
<code>Elasticsearch Server</code>
in the context.
<example>
<title>Transport Client using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"&gt;
&lt;elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" /&gt;
&lt;/beans&gt; </programlisting>
</example>
<example>
<title>Node Client using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"&gt;
&lt;elasticsearch:node-client id="client" local="true"" /&gt;
&lt;/beans&gt; </programlisting>
</example>
</para>
</section>
<section id="elasticsearch.annotation">
<title>Annotation based configuration</title>
<para>The Spring Data Elasticsearch repositories support cannot only be
activated through an XML namespace but also using an annotation
through JavaConfig.
</para>
<example>
<title>Spring Data Elasticsearch repositories using JavaConfig</title>
<programlisting language="java">@Configuration
@EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
static class Config {
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
}
}</programlisting>
<para>
The configuration above sets up an
<classname>Embedded Elasticsearch Server</classname>
which is used by the
<classname>ElasticsearchTemplate</classname>
. Spring Data Elasticsearch Repositories are activated using the
<interfacename>@EnableElasticsearchRepositories</interfacename>
annotation, which
essentially carries the same attributes as the XML
namespace does. If no
base package is configured, it will use the
one
the configuration class
resides in.
</para>
</example>
</section>
<!--<section id="elasticsearch.cdi">-->
<!--<title>Elasticsearch Repositores using CDI</title>-->
<!--<para>The Spring Data Elasticsearch repositories can also be set up using CDI-->
<!--functionality.-->
<!--</para>-->
<!--<example>-->
<!--<title>Spring Data Elasticsearch repositories using JavaConfig</title>-->
<!--<programlisting language="java">class SolrTemplateProducer {-->
<!--@Produces-->
<!--@ApplicationScoped-->
<!--public SolrOperations createSolrTemplate() {-->
<!--return new SolrTemplate(new EmbeddedSolrServerFactory("classpath:com/acme/solr"));-->
<!--}-->
<!--}-->
<!--class ProductService {-->
<!--private ProductRepository repository;-->
<!--public Page&lt;Product&gt; findAvailableProductsByName(String name, Pageable pageable) {-->
<!--return repository.findByAvailableTrueAndNameStartingWith(name, pageable);-->
<!--}-->
<!--@Inject-->
<!--public void setRepository(ProductRepository repository) {-->
<!--this.repository = repository;-->
<!--}-->
<!--}</programlisting>-->
<!--</example>-->
<!--</section>-->
</section>
<section id="elasticsearch.query-methods">
<title>Query methods</title>
<section id="elasticsearch.query-methods.finders">
<title>Query lookup strategies</title>
<para>
The Elasticsearch module supports all basic query building feature as String,Abstract,Criteria or
have
it being derived from the method name.
<note>
There is no QueryDSL Support present at this time.
</note>
</para>
<simplesect>
<title>Declared queries</title>
<para>
Deriving the query from the method name is not always sufficient
and/or may result in unreadable method names. In this case one
might make either use of Elasticsearch named queries (see
<xref linkend="elasticsearch.query-methods.named-queries" />
) or use the
<interfacename>@Query</interfacename>
annotation (see
<xref linkend="elasticsearch.query-methods.at-query" />
).
</para>
</simplesect>
</section>
<section id="elasticsearch.query-methods.criterions">
<title>Query creation</title>
<para>
Generally the query creation mechanism for Elasticsearch works as described
in
<xref linkend="repositories.query-methods" />
. Here's a short example
of what a Elasticsearch query method translates into:
<example>
<title>Query creation from method names</title>
<programlisting language="java">public interface BookRepository extends Repository&lt;Book, String&gt; {
List&lt;Book&gt; findByNameAndPrice(String name, Integer price);
}</programlisting>
<para>
The method name above will be translated into the following
Elasticsearch json query
</para>
<programlisting>{
"bool" : {
"must" : [ {
"field" : {
"type" : "test"
}
}, {
"field" : {
"message" : "some message"
}
} ]
}
}</programlisting>
</example>
</para>
<para>
A list of supported keywords for Elasticsearch is shown below.
<table>
<title>Supported keywords inside method names</title>
<tgroup cols="3">
<colspec colwidth="1*" />
<colspec colwidth="2*" />
<colspec colwidth="3*" />
<thead>
<row>
<entry>Keyword</entry>
<entry>Sample</entry>
<entry>Elasticsearch Query String</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<code>And</code>
</entry>
<entry>
<code>findByNameAndPrice</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : [ {
"field" : {
"name" : "?"
}
}, {
"field" : {
"price" : "?"
}
} ]
}
}</code>
</entry>
</row>
<row>
<entry>
<code>Or</code>
</entry>
<entry>
<code>findByNameOrPrice</code>
</entry>
<entry>
<code>{
"bool" : {
"should" : [ {
"field" : {
"name" : "?"
}
}, {
"field" : {
"price" : "?"
}
} ]
}
}</code>
</entry>
</row>
<row>
<entry>
<code>Is</code>
</entry>
<entry>
<code>findByName</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : {
"field" : {
"name" : "?"
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>Not</code>
</entry>
<entry>
<code>findByNameNot</code>
</entry>
<entry>
<code>{
"bool" : {
"must_not" : {
"field" : {
"name" : "?"
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>Between</code>
</entry>
<entry>
<code>findByPriceBetween</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : {
"range" : {
"price" : {
"from" : ?,
"to" : ?,
"include_lower" : true,
"include_upper" : true
}
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>LessThanEqual</code>
</entry>
<entry>
<code>findByPriceLessThan</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : {
"range" : {
"price" : {
"from" : null,
"to" : ?,
"include_lower" : true,
"include_upper" : true
}
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>GreaterThanEqual</code>
</entry>
<entry>
<code>findByPriceGreaterThan</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : {
"range" : {
"rate" : {
"from" : ?,
"to" : null,
"include_lower" : true,
"include_upper" : true
}
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>Before</code>
</entry>
<entry>
<code>findByPriceBefore</code>
</entry>
<entry>
<code>{
"bool" : {
"must" : {
"range" : {
"price" : {
"from" : null,
"to" : ?,
"include_lower" : true,
"include_upper" : true
}
}
}
}
}</code>
</entry>
</row>
<row>
<entry>
<code>After</code>
</entry>
<entry>
<code>findByLastModifiedAfter</code>
</entry>
<entry>
<code>q=last_modified:[?0 TO *]</code>
</entry>
</row>
<row>
<entry>
<code>Like</code>
</entry>
<entry>
<code>findByNameLike</code>
</entry>
<entry>
<code>q=name:?0*</code>
</entry>
</row>
<row>
<entry>
<code>StartingWith</code>
</entry>
<entry>
<code>findByNameStartingWith</code>
</entry>
<entry>
<code>q=name:?0*</code>
</entry>
</row>
<row>
<entry>
<code>EndingWith</code>
</entry>
<entry>
<code>findByNameEndingWith</code>
</entry>
<entry>
<code>q=name:*?0</code>
</entry>
</row>
<row>
<entry>
<code>Containing</code>
</entry>
<entry>
<code>findByNameContaining</code>
</entry>
<entry>
<code>q=name:*?0*</code>
</entry>
</row>
<row>
<entry>
<code>In</code>
</entry>
<entry>
<code>findByNameIn(Collection&lt;String&gt;
names)
</code>
</entry>
<entry>
<code>q=name:(?0... )</code>
</entry>
</row>
<row>
<entry>
<code>NotIn</code>
</entry>
<entry>
<code>findByNameNotIn(Collection&lt;String&gt;
names)
</code>
</entry>
<entry>
<code>q=-name:(?0... )</code>
</entry>
</row>
<row>
<entry>
<code>Near</code>
</entry>
<entry>
<code>findByStoreNear</code>
</entry>
<entry>
<code>q={!geofilt pt=?0.latitude,?0.longitude sfield=store
d=?1}
</code>
</entry>
</row>
<row>
<entry>
<code>True</code>
</entry>
<entry>
<code>findByAvailableTrue</code>
</entry>
<entry>
<code>q=inStock:true</code>
</entry>
</row>
<row>
<entry>
<code>False</code>
</entry>
<entry>
<code>findByAvailableFalse</code>
</entry>
<entry>
<code>q=inStock:false</code>
</entry>
</row>
<row>
<entry>
<code>OrderBy</code>
</entry>
<entry>
<code>findByAvailableTrueOrderByNameDesc</code>
</entry>
<entry>
<code>q=inStock:true&amp;sort=name desc</code>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section id="solr.query-methods.at-query">
<title>Using @Query Annotation</title>
<para>
Using named queries (
<xref linkend="solr.query-methods.named-queries" />
) to declare queries for entities is a valid
approach and works fine
for a small number of queries. As the
queries themselves are tied to
the Java method that executes them,
you actually can bind them
directly using the Spring Data Solr
<code>@Query</code>
annotation.
</para>
<example>
<title>
Declare query at the method using the
<interfacename>@Query</interfacename>
annotation.
</title>
<programlisting language="java">public interface ProductRepository extends SolrRepository&lt;Product, String&gt; {
@Query("inStock:?0")
List&lt;Product&gt; findByAvailable(Boolean available);
}</programlisting>
</example>
</section>
<section id="solr.query-methods.named-queries">
<title>Using NamedQueries</title>
<para>
Named queries can be kept in a properties file and wired to the
accroding method. Please mind the naming convention described in
<xref linkend="repositories.query-methods.query-lookup-strategies" />
or use
<interfacename>@Query</interfacename>
.
</para>
<example>
<title>
Declare named query in properites file
</title>
<programlisting>Product.findByNamedQuery=popularity:?0
Product.findByName=name:?0</programlisting>
<programlisting language="java">public interface ProductRepository extends SolrCrudRepository&lt;Product, String&gt; {
List&lt;Product&gt; findByNamedQuery(Integer popularity);
@Query(name = "Product.findByName")
List&lt;Product&gt; findByAnnotatedNamedQuery(String name);
}</programlisting>
</example>
</section>
</section>
</chapter>

View File

@ -1,501 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<chapter id="solr.repositories">
<title>Solr Repositories</title>
<abstract>
<para>This chapter includes details of the Solr repository
implementation.
</para>
</abstract>
<section id="solr.introduction">
<title>Introduction</title>
<section id="solr.namespace">
<title>Spring Namespace</title>
<para>
The Spring Data Solr module contains a custom namespace allowing
definition of repository beans as well as elements for instantiating
a
<classname>SolrServer</classname>
.
</para>
<para>
Using the
<code>repositories</code>
element looks up Spring Data repositories as described in
<xref linkend="repositories.create-instances" />
.
</para>
<example>
<title>Setting up Solr repositories using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd"&gt;
&lt;solr:repositories base-package="com.acme.repositories" /&gt;
&lt;/beans&gt;</programlisting>
</example>
<para>
Using the
<code>solr-server</code>
or
<code>embedded-solr-server</code>
element registers an instance of
<code>SolrServer</code>
in the context.
<example>
<title>HttpSolrServer using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd"&gt;
&lt;solr:solr-server id="solrServer" url="http://locahost:8983/solr" /&gt;
&lt;/beans&gt; </programlisting>
</example>
<example>
<title>EmbeddedSolrServer using Namespace</title>
<programlisting language="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd"&gt;
&lt;solr:embedded-solr-server id="solrServer" solrHome="classpath:com/acme/solr" /&gt;
&lt;/beans&gt; </programlisting>
</example>
</para>
</section>
<section id="solr.annotation">
<title>Annotation based configuration</title>
<para>The Spring Data Solr repositories support cannot only be
activated through an XML namespace but also using an annotation
through JavaConfig.
</para>
<example>
<title>Spring Data Solr repositories using JavaConfig</title>
<programlisting language="java">@Configuration
@EnableSolrRepositories
class ApplicationConfig {
@Bean
public SolrServer solrServer() {
EmbeddedSolrServerFactory factory = new EmbeddedSolrServerFactory("classpath:com/acme/solr");
return factory.getSolrServer();
}
@Bean
public SolrOperations solrTemplate() {
return new SolrTemplate(solrServer());
}
}</programlisting>
<para>
The configuration above sets up an
<classname>EmbeddedSolrServer</classname>
which is used by the
<classname>SolrTemplate</classname>
. Spring Data Solr Repositories are activated using the
<interfacename>@EnableSolrRepositories</interfacename>
annotation, which
essentially carries the same attributes as the XML
namespace does. If no
base package is configured, it will use the
one
the configuration class
resides in.
</para>
</example>
</section>
<section id="solr.cdi">
<title>Solr Repositores using CDI</title>
<para>The Spring Data Solr repositories can also be set up using CDI
functionality.
</para>
<example>
<title>Spring Data Solr repositories using JavaConfig</title>
<programlisting language="java">class SolrTemplateProducer {
@Produces
@ApplicationScoped
public SolrOperations createSolrTemplate() {
return new SolrTemplate(new EmbeddedSolrServerFactory("classpath:com/acme/solr"));
}
}
class ProductService {
private ProductRepository repository;
public Page&lt;Product&gt; findAvailableProductsByName(String name, Pageable pageable) {
return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
}
@Inject
public void setRepository(ProductRepository repository) {
this.repository = repository;
}
}</programlisting>
</example>
</section>
</section>
<section id="solr.query-methods">
<title>Query methods</title>
<section id="solr.query-methods.finders">
<title>Query lookup strategies</title>
<para>
The Solr module supports defining a query manually as String or
have
it being derived from the method name.
<note>
There is no QueryDSL Support present at this time.
</note>
</para>
<simplesect>
<title>Declared queries</title>
<para>
Deriving the query from the method name is not always sufficient
and/or may result in unreadable method names. In this case one
might make either use of Solr named queries (see
<xref linkend="solr.query-methods.named-queries" />
) or use the
<interfacename>@Query</interfacename>
annotation (see
<xref linkend="solr.query-methods.at-query" />
).
</para>
</simplesect>
</section>
<section id="solr.query-methods.criterions">
<title>Query creation</title>
<para>
Generally the query creation mechanism for Solr works as described
in
<xref linkend="repositories.query-methods" />
. Here's a short example
of what a Solr query method translates into:
<example>
<title>Query creation from method names</title>
<programlisting language="java">public interface ProductRepository extends Repository&lt;Product, String&gt; {
List&lt;Product&gt; findByNameAndPopularity(String name, Integer popularity);
}</programlisting>
<para>
The method name above will be translated into the following
solr query
</para>
<programlisting>q=name:?0 AND popularity:?1</programlisting>
</example>
</para>
<para>
A list of supported keywords for Solr is shown below.
<table>
<title>Supported keywords inside method names</title>
<tgroup cols="3">
<colspec colwidth="1*" />
<colspec colwidth="2*" />
<colspec colwidth="3*" />
<thead>
<row>
<entry>Keyword</entry>
<entry>Sample</entry>
<entry>Solr Query String</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<code>And</code>
</entry>
<entry>
<code>findByNameAndPopularity</code>
</entry>
<entry>
<code>q=name:?0 AND popularity:?1</code>
</entry>
</row>
<row>
<entry>
<code>Or</code>
</entry>
<entry>
<code>findByNameOrPopularity</code>
</entry>
<entry>
<code>q=name:?0 OR popularity:?1</code>
</entry>
</row>
<row>
<entry>
<code>Is</code>
</entry>
<entry>
<code>findByName</code>
</entry>
<entry>
<code>q=name:?0</code>
</entry>
</row>
<row>
<entry>
<code>Not</code>
</entry>
<entry>
<code>findByNameNot</code>
</entry>
<entry>
<code>q=-name:?0</code>
</entry>
</row>
<row>
<entry>
<code>Between</code>
</entry>
<entry>
<code>findByPopularityBetween</code>
</entry>
<entry>
<code>q=popularity:[?0 TO ?1]</code>
</entry>
</row>
<row>
<entry>
<code>LessThanEqual</code>
</entry>
<entry>
<code>findByPopularityLessThan</code>
</entry>
<entry>
<code>q=popularity:[* TO ?0]</code>
</entry>
</row>
<row>
<entry>
<code>GreaterThanEqual</code>
</entry>
<entry>
<code>findByPopularityGreaterThan</code>
</entry>
<entry>
<code>q=popularity:[?0 TO *]</code>
</entry>
</row>
<row>
<entry>
<code>Before</code>
</entry>
<entry>
<code>findByLastModifiedBefore</code>
</entry>
<entry>
<code>q=last_modified:[* TO ?0]</code>
</entry>
</row>
<row>
<entry>
<code>After</code>
</entry>
<entry>
<code>findByLastModifiedAfter</code>
</entry>
<entry>
<code>q=last_modified:[?0 TO *]</code>
</entry>
</row>
<row>
<entry>
<code>Like</code>
</entry>
<entry>
<code>findByNameLike</code>
</entry>
<entry>
<code>q=name:?0*</code>
</entry>
</row>
<row>
<entry>
<code>StartingWith</code>
</entry>
<entry>
<code>findByNameStartingWith</code>
</entry>
<entry>
<code>q=name:?0*</code>
</entry>
</row>
<row>
<entry>
<code>EndingWith</code>
</entry>
<entry>
<code>findByNameEndingWith</code>
</entry>
<entry>
<code>q=name:*?0</code>
</entry>
</row>
<row>
<entry>
<code>Containing</code>
</entry>
<entry>
<code>findByNameContaining</code>
</entry>
<entry>
<code>q=name:*?0*</code>
</entry>
</row>
<row>
<entry>
<code>In</code>
</entry>
<entry>
<code>findByNameIn(Collection&lt;String&gt;
names)
</code>
</entry>
<entry>
<code>q=name:(?0... )</code>
</entry>
</row>
<row>
<entry>
<code>NotIn</code>
</entry>
<entry>
<code>findByNameNotIn(Collection&lt;String&gt;
names)
</code>
</entry>
<entry>
<code>q=-name:(?0... )</code>
</entry>
</row>
<row>
<entry>
<code>Near</code>
</entry>
<entry>
<code>findByStoreNear</code>
</entry>
<entry>
<code>q={!geofilt pt=?0.latitude,?0.longitude sfield=store
d=?1}
</code>
</entry>
</row>
<row>
<entry>
<code>True</code>
</entry>
<entry>
<code>findByAvailableTrue</code>
</entry>
<entry>
<code>q=inStock:true</code>
</entry>
</row>
<row>
<entry>
<code>False</code>
</entry>
<entry>
<code>findByAvailableFalse</code>
</entry>
<entry>
<code>q=inStock:false</code>
</entry>
</row>
<row>
<entry>
<code>OrderBy</code>
</entry>
<entry>
<code>findByAvailableTrueOrderByNameDesc</code>
</entry>
<entry>
<code>q=inStock:true&amp;sort=name desc</code>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</section>
<section id="solr.query-methods.at-query">
<title>Using @Query Annotation</title>
<para>
Using named queries (
<xref linkend="solr.query-methods.named-queries" />
) to declare queries for entities is a valid
approach and works fine
for a small number of queries. As the
queries themselves are tied to
the Java method that executes them,
you actually can bind them
directly using the Spring Data Solr
<code>@Query</code>
annotation.
</para>
<example>
<title>
Declare query at the method using the
<interfacename>@Query</interfacename>
annotation.
</title>
<programlisting language="java">public interface ProductRepository extends SolrRepository&lt;Product, String&gt; {
@Query("inStock:?0")
List&lt;Product&gt; findByAvailable(Boolean available);
}</programlisting>
</example>
</section>
<section id="solr.query-methods.named-queries">
<title>Using NamedQueries</title>
<para>
Named queries can be kept in a properties file and wired to the
accroding method. Please mind the naming convention described in
<xref linkend="repositories.query-methods.query-lookup-strategies" />
or use
<interfacename>@Query</interfacename>
.
</para>
<example>
<title>
Declare named query in properites file
</title>
<programlisting>Product.findByNamedQuery=popularity:?0
Product.findByName=name:?0</programlisting>
<programlisting language="java">public interface ProductRepository extends SolrCrudRepository&lt;Product, String&gt; {
List&lt;Product&gt; findByNamedQuery(Integer popularity);
@Query(name = "Product.findByName")
List&lt;Product&gt; findByAnnotatedNamedQuery(String name);
}</programlisting>
</example>
</section>
</section>
</chapter>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- distribution assembly descriptor. packages up jars, source jars, documentation,
dependencies and other resources into a single archive suitable for download and
standalone use.
see pom.xml 'maven-assembly-plugin' declaration
see src/main/scripts/build-distribution.sh
see http://www.sonatype.com/books/mvnref-book/reference/assemblies-set-dist-assemblies.html -->
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<!-- adds readme and other textfiles to the root of the distribution archive -->
<directory>src/main/resources</directory>
<includes>
<include>license.txt</include>
<include>notice.txt</include>
<include>changelog.txt</include>
</includes>
<outputDirectory/>
<lineEnding>dos</lineEnding>
</fileSet>
<fileSet>
<!-- adds reference manual (html and pdf) to the distribution archive under the
'docs/reference' directory
see pom.xml 'maven-javadoc-plugin' declaration -->
<directory>target/site/reference</directory>
<outputDirectory>docs/reference</outputDirectory>
</fileSet>
<fileSet>
<!-- adds javadoc html to the distribution archive under the 'docs/javadoc' directory
see pom.xml 'maven-javadoc-plugin' declaration -->
<directory>target/site/apidocs</directory>
<outputDirectory>docs/javadoc</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${dist.finalName}.jar</source>
<outputDirectory>dist</outputDirectory>
<fileMode>0644</fileMode>
</file>
<file>
<source>target/${dist.finalName}-sources.jar</source>
<outputDirectory>sources</outputDirectory>
<fileMode>0644</fileMode>
</file>
</files>
</assembly>

View File

@ -0,0 +1,18 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>all</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>

View File

@ -1,5 +1,4 @@
Spring Data Solr 1.0
Copyright (c) [2012] SpringSource, a division of VMware, Inc.
Spring Data Elasticsearch
This product is licensed to you under the Apache License, Version 2.0 (the "License").
You may not use this product except in compliance with the License.

View File

@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@Ignore
public class CriteriaQueryTest {
@Resource
@ -476,13 +475,13 @@ public class CriteriaQueryTest {
elasticsearchTemplate.refresh(SampleEntity.class,true);
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("rate").between(350,null));
//when
SampleEntity sampleEntity = elasticsearchTemplate.queryForObject(criteriaQuery, SampleEntity.class);
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
//then
assertThat(sampleEntity,is(notNullValue()));
assertThat(page,is(notNullValue()));
assertThat(page.getTotalElements(),is(greaterThanOrEqualTo(1L)));
}
@Test
@Ignore
public void testBetweenWithoutLowerBound() {
//given
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
@ -513,13 +512,13 @@ public class CriteriaQueryTest {
elasticsearchTemplate.refresh(SampleEntity.class,true);
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("rate").between(null,550));
//when
SampleEntity sampleEntity = elasticsearchTemplate.queryForObject(criteriaQuery, SampleEntity.class);
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
//then
assertThat(sampleEntity,is(notNullValue()));
assertThat(page,is(notNullValue()));
assertThat(page.getTotalElements(),is(greaterThanOrEqualTo(1L)));
}
@Test
@Ignore
public void testLessThanEqauls() {
//given
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
@ -550,13 +549,13 @@ public class CriteriaQueryTest {
elasticsearchTemplate.refresh(SampleEntity.class,true);
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("rate").lessThanEqual(750));
//when
SampleEntity sampleEntity = elasticsearchTemplate.queryForObject(criteriaQuery, SampleEntity.class);
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
//then
assertThat(sampleEntity,is(notNullValue()));
assertThat(page,is(notNullValue()));
assertThat(page.getTotalElements(),is(greaterThanOrEqualTo(1L)));
}
@Test
@Ignore
public void testGreaterThanEqauls() {
//given
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
@ -587,9 +586,10 @@ public class CriteriaQueryTest {
elasticsearchTemplate.refresh(SampleEntity.class,true);
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("rate").greaterThanEqual(950));
//when
SampleEntity sampleEntity = elasticsearchTemplate.queryForObject(criteriaQuery, SampleEntity.class);
Page<SampleEntity> page = elasticsearchTemplate.queryForPage(criteriaQuery, SampleEntity.class);
//then
assertThat(sampleEntity,is(notNullValue()));
assertThat(page,is(notNullValue()));
assertThat(page.getTotalElements(),is(greaterThanOrEqualTo(1L)));
}
@Test

View File

@ -1,20 +1,23 @@
package org.springframework.data.elasticsearch.repositories;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.SampleEntity;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ -24,6 +27,18 @@ public class CustomMethodRepositoryTest {
@Resource
private SampleCustomMethodRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before(){
elasticsearchTemplate.createIndex(SampleEntity.class);
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setElasticsearchQuery(matchAllQuery());
elasticsearchTemplate.delete(deleteQuery,SampleEntity.class);
elasticsearchTemplate.refresh(SampleEntity.class, true);
}
@Test
public void shouldExecuteCustomMethod(){
//given
@ -40,6 +55,22 @@ public class CustomMethodRepositoryTest {
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldExecuteCustomMethodForNext(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setType("some");
sampleEntity.setMessage("some message");
repository.save(sampleEntity);
//when
Page<SampleEntity> page = repository.findByTypeNot("test", new PageRequest(1, 10));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldExecuteCustomMethodWithQuery(){
//given
@ -56,4 +87,31 @@ public class CustomMethodRepositoryTest {
assertThat(page.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldExecuteCustomMethodWithLessThan(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setType("test");
sampleEntity.setRate(10);
sampleEntity.setMessage("some message");
repository.save(sampleEntity);
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setType("test");
sampleEntity2.setRate(20);
sampleEntity2.setMessage("some message");
repository.save(sampleEntity2);
//when
Page<SampleEntity> page = repository.findByRateLessThan(10, new PageRequest(1, 10));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getTotalElements(), is(equalTo(1L)));
}
}

View File

@ -11,7 +11,13 @@ public interface SampleCustomMethodRepository extends ElasticsearchRepository<Sa
Page<SampleEntity> findByType(String type, Pageable pageable);
Page<SampleEntity> findByTypeNot(String type, Pageable pageable);
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"message\" : \"?0\"}}}}")
Page<SampleEntity> findByMessage(String message, Pageable pageable);
Page<SampleEntity> findByRateLessThan(int rate, Pageable pageable);
Page<SampleEntity> findByRateBefore(int rate, Pageable pageable);
}