├── .gitignore ├── .travis.yml ├── README.md ├── pom.xml ├── site ├── apidocs.zip ├── emma.zip ├── reference.zip └── reference │ └── pdf │ └── spring-data-elasticsearch-reference.pdf └── src ├── docbkx ├── index.xml ├── preface.xml ├── reference │ ├── data-elasticsearch.xml │ ├── elasticsearch-misc.xml │ └── repositories.xml └── resources │ ├── css │ ├── highlight.css │ └── html.css │ ├── images │ ├── admons │ │ ├── blank.png │ │ ├── caution.gif │ │ ├── caution.png │ │ ├── caution.tif │ │ ├── draft.png │ │ ├── home.gif │ │ ├── home.png │ │ ├── important.gif │ │ ├── important.png │ │ ├── important.tif │ │ ├── next.gif │ │ ├── next.png │ │ ├── note.gif │ │ ├── note.png │ │ ├── note.tif │ │ ├── prev.gif │ │ ├── prev.png │ │ ├── tip.gif │ │ ├── tip.png │ │ ├── tip.tif │ │ ├── toc-blank.png │ │ ├── toc-minus.png │ │ ├── toc-plus.png │ │ ├── up.gif │ │ ├── up.png │ │ ├── warning.gif │ │ ├── warning.png │ │ └── warning.tif │ ├── callouts │ │ ├── 1.png │ │ ├── 10.png │ │ ├── 11.png │ │ ├── 12.png │ │ ├── 13.png │ │ ├── 14.png │ │ ├── 15.png │ │ ├── 2.png │ │ ├── 3.png │ │ ├── 4.png │ │ ├── 5.png │ │ ├── 6.png │ │ ├── 7.png │ │ ├── 8.png │ │ └── 9.png │ ├── logo.png │ └── xdev-spring_logo.jpg │ └── xsl │ ├── fopdf.xsl │ ├── highlight-fo.xsl │ ├── highlight.xsl │ ├── html.xsl │ └── html_chunk.xsl ├── main ├── assembly │ ├── distribution.xml │ └── jar-with-dependencies.descriptor.xml ├── java │ └── org │ │ └── springframework │ │ └── data │ │ └── elasticsearch │ │ ├── ElasticsearchException.java │ │ ├── annotations │ │ ├── Document.java │ │ ├── Field.java │ │ └── Query.java │ │ ├── client │ │ ├── NodeClientFactoryBean.java │ │ └── TransportClientFactoryBean.java │ │ ├── config │ │ ├── ElasticsearchNamespaceHandler.java │ │ ├── NodeClientBeanDefinitionParser.java │ │ └── TransportClientBeanDefinitionParser.java │ │ ├── core │ │ ├── CriteriaQueryProcessor.java │ │ ├── ElasticsearchOperations.java │ │ ├── ElasticsearchTemplate.java │ │ ├── MappingBuilder.java │ │ ├── ResultsMapper.java │ │ ├── convert │ │ │ ├── DateTimeConverters.java │ │ │ ├── ElasticsearchConverter.java │ │ │ └── MappingElasticsearchConverter.java │ │ ├── mapping │ │ │ ├── ElasticsearchPersistentEntity.java │ │ │ ├── ElasticsearchPersistentProperty.java │ │ │ ├── SimpleElasticsearchMappingContext.java │ │ │ ├── SimpleElasticsearchPersistentEntity.java │ │ │ └── SimpleElasticsearchPersistentProperty.java │ │ └── query │ │ │ ├── AbstractQuery.java │ │ │ ├── Criteria.java │ │ │ ├── CriteriaQuery.java │ │ │ ├── DeleteQuery.java │ │ │ ├── Field.java │ │ │ ├── GetQuery.java │ │ │ ├── IndexQuery.java │ │ │ ├── MoreLikeThisQuery.java │ │ │ ├── NativeSearchQuery.java │ │ │ ├── NativeSearchQueryBuilder.java │ │ │ ├── Query.java │ │ │ ├── SearchQuery.java │ │ │ ├── SimpleField.java │ │ │ └── StringQuery.java │ │ └── repository │ │ ├── ElasticsearchCrudRepository.java │ │ ├── ElasticsearchRepository.java │ │ ├── cdi │ │ ├── ElasticsearchRepositoryBean.java │ │ └── ElasticsearchRepositoryExtension.java │ │ ├── config │ │ ├── ElasticsearchRepositoriesRegistrar.java │ │ ├── ElasticsearchRepositoryConfigExtension.java │ │ └── EnableElasticsearchRepositories.java │ │ ├── query │ │ ├── AbstractElasticsearchRepositoryQuery.java │ │ ├── ElasticsearchPartQuery.java │ │ ├── ElasticsearchQueryMethod.java │ │ ├── ElasticsearchStringQuery.java │ │ └── parser │ │ │ └── ElasticsearchQueryCreator.java │ │ └── support │ │ ├── AbstractElasticsearchRepository.java │ │ ├── ElasticsearchEntityInformation.java │ │ ├── ElasticsearchEntityInformationCreator.java │ │ ├── ElasticsearchEntityInformationCreatorImpl.java │ │ ├── ElasticsearchRepositoryFactory.java │ │ ├── ElasticsearchRepositoryFactoryBean.java │ │ ├── MappingElasticsearchEntityInformation.java │ │ ├── NumberKeyedRepository.java │ │ └── SimpleElasticsearchRepository.java └── resources │ ├── META-INF │ ├── services │ │ └── javax.enterprise.inject.spi.Extension │ ├── spring.handlers │ ├── spring.schemas │ └── spring.tooling │ ├── changelog.txt │ ├── license.txt │ ├── notice.txt │ └── org │ └── springframework │ └── data │ └── elasticsearch │ └── config │ └── spring-elasticsearch-1.0.xsd └── test ├── java └── org │ └── springframework │ └── data │ └── elasticsearch │ ├── Author.java │ ├── Book.java │ ├── DoubleIDEntity.java │ ├── IntegerIDEntity.java │ ├── NestedObjectTest.java │ ├── NonDocumentEntity.java │ ├── NonDocumentEntityTest.java │ ├── SampleEntity.java │ ├── SampleMappingEntity.java │ ├── config │ ├── ElasticsearchNamespaceHandlerTest.java │ └── EnableElasticsearchRepositoriesTest.java │ ├── core │ ├── ElasticsearchTemplateTest.java │ ├── convert │ │ ├── DateTimeConvertersTest.java │ │ └── MappingElasticsearchConverterTest.java │ ├── mapping │ │ └── SimpleElasticsearchPersistentEntityTest.java │ └── query │ │ └── CriteriaQueryTest.java │ ├── repositories │ ├── CustomMethodRepositoryTest.java │ ├── DoubleIDRepository.java │ ├── IntegerIDRepository.java │ ├── NonDocumentEntityRepository.java │ ├── SampleCustomMethodRepository.java │ ├── SampleElasticSearchBookRepository.java │ └── SampleElasticsearchRepository.java │ └── repository │ └── support │ ├── DoubleIDRepositoryTest.java │ ├── ElasticsearchRepositoryFactoryTest.java │ ├── IntegerIDRepositoryTest.java │ └── SimpleElasticsearchRepositoryTest.java └── resources ├── custom-method-repository-test.xml ├── elasticsearch-template-test.xml ├── logback.xml ├── org └── springframework │ └── data │ └── elasticsearch │ └── config │ └── namespace.xml ├── repository-non-document-entity.xml ├── repository-test-nested-object.xml └── simple-repository-test.xml /.gitignore: -------------------------------------------------------------------------------- 1 | atlassian-ide-plugin.xml 2 | 3 | ## Ignore svn files 4 | .svn 5 | 6 | target 7 | 8 | ## Ignore project files created by Eclipse 9 | .settings 10 | .project 11 | .classpath 12 | 13 | ## Ignore project files created by IntelliJ IDEA 14 | *.iml 15 | *.ipr 16 | *.iws 17 | .idea 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Note : This Project is moved to SpringSource Repository [Spring-Data-Elasticsearch](https://github.com/SpringSource/spring-data-elasticsearch) 2 | 3 | Hence all the new changes will be pushed to new repository 4 | 5 | Thanks 6 | 7 | BioMed Central Development Team 8 | -------------------------------------------------------------------------------- /site/apidocs.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/site/apidocs.zip -------------------------------------------------------------------------------- /site/emma.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/site/emma.zip -------------------------------------------------------------------------------- /site/reference.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/site/reference.zip -------------------------------------------------------------------------------- /site/reference/pdf/spring-data-elasticsearch-reference.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/site/reference/pdf/spring-data-elasticsearch-reference.pdf -------------------------------------------------------------------------------- /src/docbkx/index.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Spring Data Elasticsearch 7 | 8 | 9 | BioMed Central 10 | Development Team 11 | 12 | 13 | 14 | 15 | Copies of this document may be made for your own use and for 16 | distribution to others, provided that you do not 17 | charge any fee for 18 | such copies and further provided that each copy 19 | contains this 20 | Copyright Notice, whether 21 | distributed in print or electronically. 22 | 23 | 24 | 25 | 26 | 2013 27 | The original author(s) 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Reference Documentation 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | Appendix 48 | 50 | 52 | 53 | 55 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/docbkx/preface.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Preface 6 | The Spring Data Elasticsearch project applies core Spring concepts to 7 | the 8 | development of solutions using the Elasticsearch Search Engine. 9 | We have povided a "template" as a high-level abstraction for 10 | storing,querying,sorting and faceting documents. You will notice similarities 11 | to the Spring data solr and 12 | mongodb support in the Spring Framework. 13 | 14 |
15 | Project Metadata 16 | 17 | 18 | 19 | Version Control - 20 | git://github.com/BioMedCentralLtd/spring-data-elasticsearch.git 21 | 22 | 23 | 24 | 25 |
26 |
27 | Requirements 28 | 29 | Requires 30 | Elasticsearch 31 | 0.20.2 and above or optional dependency or not even that if you are using Embedded Node Client 32 | 33 |
34 |
-------------------------------------------------------------------------------- /src/docbkx/reference/elasticsearch-misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Miscellaneous Elasticsearch Operation Support 6 | 7 | 8 | This chapter covers additional support for Elasticsearch operations 9 | that cannot be directly accessed via the repository 10 | interface. 11 | It is recommended to add those operations as custom 12 | implementation as 13 | described in 14 | 15 | . 16 | 17 | 18 |
19 | Filter Builder 20 | 21 | Filter Builder improves query speed. 22 | 23 | 24 | 25 | private ElasticsearchTemplate elasticsearchTemplate; 26 | SearchQuery searchQuery = new NativeSearchQueryBuilder() 27 | .withQuery(matchAllQuery()) 28 | .withFilter(boolFilter().must(termFilter("id", documentId))) 29 | .build(); 30 | Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery,SampleEntity.class); 31 | 32 | 33 |
34 |
35 | Using Scan And Scroll For Big Result Set 36 | 37 | Elasticsearch has scan and scroll feature for getting big result set in chunks. 38 | ElasticsearchTemplate 39 | has scan and scroll methods that can be used as below. 40 | 41 | 42 | 43 | Using Scan and Scroll 44 | 45 | 46 | SearchQuery searchQuery = new NativeSearchQueryBuilder() 47 | .withQuery(matchAllQuery()) 48 | .withIndices("test-index") 49 | .withTypes("test-type") 50 | .withPageable(new PageRequest(0,1)) 51 | .build(); 52 | String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false); 53 | List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>(); 54 | boolean hasRecords = true; 55 | while (hasRecords){ 56 | Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>() { 57 | @Override 58 | public Page<SampleEntity> mapResults(SearchResponse response) { 59 | List<SampleEntity> chunk = new ArrayList<SampleEntity>(); 60 | for(SearchHit searchHit : response.getHits()){ 61 | if(response.getHits().getHits().length <= 0) { 62 | return null; 63 | } 64 | SampleEntity user = new SampleEntity(); 65 | user.setId(searchHit.getId()); 66 | user.setMessage((String)searchHit.getSource().get("message")); 67 | chunk.add(user); 68 | } 69 | return new PageImpl<SampleEntity>(chunk); 70 | } 71 | }); 72 | if(page != null) { 73 | sampleEntities.addAll(page.getContent()); 74 | hasRecords = page.hasNextPage(); 75 | } 76 | else{ 77 | hasRecords = false; 78 | } 79 | } 80 | } 81 | 82 |
83 |
-------------------------------------------------------------------------------- /src/docbkx/resources/css/highlight.css: -------------------------------------------------------------------------------- 1 | /* 2 | borrowed from: https://raw.github.com/SpringSource/spring-data-jpa/master/src/docbkx/resources/css/highlight.css 3 | code highlight CSS resemblign the Eclipse IDE default color schema 4 | @author Costin Leau 5 | */ 6 | 7 | .hl-keyword { 8 | color: #7F0055; 9 | font-weight: bold; 10 | } 11 | 12 | .hl-comment { 13 | color: #3F5F5F; 14 | font-style: italic; 15 | } 16 | 17 | .hl-multiline-comment { 18 | color: #3F5FBF; 19 | font-style: italic; 20 | } 21 | 22 | .hl-tag { 23 | color: #3F7F7F; 24 | } 25 | 26 | .hl-attribute { 27 | color: #7F007F; 28 | } 29 | 30 | .hl-value { 31 | color: #2A00FF; 32 | } 33 | 34 | .hl-string { 35 | color: #2A00FF; 36 | } -------------------------------------------------------------------------------- /src/docbkx/resources/css/html.css: -------------------------------------------------------------------------------- 1 | /* 2 | borrowed from: https://raw.github.com/SpringSource/spring-data-jpa/master/src/docbkx/resources/css/html.css 3 | */ 4 | 5 | @IMPORT url("highlight.css"); 6 | 7 | html { 8 | padding: 0pt; 9 | margin: 0pt; 10 | } 11 | 12 | body { 13 | margin-left: 15%; 14 | margin-right: 15%; 15 | font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 16 | } 17 | 18 | div { 19 | margin: 0pt; 20 | } 21 | 22 | p { 23 | text-align: justify; 24 | line-height: 1.3em; 25 | } 26 | 27 | hr { 28 | border: 1px solid gray; 29 | background: gray; 30 | } 31 | 32 | h1,h2,h3,h4,h5 { 33 | color: #234623; 34 | font-weight: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 35 | margin-bottom: 0em; 36 | margin-top: 2em; 37 | } 38 | 39 | pre { 40 | line-height: 1.0; 41 | color: black; 42 | } 43 | 44 | table code { 45 | font-size: 110%; 46 | } 47 | 48 | pre.programlisting { 49 | font-size: 1em; 50 | padding: 3pt 3pt; 51 | border: 1pt solid black; 52 | background: #eeeeee; 53 | clear: both; 54 | } 55 | 56 | div.table { 57 | margin: 1em; 58 | padding: 0.5em; 59 | text-align: center; 60 | } 61 | 62 | div.table table { 63 | display: table; 64 | width: 100%; 65 | } 66 | 67 | div.table td { 68 | padding-left: 7px; 69 | padding-right: 7px; 70 | } 71 | 72 | .sidebar { 73 | float: right; 74 | margin: 10px 0 10px 30px; 75 | padding: 10px 20px 20px 20px; 76 | width: 33%; 77 | border: 1px solid black; 78 | background-color: #F4F4F4; 79 | font-size: 14px; 80 | } 81 | 82 | .mediaobject { 83 | padding-top: 30px; 84 | padding-bottom: 30px; 85 | } 86 | 87 | .legalnotice { 88 | font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 89 | font-size: 12px; 90 | font-style: italic; 91 | } 92 | 93 | p.releaseinfo { 94 | font-size: 100%; 95 | font-weight: bold; 96 | font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 97 | padding-top: 10px; 98 | } 99 | 100 | p.pubdate { 101 | font-size: 120%; 102 | font-weight: bold; 103 | font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 104 | } 105 | 106 | span.productname { 107 | font-size: 200%; 108 | font-weight: bold; 109 | font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 110 | } 111 | 112 | code { 113 | font-size: 125%; 114 | } -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/blank.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/caution.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/caution.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/caution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/caution.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/caution.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/caution.tif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/draft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/draft.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/home.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/home.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/home.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/important.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/important.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/important.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/important.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/important.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/important.tif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/next.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/next.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/next.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/note.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/note.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/note.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/note.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/note.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/note.tif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/prev.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/prev.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/prev.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/prev.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/tip.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/tip.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/tip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/tip.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/tip.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/tip.tif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/toc-blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/toc-blank.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/toc-minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/toc-minus.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/toc-plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/toc-plus.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/up.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/up.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/up.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/warning.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/warning.gif -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/warning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/warning.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/admons/warning.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/admons/warning.tif -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/1.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/10.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/11.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/12.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/13.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/14.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/15.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/2.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/3.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/4.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/5.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/6.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/7.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/8.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/callouts/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/callouts/9.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/logo.png -------------------------------------------------------------------------------- /src/docbkx/resources/images/xdev-spring_logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/docbkx/resources/images/xdev-spring_logo.jpg -------------------------------------------------------------------------------- /src/docbkx/resources/xsl/highlight-fo.xsl: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/docbkx/resources/xsl/highlight.xsl: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/docbkx/resources/xsl/html.xsl: -------------------------------------------------------------------------------- 1 | 2 | 7 | 12 | 13 | 14 | 15 | 16 | 19 | 20 | 21 | 0 22 | 0 23 | 1 24 | 25 | 28 | 29 | 30 | 31 | book toc 32 | 33 | 34 | 35 | 3 36 | 37 | 40 | 41 | 42 | 1 43 | 44 | 45 | 46 | 49 | 50 | 51 | 1 52 | 53 | 54 | 90 55 | 56 | 59 | 60 | 61 | 1 62 | images/admons/ 63 | 66 | 67 | 68 | figure after 69 | example before 70 | equation before 71 | table before 72 | procedure before 73 | 74 | 75 | 76 | , 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
86 |

Authors

87 |

88 | 89 |

90 |
91 | 94 | 95 | 96 | 105 | 106 | 107 |
108 | 109 | -------------------------------------------------------------------------------- /src/main/assembly/distribution.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | distribution 10 | 11 | zip 12 | 13 | true 14 | 15 | 16 | 17 | src/main/resources 18 | 19 | license.txt 20 | notice.txt 21 | changelog.txt 22 | 23 | 24 | dos 25 | 26 | 27 | 30 | target/site/reference 31 | docs/reference 32 | 33 | 34 | 36 | target/site/apidocs 37 | docs/javadoc 38 | 39 | 40 | 41 | 42 | target/${dist.finalName}.jar 43 | dist 44 | 0644 45 | 46 | 47 | target/${dist.finalName}-sources.jar 48 | sources 49 | 0644 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/main/assembly/jar-with-dependencies.descriptor.xml: -------------------------------------------------------------------------------- 1 | 4 | all 5 | 6 | jar 7 | 8 | false 9 | 10 | 11 | / 12 | true 13 | true 14 | runtime 15 | true 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/ElasticsearchException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | 19 | import java.util.Map; 20 | 21 | /** 22 | * ElasticsearchException 23 | * 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | */ 27 | public class ElasticsearchException extends RuntimeException{ 28 | 29 | private Map failedDocuments; 30 | 31 | public ElasticsearchException(String message) { 32 | super(message); 33 | } 34 | 35 | public ElasticsearchException(String message, Throwable cause) { 36 | super(message, cause); 37 | } 38 | 39 | public ElasticsearchException(String message, Throwable cause, Map failedDocuments) { 40 | super(message, cause); 41 | this.failedDocuments = failedDocuments; 42 | } 43 | 44 | public ElasticsearchException(String message, Map failedDocuments) { 45 | super(message); 46 | this.failedDocuments = failedDocuments; 47 | } 48 | 49 | public Map getFailedDocuments() { 50 | return failedDocuments; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/annotations/Document.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.annotations; 18 | 19 | 20 | import org.springframework.data.annotation.Persistent; 21 | 22 | import java.lang.annotation.*; 23 | 24 | /** 25 | * Document 26 | * 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | 31 | @Persistent 32 | @Inherited 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target({ ElementType.TYPE }) 35 | public @interface Document { 36 | 37 | String indexName(); 38 | String type() default ""; 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/annotations/Field.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.annotations; 18 | 19 | import java.lang.annotation.*; 20 | 21 | 22 | /** 23 | * @author Rizwan Idrees 24 | * @author Mohsin Husen 25 | */ 26 | @Retention(RetentionPolicy.RUNTIME) 27 | @Target(ElementType.FIELD) 28 | @Documented 29 | public @interface Field { 30 | 31 | String type() default ""; 32 | String index() default ""; 33 | boolean store() default true; 34 | String searchAnalyzer() default ""; 35 | String indexAnalyzer() default ""; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/annotations/Query.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.annotations; 17 | 18 | import java.lang.annotation.*; 19 | 20 | /** 21 | * Query 22 | * 23 | * @author Rizwan Idrees 24 | * @author Mohsin Husen 25 | */ 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.METHOD) 29 | @Documented 30 | public @interface Query { 31 | 32 | /** 33 | * Elasticsearch query to be used when executing query. May contain placeholders eg. ?0 34 | * 35 | * @return 36 | */ 37 | String value() default ""; 38 | 39 | /** 40 | * Named Query Named looked up by repository. 41 | * 42 | * @return 43 | */ 44 | String name() default ""; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/client/NodeClientFactoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.client; 17 | 18 | import org.elasticsearch.client.Client; 19 | import org.elasticsearch.client.node.NodeClient; 20 | import org.springframework.beans.factory.FactoryBean; 21 | import org.springframework.beans.factory.InitializingBean; 22 | 23 | import static org.elasticsearch.node.NodeBuilder.nodeBuilder; 24 | 25 | /** 26 | * NodeClientFactoryBean 27 | * 28 | * @author Rizwan Idrees 29 | * @author Mohsin Husen 30 | */ 31 | 32 | public class NodeClientFactoryBean implements FactoryBean, InitializingBean{ 33 | 34 | private boolean local; 35 | private NodeClient nodeClient; 36 | 37 | NodeClientFactoryBean() { 38 | } 39 | 40 | public NodeClientFactoryBean(boolean local) { 41 | this.local = local; 42 | } 43 | 44 | @Override 45 | public NodeClient getObject() throws Exception { 46 | return nodeClient; 47 | } 48 | 49 | @Override 50 | public Class getObjectType() { 51 | return NodeClient.class; 52 | } 53 | 54 | @Override 55 | public boolean isSingleton() { 56 | return true; 57 | } 58 | 59 | @Override 60 | public void afterPropertiesSet() throws Exception { 61 | nodeClient = (NodeClient) nodeBuilder().local(this.local).node().client(); 62 | } 63 | 64 | public void setLocal(boolean local) { 65 | this.local = local; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/client/TransportClientFactoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.client; 18 | 19 | import org.elasticsearch.client.transport.TransportClient; 20 | import org.elasticsearch.common.settings.Settings; 21 | import org.elasticsearch.common.transport.InetSocketTransportAddress; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | import org.springframework.beans.factory.DisposableBean; 25 | import org.springframework.beans.factory.FactoryBean; 26 | import org.springframework.beans.factory.InitializingBean; 27 | import org.springframework.util.Assert; 28 | 29 | import java.util.Properties; 30 | 31 | import static org.apache.commons.lang.StringUtils.substringAfter; 32 | import static org.apache.commons.lang.StringUtils.substringBefore; 33 | import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; 34 | 35 | /** 36 | * TransportClientFactoryBean 37 | * 38 | * @author Rizwan Idrees 39 | * @author Mohsin Husen 40 | */ 41 | 42 | public class TransportClientFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 43 | 44 | private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class); 45 | private String[] clusterNodes; 46 | private TransportClient client; 47 | private Properties properties; 48 | static final String COLON = ":"; 49 | 50 | @Override 51 | public void destroy() throws Exception { 52 | try { 53 | logger.info("Closing elasticSearch client"); 54 | if (client != null) { 55 | client.close(); 56 | } 57 | } catch (final Exception e) { 58 | logger.error("Error closing ElasticSearch client: ", e); 59 | } 60 | } 61 | 62 | @Override 63 | public TransportClient getObject() throws Exception { 64 | return client; 65 | } 66 | 67 | @Override 68 | public Class getObjectType() { 69 | return TransportClient.class; 70 | } 71 | 72 | @Override 73 | public boolean isSingleton() { 74 | return false; 75 | } 76 | 77 | @Override 78 | public void afterPropertiesSet() throws Exception { 79 | buildClient(); 80 | } 81 | 82 | protected void buildClient() throws Exception { 83 | client = new TransportClient(settings()); 84 | Assert.notEmpty(clusterNodes,"[Assertion failed] clusterNodes settings missing."); 85 | for (String clusterNode : clusterNodes) { 86 | String hostName = substringBefore(clusterNode, COLON); 87 | String port = substringAfter(clusterNode, COLON); 88 | Assert.hasText(hostName,"[Assertion failed] missing host name in 'clusterNodes'"); 89 | Assert.hasText(port,"[Assertion failed] missing port in 'clusterNodes'"); 90 | logger.info("adding transport node : " + clusterNode); 91 | client.addTransportAddress(new InetSocketTransportAddress(hostName, Integer.valueOf(port))); 92 | } 93 | client.connectedNodes(); 94 | } 95 | 96 | private Settings settings(){ 97 | if(properties != null){ 98 | return settingsBuilder().put(properties).build(); 99 | } 100 | return settingsBuilder() 101 | .put("client.transport.sniff",true).build(); 102 | } 103 | 104 | public void setClusterNodes(String[] clusterNodes) { 105 | this.clusterNodes = clusterNodes; 106 | } 107 | 108 | public void setProperties(Properties properties) { 109 | this.properties = properties; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/config/ElasticsearchNamespaceHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.config; 18 | 19 | import org.springframework.beans.factory.xml.NamespaceHandlerSupport; 20 | import org.springframework.data.elasticsearch.repository.config.ElasticsearchRepositoryConfigExtension; 21 | import org.springframework.data.repository.config.RepositoryBeanDefinitionParser; 22 | import org.springframework.data.repository.config.RepositoryConfigurationExtension; 23 | 24 | /** 25 | * ElasticsearchNamespaceHandler 26 | * 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | 31 | public class ElasticsearchNamespaceHandler extends NamespaceHandlerSupport{ 32 | 33 | @Override 34 | public void init() { 35 | RepositoryConfigurationExtension extension = new ElasticsearchRepositoryConfigExtension(); 36 | RepositoryBeanDefinitionParser parser = new RepositoryBeanDefinitionParser(extension); 37 | 38 | registerBeanDefinitionParser("repositories", parser); 39 | registerBeanDefinitionParser("node-client", new NodeClientBeanDefinitionParser()); 40 | registerBeanDefinitionParser("transport-client", new TransportClientBeanDefinitionParser()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/config/NodeClientBeanDefinitionParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.config; 18 | 19 | import org.springframework.beans.factory.support.AbstractBeanDefinition; 20 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; 21 | import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; 22 | import org.springframework.beans.factory.xml.ParserContext; 23 | import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; 24 | import org.w3c.dom.Element; 25 | 26 | /** 27 | * NodeClientBeanDefinitionParser 28 | * 29 | * @author Rizwan Idrees 30 | * @author Mohsin Husen 31 | */ 32 | 33 | public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser { 34 | 35 | @Override 36 | protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { 37 | BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NodeClientFactoryBean.class); 38 | setLocalSettings(element,builder); 39 | return getSourcedBeanDefinition(builder, element, parserContext); 40 | } 41 | 42 | private void setLocalSettings(Element element, BeanDefinitionBuilder builder) { 43 | builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local"))); 44 | } 45 | 46 | 47 | private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, 48 | ParserContext context) { 49 | AbstractBeanDefinition definition = builder.getBeanDefinition(); 50 | definition.setSource(context.extractSource(source)); 51 | return definition; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/config/TransportClientBeanDefinitionParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.config; 18 | 19 | import org.springframework.beans.factory.support.AbstractBeanDefinition; 20 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; 21 | import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; 22 | import org.springframework.beans.factory.xml.ParserContext; 23 | import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; 24 | import org.w3c.dom.Element; 25 | 26 | import static org.apache.commons.lang.StringUtils.split; 27 | /** 28 | * TransportClientBeanDefinitionParser 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | 34 | public class TransportClientBeanDefinitionParser extends AbstractBeanDefinitionParser { 35 | 36 | private static final String SEPARATOR_CHARS = ","; 37 | 38 | @Override 39 | protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { 40 | BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TransportClientFactoryBean.class); 41 | setClusterNodes(element, builder); 42 | return getSourcedBeanDefinition(builder,element, parserContext); 43 | } 44 | 45 | private void setClusterNodes(Element element, BeanDefinitionBuilder builder){ 46 | builder.addPropertyValue("clusterNodes", split(element.getAttribute("cluster-nodes"), SEPARATOR_CHARS)); 47 | } 48 | 49 | private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, 50 | ParserContext context) { 51 | AbstractBeanDefinition definition = builder.getBeanDefinition(); 52 | definition.setSource(context.extractSource(source)); 53 | return definition; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/CriteriaQueryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core; 17 | 18 | import org.elasticsearch.index.query.BoolQueryBuilder; 19 | import org.elasticsearch.index.query.BoostableQueryBuilder; 20 | import org.elasticsearch.index.query.QueryBuilder; 21 | import org.springframework.data.elasticsearch.core.query.Criteria; 22 | import org.springframework.util.Assert; 23 | 24 | import java.util.Iterator; 25 | import java.util.ListIterator; 26 | 27 | import static org.elasticsearch.index.query.QueryBuilders.*; 28 | import static org.springframework.data.elasticsearch.core.query.Criteria.OperationKey; 29 | 30 | /** 31 | * CriteriaQueryProcessor 32 | * 33 | * @author Rizwan Idrees 34 | * @author Mohsin Husen 35 | */ 36 | class CriteriaQueryProcessor { 37 | 38 | 39 | QueryBuilder createQueryFromCriteria(Criteria criteria) { 40 | BoolQueryBuilder query = boolQuery(); 41 | 42 | ListIterator chainIterator = criteria.getCriteriaChain().listIterator(); 43 | while (chainIterator.hasNext()) { 44 | Criteria chainedCriteria = chainIterator.next(); 45 | if(chainedCriteria.isOr()){ 46 | query.should(createQueryFragmentForCriteria(chainedCriteria)); 47 | }else if(chainedCriteria.isNegating()){ 48 | query.mustNot(createQueryFragmentForCriteria(chainedCriteria)); 49 | }else{ 50 | query.must(createQueryFragmentForCriteria(chainedCriteria)); 51 | } 52 | } 53 | return query; 54 | } 55 | 56 | 57 | private QueryBuilder createQueryFragmentForCriteria(Criteria chainedCriteria) { 58 | Iterator it = chainedCriteria.getCriteriaEntries().iterator(); 59 | boolean singeEntryCriteria = (chainedCriteria.getCriteriaEntries().size() == 1); 60 | 61 | String fieldName = chainedCriteria.getField().getName(); 62 | Assert.notNull(fieldName,"Unknown field"); 63 | QueryBuilder query = null; 64 | 65 | if(singeEntryCriteria){ 66 | Criteria.CriteriaEntry entry = it.next(); 67 | query = processCriteriaEntry(entry.getKey(), entry.getValue(), fieldName); 68 | }else{ 69 | query = boolQuery(); 70 | while (it.hasNext()){ 71 | Criteria.CriteriaEntry entry = it.next(); 72 | ((BoolQueryBuilder)query).must(processCriteriaEntry(entry.getKey(), entry.getValue(), fieldName)); 73 | } 74 | } 75 | 76 | addBoost(query, chainedCriteria.getBoost()); 77 | return query; 78 | } 79 | 80 | 81 | private QueryBuilder processCriteriaEntry(OperationKey key, Object value, String fieldName) { 82 | if (value == null) { 83 | return null; 84 | } 85 | QueryBuilder query = null; 86 | 87 | switch (key){ 88 | case EQUALS: 89 | query = fieldQuery(fieldName, value); break; 90 | case CONTAINS: 91 | query = fieldQuery(fieldName,"*" + value + "*").analyzeWildcard(true); break; 92 | case STARTS_WITH: 93 | query = fieldQuery(fieldName,value +"*").analyzeWildcard(true); break; 94 | case ENDS_WITH: 95 | query = fieldQuery(fieldName, "*"+value).analyzeWildcard(true); break; 96 | case EXPRESSION: 97 | query = queryString((String)value).field(fieldName); break; 98 | case BETWEEN: 99 | Object[] ranges = (Object[]) value; 100 | query = rangeQuery(fieldName).from(ranges[0]).to(ranges[1]); break; 101 | case FUZZY: 102 | query = fuzzyQuery(fieldName, (String) value); break; 103 | case IN: 104 | query = boolQuery(); 105 | Iterable collection = (Iterable) value; 106 | for(Object item : collection){ 107 | ((BoolQueryBuilder) query).should(fieldQuery(fieldName, item)); 108 | } 109 | break; 110 | } 111 | 112 | return query; 113 | } 114 | 115 | private QueryBuilder buildNegationQuery(String fieldName, Iterator it){ 116 | BoolQueryBuilder notQuery = boolQuery(); 117 | while (it.hasNext()){ 118 | notQuery.mustNot(fieldQuery(fieldName, it.next().getValue())); 119 | } 120 | return notQuery; 121 | } 122 | 123 | private void addBoost(QueryBuilder query, float boost){ 124 | if(Float.isNaN(boost)){ 125 | return; 126 | } 127 | if(query instanceof BoostableQueryBuilder){ 128 | ((BoostableQueryBuilder)query).boost(boost); 129 | } 130 | 131 | } 132 | 133 | 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/ResultsMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core; 18 | 19 | import org.elasticsearch.action.search.SearchResponse; 20 | import org.springframework.data.domain.Page; 21 | 22 | /** 23 | * ResultsMapper 24 | * @param 25 | * 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | * 29 | */ 30 | 31 | public interface ResultsMapper { 32 | 33 | Page mapResults(SearchResponse response); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/convert/DateTimeConverters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.convert; 17 | 18 | import org.joda.time.DateTimeZone; 19 | import org.joda.time.LocalDateTime; 20 | import org.joda.time.ReadableInstant; 21 | import org.joda.time.format.DateTimeFormatter; 22 | import org.joda.time.format.ISODateTimeFormat; 23 | import org.springframework.core.convert.converter.Converter; 24 | 25 | import java.util.Date; 26 | 27 | /** 28 | * DateTimeConverters 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | 34 | public final class DateTimeConverters { 35 | 36 | private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); 37 | 38 | public enum JodaDateTimeConverter implements Converter { 39 | INSTANCE; 40 | 41 | @Override 42 | public String convert(ReadableInstant source) { 43 | if (source == null) { 44 | return null; 45 | } 46 | return formatter.print(source); 47 | } 48 | 49 | } 50 | 51 | public enum JodaLocalDateTimeConverter implements Converter { 52 | INSTANCE; 53 | 54 | @Override 55 | public String convert(LocalDateTime source) { 56 | if (source == null) { 57 | return null; 58 | } 59 | return formatter.print(source.toDateTime(DateTimeZone.UTC)); 60 | } 61 | 62 | } 63 | 64 | public enum JavaDateConverter implements Converter { 65 | INSTANCE; 66 | 67 | @Override 68 | public String convert(Date source) { 69 | if (source == null) { 70 | return null; 71 | } 72 | 73 | return formatter.print(source.getTime()); 74 | } 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/convert/ElasticsearchConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.convert; 17 | 18 | import org.springframework.core.convert.ConversionService; 19 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; 20 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 21 | import org.springframework.data.mapping.context.MappingContext; 22 | 23 | /** 24 | * ElasticsearchConverter 25 | * 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | */ 29 | 30 | public interface ElasticsearchConverter{ 31 | 32 | /** 33 | * Returns the underlying {@link org.springframework.data.mapping.context.MappingContext} used by the converter. 34 | * 35 | * @return never {@literal null} 36 | */ 37 | MappingContext, ElasticsearchPersistentProperty> getMappingContext(); 38 | 39 | /** 40 | * Returns the underlying {@link org.springframework.core.convert.ConversionService} used by the converter. 41 | * 42 | * @return never {@literal null}. 43 | */ 44 | ConversionService getConversionService(); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.convert; 17 | 18 | import org.springframework.beans.BeansException; 19 | import org.springframework.context.ApplicationContext; 20 | import org.springframework.context.ApplicationContextAware; 21 | import org.springframework.core.convert.ConversionService; 22 | import org.springframework.core.convert.support.DefaultConversionService; 23 | import org.springframework.core.convert.support.GenericConversionService; 24 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; 25 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 26 | import org.springframework.data.mapping.context.MappingContext; 27 | import org.springframework.util.Assert; 28 | 29 | /** 30 | * MappingElasticsearchConverter 31 | * 32 | * @author Rizwan Idrees 33 | * @author Mohsin Husen 34 | */ 35 | 36 | public class MappingElasticsearchConverter implements ElasticsearchConverter, ApplicationContextAware{ 37 | 38 | private final MappingContext,ElasticsearchPersistentProperty> mappingContext; 39 | private final GenericConversionService conversionService; 40 | 41 | @SuppressWarnings("unused") 42 | private ApplicationContext applicationContext; 43 | 44 | public MappingElasticsearchConverter(MappingContext,ElasticsearchPersistentProperty> mappingContext) { 45 | Assert.notNull(mappingContext); 46 | this.mappingContext = mappingContext; 47 | this.conversionService = new DefaultConversionService(); 48 | } 49 | 50 | @Override 51 | public MappingContext,ElasticsearchPersistentProperty> getMappingContext() { 52 | return mappingContext; 53 | } 54 | 55 | @Override 56 | public ConversionService getConversionService() { 57 | return this.conversionService; 58 | } 59 | 60 | @Override 61 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 62 | this.applicationContext = applicationContext; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.mapping; 17 | 18 | import org.springframework.data.mapping.PersistentEntity; 19 | /** 20 | * ElasticsearchPersistentEntity 21 | * 22 | * @author Rizwan Idrees 23 | * @author Mohsin Husen 24 | */ 25 | 26 | public interface ElasticsearchPersistentEntity extends PersistentEntity { 27 | 28 | String getIndexName(); 29 | String getIndexType(); 30 | ElasticsearchPersistentProperty getVersionProperty(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.mapping; 17 | 18 | import org.springframework.core.convert.converter.Converter; 19 | import org.springframework.data.mapping.PersistentProperty; 20 | /** 21 | * ElasticsearchPersistentProperty 22 | * 23 | * @author Rizwan Idrees 24 | * @author Mohsin Husen 25 | */ 26 | 27 | public interface ElasticsearchPersistentProperty extends PersistentProperty{ 28 | 29 | String getFieldName(); 30 | 31 | public enum PropertyToFieldNameConverter implements Converter { 32 | 33 | INSTANCE; 34 | 35 | public String convert(ElasticsearchPersistentProperty source) { 36 | return source.getFieldName(); 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchMappingContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.mapping; 17 | 18 | import org.springframework.data.mapping.context.AbstractMappingContext; 19 | import org.springframework.data.mapping.model.SimpleTypeHolder; 20 | import org.springframework.data.util.TypeInformation; 21 | 22 | import java.beans.PropertyDescriptor; 23 | import java.lang.reflect.Field; 24 | 25 | /** 26 | * SimpleElasticsearchMappingContext 27 | * 28 | * @author Rizwan Idrees 29 | * @author Mohsin Husen 30 | */ 31 | 32 | public class SimpleElasticsearchMappingContext extends 33 | AbstractMappingContext, ElasticsearchPersistentProperty> { 34 | 35 | @Override 36 | protected SimpleElasticsearchPersistentEntity createPersistentEntity(TypeInformation typeInformation) { 37 | return new SimpleElasticsearchPersistentEntity(typeInformation); 38 | } 39 | 40 | @Override 41 | protected ElasticsearchPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor, 42 | SimpleElasticsearchPersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { 43 | return new SimpleElasticsearchPersistentProperty(field, descriptor, owner, simpleTypeHolder); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.mapping; 17 | 18 | import org.springframework.beans.BeansException; 19 | import org.springframework.context.ApplicationContext; 20 | import org.springframework.context.ApplicationContextAware; 21 | import org.springframework.context.expression.BeanFactoryAccessor; 22 | import org.springframework.context.expression.BeanFactoryResolver; 23 | import org.springframework.data.elasticsearch.annotations.Document; 24 | import org.springframework.data.mapping.model.BasicPersistentEntity; 25 | import org.springframework.data.mapping.model.MappingException; 26 | import org.springframework.data.util.TypeInformation; 27 | import org.springframework.expression.spel.support.StandardEvaluationContext; 28 | import org.springframework.util.Assert; 29 | 30 | import java.util.Locale; 31 | 32 | import static org.springframework.util.StringUtils.hasText; 33 | 34 | /** 35 | * Elasticsearch specific {@link org.springframework.data.mapping.PersistentEntity} implementation holding 36 | * 37 | * @param 38 | * 39 | * @author Rizwan Idrees 40 | * @author Mohsin Husen 41 | */ 42 | public class SimpleElasticsearchPersistentEntity extends BasicPersistentEntity implements 43 | ElasticsearchPersistentEntity, ApplicationContextAware { 44 | 45 | private final StandardEvaluationContext context; 46 | private String indexName; 47 | private String indexType; 48 | 49 | public SimpleElasticsearchPersistentEntity(TypeInformation typeInformation) { 50 | super(typeInformation); 51 | this.context = new StandardEvaluationContext(); 52 | Class clazz = typeInformation.getType(); 53 | if(clazz.isAnnotationPresent(Document.class)){ 54 | Document document = clazz.getAnnotation(Document.class); 55 | Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")"); 56 | this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName(); 57 | this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH); 58 | } 59 | } 60 | 61 | @Override 62 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 63 | context.addPropertyAccessor(new BeanFactoryAccessor()); 64 | context.setBeanResolver(new BeanFactoryResolver(applicationContext)); 65 | context.setRootObject(applicationContext); 66 | } 67 | 68 | @Override 69 | public String getIndexName() { 70 | return indexName; 71 | } 72 | 73 | @Override 74 | public String getIndexType() { 75 | return indexType; 76 | } 77 | 78 | @Override 79 | public void addPersistentProperty(ElasticsearchPersistentProperty property) { 80 | super.addPersistentProperty(property); 81 | if(property.isVersionProperty()){ 82 | Assert.isTrue(property.getType() == Long.class, "Version property should be Long"); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.mapping; 17 | 18 | import org.springframework.data.mapping.Association; 19 | import org.springframework.data.mapping.PersistentEntity; 20 | import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; 21 | import org.springframework.data.mapping.model.SimpleTypeHolder; 22 | 23 | import java.beans.PropertyDescriptor; 24 | import java.lang.reflect.Field; 25 | import java.util.HashSet; 26 | import java.util.Set; 27 | 28 | /** 29 | * Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing 30 | * 31 | * @author Rizwan Idrees 32 | * @author Mohsin Husen 33 | */ 34 | public class SimpleElasticsearchPersistentProperty extends AnnotationBasedPersistentProperty implements 35 | ElasticsearchPersistentProperty { 36 | 37 | private static final Set> SUPPORTED_ID_TYPES = new HashSet>(); 38 | private static final Set SUPPORTED_ID_PROPERTY_NAMES = new HashSet(); 39 | 40 | static { 41 | SUPPORTED_ID_TYPES.add(String.class); 42 | SUPPORTED_ID_PROPERTY_NAMES.add("id"); 43 | SUPPORTED_ID_PROPERTY_NAMES.add("documentId"); 44 | } 45 | 46 | public SimpleElasticsearchPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, 47 | PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { 48 | super(field, propertyDescriptor, owner, simpleTypeHolder); 49 | } 50 | 51 | @Override 52 | public String getFieldName() { 53 | return field.getName(); 54 | } 55 | 56 | @Override 57 | public boolean isIdProperty() { 58 | return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()); 59 | } 60 | 61 | @Override 62 | protected Association createAssociation() { 63 | return null; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | import org.springframework.data.domain.Pageable; 19 | import org.springframework.data.domain.Sort; 20 | import org.springframework.util.Assert; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import static org.apache.commons.collections.CollectionUtils.addAll; 26 | 27 | /** 28 | * AbstractQuery 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | abstract class AbstractQuery implements Query{ 34 | 35 | protected Pageable pageable = DEFAULT_PAGE; 36 | protected Sort sort; 37 | protected List indices = new ArrayList(); 38 | protected List types = new ArrayList(); 39 | protected List fields = new ArrayList(); 40 | 41 | @Override 42 | public Sort getSort() { 43 | return this.sort; 44 | } 45 | 46 | @Override 47 | public Pageable getPageable() { 48 | return this.pageable; 49 | } 50 | 51 | @Override 52 | public final T setPageable(Pageable pageable) { 53 | Assert.notNull(pageable); 54 | this.pageable = pageable; 55 | return (T) this.addSort(pageable.getSort()); 56 | } 57 | 58 | @Override 59 | public void addFields(String...fields) { 60 | addAll(this.fields, fields); 61 | } 62 | 63 | @Override 64 | public List getFields() { 65 | return fields; 66 | } 67 | 68 | @Override 69 | public List getIndices() { 70 | return indices; 71 | } 72 | 73 | @Override 74 | public void addIndices(String... indices) { 75 | addAll(this.indices,indices); 76 | } 77 | 78 | @Override 79 | public void addTypes(String... types) { 80 | addAll(this.types,types); 81 | } 82 | 83 | @Override 84 | public List getTypes() { 85 | return types; 86 | } 87 | 88 | @SuppressWarnings("unchecked") 89 | public final T addSort(Sort sort) { 90 | if (sort == null) { 91 | return (T) this; 92 | } 93 | 94 | if (this.sort == null) { 95 | this.sort = sort; 96 | } else { 97 | this.sort = this.sort.and(sort); 98 | } 99 | 100 | return (T) this; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/CriteriaQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | 19 | import org.springframework.data.domain.Pageable; 20 | import org.springframework.util.Assert; 21 | 22 | /** 23 | * CriteriaQuery 24 | * 25 | * @author Rizwan Idrees 26 | * @author Mohsin Husen 27 | */ 28 | public class CriteriaQuery extends AbstractQuery{ 29 | 30 | private Criteria criteria; 31 | private CriteriaQuery() { 32 | } 33 | 34 | public CriteriaQuery(Criteria criteria) { 35 | this(criteria, null); 36 | } 37 | 38 | public CriteriaQuery(Criteria criteria, Pageable pageable) { 39 | this.criteria = criteria; 40 | this.pageable = pageable; 41 | if (pageable != null) { 42 | this.addSort(pageable.getSort()); 43 | } 44 | } 45 | 46 | public static final Query fromQuery(CriteriaQuery source) { 47 | return fromQuery(source, new CriteriaQuery()); 48 | } 49 | 50 | public static T fromQuery(CriteriaQuery source, T destination) { 51 | if (source == null || destination == null) { 52 | return null; 53 | } 54 | 55 | if (source.getCriteria() != null) { 56 | destination.addCriteria(source.getCriteria()); 57 | } 58 | 59 | if (source.getSort() != null) { 60 | destination.addSort(source.getSort()); 61 | } 62 | 63 | return destination; 64 | } 65 | 66 | @SuppressWarnings("unchecked") 67 | public final T addCriteria(Criteria criteria) { 68 | Assert.notNull(criteria, "Cannot add null criteria."); 69 | if (this.criteria == null) { 70 | this.criteria = criteria; 71 | } else { 72 | this.criteria.and(criteria); 73 | } 74 | return (T) this; 75 | } 76 | 77 | public Criteria getCriteria() { 78 | return this.criteria; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/DeleteQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | 19 | import org.elasticsearch.index.query.QueryBuilder; 20 | /** 21 | * DeleteQuery 22 | * 23 | * @author Rizwan Idrees 24 | * @author Mohsin Husen 25 | */ 26 | public class DeleteQuery{ 27 | 28 | private QueryBuilder query; 29 | 30 | public QueryBuilder getQuery() { 31 | return query; 32 | } 33 | 34 | public void setQuery(QueryBuilder query) { 35 | this.query = query; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/Field.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | /** 19 | * Defines a Field that can be used within a Criteria. 20 | * 21 | * @author Rizwan Idrees 22 | * @author Mohsin Husen 23 | */ 24 | public interface Field { 25 | 26 | /** 27 | * Get the name of the field used in schema.xml of elasticsearch server 28 | * 29 | * @return 30 | */ 31 | String getName(); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/GetQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core.query; 18 | 19 | /** 20 | * GetQuery 21 | * 22 | * @author Rizwan Idrees 23 | * @author Mohsin Husen 24 | */ 25 | public class GetQuery{ 26 | 27 | private String id; 28 | 29 | public String getId() { 30 | return id; 31 | } 32 | 33 | public void setId(String id) { 34 | this.id = id; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/IndexQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core.query; 18 | 19 | /** 20 | * IndexQuery 21 | * 22 | * @author Rizwan Idrees 23 | * @author Mohsin Husen 24 | */ 25 | 26 | public class IndexQuery{ 27 | 28 | private String id; 29 | private Object object; 30 | private Long version; 31 | private String indexName; 32 | private String type; 33 | 34 | public String getId() { 35 | return id; 36 | } 37 | 38 | public void setId(String id) { 39 | this.id = id; 40 | } 41 | 42 | public Object getObject() { 43 | return object; 44 | } 45 | 46 | public void setObject(Object object) { 47 | this.object = object; 48 | } 49 | 50 | public Long getVersion() { 51 | return version; 52 | } 53 | 54 | public void setVersion(Long version) { 55 | this.version = version; 56 | } 57 | 58 | public String getIndexName() { 59 | return indexName; 60 | } 61 | 62 | public void setIndexName(String indexName) { 63 | this.indexName = indexName; 64 | } 65 | 66 | public String getType() { 67 | return type; 68 | } 69 | 70 | public void setType(String type) { 71 | this.type = type; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/MoreLikeThisQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core.query; 18 | 19 | import org.springframework.data.domain.Pageable; 20 | 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import static org.apache.commons.collections.CollectionUtils.addAll; 25 | import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE; 26 | 27 | /** 28 | * MoreLikeThisQuery 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | 34 | public class MoreLikeThisQuery { 35 | 36 | private String id; 37 | private String indexName; 38 | private String type; 39 | private List searchIndices = new ArrayList(); 40 | private List searchTypes = new ArrayList(); 41 | private List fields = new ArrayList(); 42 | private String routing; 43 | private Float percentTermsToMatch; 44 | private Integer minTermFreq; 45 | private Integer maxQueryTerms; 46 | private List stopWords = new ArrayList(); 47 | private Integer minDocFreq; 48 | private Integer maxDocFreq; 49 | private Integer minWordLen; 50 | private Integer maxWordLen; 51 | private Float boostTerms; 52 | private Pageable pageable = DEFAULT_PAGE; 53 | 54 | public String getId() { 55 | return id; 56 | } 57 | 58 | public void setId(String id) { 59 | this.id = id; 60 | } 61 | 62 | public String getIndexName() { 63 | return indexName; 64 | } 65 | 66 | public void setIndexName(String indexName) { 67 | this.indexName = indexName; 68 | } 69 | 70 | public String getType() { 71 | return type; 72 | } 73 | 74 | public void setType(String type) { 75 | this.type = type; 76 | } 77 | 78 | public List getSearchIndices() { 79 | return searchIndices; 80 | } 81 | 82 | public void addSearchIndices(String...searchIndices) { 83 | addAll(this.searchIndices, searchIndices); 84 | } 85 | 86 | public List getSearchTypes() { 87 | return searchTypes; 88 | } 89 | 90 | public void addSearchTypes(String...searchTypes) { 91 | addAll(this.searchTypes, searchTypes); 92 | } 93 | 94 | public List getFields() { 95 | return fields; 96 | } 97 | 98 | public void addFields(String...fields) { 99 | addAll(this.fields,fields); 100 | } 101 | 102 | public String getRouting() { 103 | return routing; 104 | } 105 | 106 | public void setRouting(String routing) { 107 | this.routing = routing; 108 | } 109 | 110 | public Float getPercentTermsToMatch() { 111 | return percentTermsToMatch; 112 | } 113 | 114 | public void setPercentTermsToMatch(Float percentTermsToMatch) { 115 | this.percentTermsToMatch = percentTermsToMatch; 116 | } 117 | 118 | public Integer getMinTermFreq() { 119 | return minTermFreq; 120 | } 121 | 122 | public void setMinTermFreq(Integer minTermFreq) { 123 | this.minTermFreq = minTermFreq; 124 | } 125 | 126 | public Integer getMaxQueryTerms() { 127 | return maxQueryTerms; 128 | } 129 | 130 | public void setMaxQueryTerms(Integer maxQueryTerms) { 131 | this.maxQueryTerms = maxQueryTerms; 132 | } 133 | 134 | public List getStopWords() { 135 | return stopWords; 136 | } 137 | 138 | public void addStopWords(String...stopWords) { 139 | addAll(this.stopWords,stopWords); 140 | } 141 | 142 | public Integer getMinDocFreq() { 143 | return minDocFreq; 144 | } 145 | 146 | public void setMinDocFreq(Integer minDocFreq) { 147 | this.minDocFreq = minDocFreq; 148 | } 149 | 150 | public Integer getMaxDocFreq() { 151 | return maxDocFreq; 152 | } 153 | 154 | public void setMaxDocFreq(Integer maxDocFreq) { 155 | this.maxDocFreq = maxDocFreq; 156 | } 157 | 158 | public Integer getMinWordLen() { 159 | return minWordLen; 160 | } 161 | 162 | public void setMinWordLen(Integer minWordLen) { 163 | this.minWordLen = minWordLen; 164 | } 165 | 166 | public Integer getMaxWordLen() { 167 | return maxWordLen; 168 | } 169 | 170 | public void setMaxWordLen(Integer maxWordLen) { 171 | this.maxWordLen = maxWordLen; 172 | } 173 | 174 | public Float getBoostTerms() { 175 | return boostTerms; 176 | } 177 | 178 | public void setBoostTerms(Float boostTerms) { 179 | this.boostTerms = boostTerms; 180 | } 181 | 182 | public Pageable getPageable() { 183 | return pageable; 184 | } 185 | 186 | public void setPageable(Pageable pageable) { 187 | this.pageable = pageable; 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | 19 | import org.elasticsearch.index.query.FilterBuilder; 20 | import org.elasticsearch.index.query.QueryBuilder; 21 | import org.elasticsearch.search.sort.SortBuilder; 22 | 23 | /** 24 | * NativeSearchQuery 25 | * 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | * @author Artur Konczak 29 | */ 30 | public class NativeSearchQuery extends AbstractQuery implements SearchQuery { 31 | 32 | private QueryBuilder query; 33 | private FilterBuilder filter; 34 | private SortBuilder sort; 35 | 36 | public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, SortBuilder sort) { 37 | this.query = query; 38 | this.filter = filter; 39 | this.sort = sort; 40 | } 41 | 42 | public QueryBuilder getQuery() { 43 | return query; 44 | } 45 | 46 | 47 | public FilterBuilder getFilter() { 48 | return filter; 49 | } 50 | 51 | public SortBuilder getElasticsearchSort() { 52 | return sort; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | import org.elasticsearch.index.query.FilterBuilder; 19 | import org.elasticsearch.index.query.QueryBuilder; 20 | import org.elasticsearch.search.sort.SortBuilder; 21 | import org.springframework.data.domain.Pageable; 22 | 23 | /** 24 | * NativeSearchQuery 25 | * 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | * @author Artur Konczak 29 | */ 30 | 31 | public class NativeSearchQueryBuilder { 32 | 33 | private QueryBuilder queryBuilder; 34 | private FilterBuilder filterBuilder; 35 | private SortBuilder sortBuilder; 36 | private Pageable pageable; 37 | private String[] indices; 38 | private String[] types; 39 | private String[] fields; 40 | 41 | public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder){ 42 | this.queryBuilder = queryBuilder; 43 | return this; 44 | } 45 | 46 | public NativeSearchQueryBuilder withFilter(FilterBuilder filterBuilder){ 47 | this.filterBuilder = filterBuilder; 48 | return this; 49 | } 50 | 51 | public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder){ 52 | this.sortBuilder = sortBuilder; 53 | return this; 54 | } 55 | 56 | public NativeSearchQueryBuilder withPageable(Pageable pageable){ 57 | this.pageable = pageable; 58 | return this; 59 | } 60 | 61 | public NativeSearchQueryBuilder withIndices(String... indices){ 62 | this.indices = indices; 63 | return this; 64 | } 65 | 66 | public NativeSearchQueryBuilder withTypes(String... types){ 67 | this.types = types; 68 | return this; 69 | } 70 | 71 | public NativeSearchQueryBuilder withFields(String... fields){ 72 | this.fields = fields; 73 | return this; 74 | } 75 | 76 | public NativeSearchQuery build(){ 77 | NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder,filterBuilder,sortBuilder); 78 | if(pageable != null){ 79 | nativeSearchQuery.setPageable(pageable); 80 | } 81 | if(indices != null) { 82 | nativeSearchQuery.addIndices(indices); 83 | } 84 | if(types != null) { 85 | nativeSearchQuery.addTypes(types); 86 | } 87 | if(fields != null) { 88 | nativeSearchQuery.addFields(fields); 89 | } 90 | return nativeSearchQuery; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/Query.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core.query; 18 | 19 | 20 | import org.springframework.data.domain.PageRequest; 21 | import org.springframework.data.domain.Pageable; 22 | import org.springframework.data.domain.Sort; 23 | 24 | import java.util.List; 25 | /** 26 | * Query 27 | * 28 | * @author Rizwan Idrees 29 | * @author Mohsin Husen 30 | */ 31 | public interface Query { 32 | 33 | public static final int DEFAULT_PAGE_SIZE = 10; 34 | public static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE); 35 | 36 | 37 | /** 38 | * restrict result to entries on given page. Corresponds to the 'start' and 'rows' parameter in elasticsearch 39 | * 40 | * @param pageable 41 | * @return 42 | */ 43 | T setPageable(Pageable pageable); 44 | 45 | 46 | 47 | /** 48 | * Get filter queries if defined 49 | * 50 | * @return 51 | */ 52 | //List getFilterQueries(); 53 | 54 | /** 55 | * Get page settings if defined 56 | * 57 | * @return 58 | */ 59 | Pageable getPageable(); 60 | 61 | 62 | 63 | /** 64 | * Add {@link org.springframework.data.domain.Sort} to query 65 | * 66 | * @param sort 67 | * @return 68 | */ 69 | T addSort(Sort sort); 70 | 71 | /** 72 | * @return null if not set 73 | */ 74 | Sort getSort(); 75 | 76 | 77 | /** 78 | * Get Indices to be searched 79 | * @return 80 | */ 81 | List getIndices(); 82 | 83 | 84 | /** 85 | * Add Indices to be added as part of search request 86 | * @param indices 87 | */ 88 | void addIndices(String...indices); 89 | 90 | /** 91 | * Add types to be searched 92 | * @param types 93 | */ 94 | void addTypes(String...types); 95 | 96 | /** 97 | * Get types to be searched 98 | * @return 99 | */ 100 | List getTypes(); 101 | 102 | /** 103 | * Add fields to be added as part of search request 104 | * @param fields 105 | */ 106 | void addFields(String...fields); 107 | 108 | 109 | /** 110 | * Get fields to be returned as part of search request 111 | * @return 112 | */ 113 | List getFields(); 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | import org.elasticsearch.index.query.FilterBuilder; 19 | import org.elasticsearch.index.query.QueryBuilder; 20 | import org.elasticsearch.search.sort.SortBuilder; 21 | /** 22 | * NativeSearchQuery 23 | * 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | * @author Artur Konczak 27 | */ 28 | public interface SearchQuery extends Query { 29 | QueryBuilder getQuery(); 30 | FilterBuilder getFilter(); 31 | SortBuilder getElasticsearchSort(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/SimpleField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | /** 19 | * The most trivial implementation of a Field 20 | * 21 | * @author Rizwan Idrees 22 | * @author Mohsin Husen 23 | */ 24 | public class SimpleField implements Field { 25 | 26 | private final String name; 27 | 28 | public SimpleField(String name) { 29 | this.name = name; 30 | } 31 | 32 | @Override 33 | public String getName() { 34 | return this.name; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return this.name; 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/core/query/StringQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.query; 17 | 18 | 19 | import org.springframework.data.domain.Pageable; 20 | import org.springframework.data.domain.Sort; 21 | 22 | /** 23 | * StringQuery 24 | * 25 | * @author Rizwan Idrees 26 | * @author Mohsin Husen 27 | */ 28 | public class StringQuery extends AbstractQuery{ 29 | 30 | private String source; 31 | 32 | public StringQuery(String source) { 33 | this.source = source; 34 | } 35 | 36 | public StringQuery(String source, Pageable pageable) { 37 | this.source = source; 38 | this.pageable = pageable; 39 | } 40 | 41 | public StringQuery(String source, Pageable pageable, Sort sort) { 42 | this.pageable = pageable; 43 | this.sort = sort; 44 | this.source = source; 45 | } 46 | 47 | 48 | public String getSource() { 49 | return source; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/ElasticsearchCrudRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository; 17 | 18 | import org.springframework.data.repository.PagingAndSortingRepository; 19 | 20 | import java.io.Serializable; 21 | import java.util.List; 22 | 23 | /** 24 | * @param 25 | * @param 26 | * 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | public interface ElasticsearchCrudRepository extends PagingAndSortingRepository { 31 | 32 | List save(List entities); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/ElasticsearchRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository; 17 | 18 | import org.elasticsearch.index.query.QueryBuilder; 19 | import org.springframework.data.domain.Page; 20 | import org.springframework.data.domain.Pageable; 21 | import org.springframework.data.elasticsearch.core.query.SearchQuery; 22 | import org.springframework.data.repository.NoRepositoryBean; 23 | 24 | import java.io.Serializable; 25 | 26 | /** 27 | * @param 28 | * @param 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | @NoRepositoryBean 34 | public interface ElasticsearchRepository extends ElasticsearchCrudRepository { 35 | 36 | S index(S entity); 37 | 38 | Iterable search(QueryBuilder query); 39 | 40 | Page search(QueryBuilder query, Pageable pageable); 41 | 42 | Page search(SearchQuery searchQuery); 43 | 44 | Page searchSimilar(T entity); 45 | 46 | Page searchSimilar(T entity, Pageable pageable); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.cdi; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory; 20 | import org.springframework.data.repository.cdi.CdiRepositoryBean; 21 | import org.springframework.util.Assert; 22 | 23 | import javax.enterprise.context.spi.CreationalContext; 24 | import javax.enterprise.inject.spi.Bean; 25 | import javax.enterprise.inject.spi.BeanManager; 26 | import java.lang.annotation.Annotation; 27 | import java.util.Set; 28 | 29 | /** 30 | * Uses CdiRepositoryBean to create ElasticsearchRepository instances. 31 | * 32 | * @author Rizwan Idrees 33 | * @author Mohsin Husen 34 | */ 35 | public class ElasticsearchRepositoryBean extends CdiRepositoryBean { 36 | 37 | private final Bean elasticsearchOperationsBean; 38 | 39 | public ElasticsearchRepositoryBean(Bean operations, Set qualifiers, Class repositoryType, 40 | BeanManager beanManager) { 41 | super(qualifiers, repositoryType, beanManager); 42 | 43 | Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations."); 44 | this.elasticsearchOperationsBean = operations; 45 | } 46 | 47 | @Override 48 | protected T create(CreationalContext creationalContext, Class repositoryType) { 49 | ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean, ElasticsearchOperations.class); 50 | return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType); 51 | } 52 | 53 | @Override 54 | public Class getScope() { 55 | return elasticsearchOperationsBean.getScope(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/cdi/ElasticsearchRepositoryExtension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.cdi; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; 20 | 21 | import javax.enterprise.event.Observes; 22 | import javax.enterprise.inject.UnsatisfiedResolutionException; 23 | import javax.enterprise.inject.spi.AfterBeanDiscovery; 24 | import javax.enterprise.inject.spi.Bean; 25 | import javax.enterprise.inject.spi.BeanManager; 26 | import javax.enterprise.inject.spi.ProcessBean; 27 | import java.lang.annotation.Annotation; 28 | import java.lang.reflect.Type; 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | import java.util.Map.Entry; 32 | import java.util.Set; 33 | 34 | /** 35 | * ElasticsearchRepositoryExtension 36 | * 37 | * @author Rizwan Idrees 38 | * @author Mohsin Husen 39 | */ 40 | 41 | public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupport { 42 | 43 | private final Map> elasticsearchOperationsMap = new HashMap>(); 44 | 45 | @SuppressWarnings("unchecked") 46 | void processBean(@Observes ProcessBean processBean) { 47 | Bean bean = processBean.getBean(); 48 | for (Type type : bean.getTypes()) { 49 | if (type instanceof Class && ElasticsearchOperations.class.isAssignableFrom((Class) type)) { 50 | elasticsearchOperationsMap.put(bean.getQualifiers().toString(), ((Bean) bean)); 51 | } 52 | } 53 | } 54 | 55 | void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { 56 | for (Entry, Set> entry : getRepositoryTypes()) { 57 | 58 | Class repositoryType = entry.getKey(); 59 | Set qualifiers = entry.getValue(); 60 | 61 | Bean repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); 62 | afterBeanDiscovery.addBean(repositoryBean); 63 | } 64 | } 65 | 66 | private Bean createRepositoryBean(Class repositoryType, Set qualifiers, BeanManager beanManager) { 67 | Bean elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers.toString()); 68 | 69 | if (elasticsearchOperationsBean == null) { 70 | throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", 71 | ElasticsearchOperations.class.getName(), qualifiers)); 72 | } 73 | return new ElasticsearchRepositoryBean(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/config/ElasticsearchRepositoriesRegistrar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.config; 17 | 18 | import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport; 19 | import org.springframework.data.repository.config.RepositoryConfigurationExtension; 20 | 21 | import java.lang.annotation.Annotation; 22 | 23 | /** 24 | * {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar} implementation to trigger configuration of the {@link EnableElasticsearchRepositories} 25 | * annotation. 26 | * 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | class ElasticsearchRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport { 31 | 32 | /* 33 | * (non-Javadoc) 34 | * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getAnnotation() 35 | */ 36 | @Override 37 | protected Class getAnnotation() { 38 | return EnableElasticsearchRepositories.class; 39 | } 40 | 41 | /* 42 | * (non-Javadoc) 43 | * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension() 44 | */ 45 | @Override 46 | protected RepositoryConfigurationExtension getExtension() { 47 | return new ElasticsearchRepositoryConfigExtension(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/config/ElasticsearchRepositoryConfigExtension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.config; 17 | 18 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; 19 | import org.springframework.core.annotation.AnnotationAttributes; 20 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean; 21 | import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource; 22 | import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport; 23 | import org.springframework.data.repository.config.XmlRepositoryConfigurationSource; 24 | import org.w3c.dom.Element; 25 | 26 | 27 | 28 | /** 29 | * {@link org.springframework.data.repository.config.RepositoryConfigurationExtension} implementation to configure Elasticsearch repository configuration support, 30 | * evaluating the {@link EnableElasticsearchRepositories} annotation or the equivalent XML element. 31 | * 32 | * @author Rizwan Idrees 33 | * @author Mohsin Husen 34 | */ 35 | public class ElasticsearchRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport { 36 | 37 | /* 38 | * (non-Javadoc) 39 | * @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryClassName() 40 | */ 41 | @Override 42 | public String getRepositoryFactoryClassName() { 43 | return ElasticsearchRepositoryFactoryBean.class.getName(); 44 | } 45 | 46 | /* 47 | * (non-Javadoc) 48 | * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix() 49 | */ 50 | @Override 51 | protected String getModulePrefix() { 52 | return "elasticsearch"; 53 | } 54 | 55 | /* 56 | * (non-Javadoc) 57 | * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource) 58 | */ 59 | @Override 60 | public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) { 61 | 62 | AnnotationAttributes attributes = config.getAttributes(); 63 | builder.addPropertyReference("elasticsearchOperations", attributes.getString("elasticsearchTemplateRef")); 64 | } 65 | 66 | /* 67 | * (non-Javadoc) 68 | * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.XmlRepositoryConfigurationSource) 69 | */ 70 | @Override 71 | public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) { 72 | 73 | Element element = config.getElement(); 74 | builder.addPropertyReference("elasticsearchOperations", element.getAttribute("elasticsearch-template-ref")); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/config/EnableElasticsearchRepositories.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.config; 17 | 18 | import org.springframework.context.annotation.ComponentScan.Filter; 19 | import org.springframework.context.annotation.Import; 20 | import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; 21 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean; 22 | import org.springframework.data.repository.query.QueryLookupStrategy.Key; 23 | 24 | import java.lang.annotation.*; 25 | 26 | /** 27 | * Annotation to enable Elasticsearch repositories. Will scan the package of the annotated configuration class for Spring Data 28 | * repositories by default. 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | @Target(ElementType.TYPE) 34 | @Retention(RetentionPolicy.RUNTIME) 35 | @Documented 36 | @Inherited 37 | @Import(ElasticsearchRepositoriesRegistrar.class) 38 | public @interface EnableElasticsearchRepositories { 39 | 40 | /** 41 | * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.: 42 | * {@code @EnableElasticsearchRepositories("org.my.pkg")} instead of {@code @EnableElasticsearchRepositories(basePackages="org.my.pkg")}. 43 | */ 44 | String[] value() default {}; 45 | 46 | /** 47 | * Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this 48 | * attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names. 49 | */ 50 | String[] basePackages() default {}; 51 | 52 | /** 53 | * Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The 54 | * package of each class specified will be scanned. Consider creating a special no-op marker class or interface in 55 | * each package that serves no purpose other than being referenced by this attribute. 56 | */ 57 | Class[] basePackageClasses() default {}; 58 | 59 | /** 60 | * Specifies which types are eligible for component scanning. Further narrows the set of candidate components from 61 | * everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters. 62 | */ 63 | Filter[] includeFilters() default {}; 64 | 65 | /** 66 | * Specifies which types are not eligible for component scanning. 67 | */ 68 | Filter[] excludeFilters() default {}; 69 | 70 | /** 71 | * Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So 72 | * for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning 73 | * for {@code PersonRepositoryImpl}. 74 | * 75 | * @return 76 | */ 77 | String repositoryImplementationPostfix() default "Impl"; 78 | 79 | /** 80 | * Configures the location of where to find the Spring Data named queries properties file. Will default to 81 | * {@code META-INFO/elasticsearch-named-queries.properties}. 82 | * 83 | * @return 84 | */ 85 | String namedQueriesLocation() default ""; 86 | 87 | /** 88 | * Returns the key of the {@link org.springframework.data.repository.query.QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to 89 | * {@link org.springframework.data.repository.query.QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}. 90 | * 91 | * @return 92 | */ 93 | Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND; 94 | 95 | /** 96 | * Returns the {@link org.springframework.beans.factory.FactoryBean} class to be used for each repository instance. Defaults to 97 | * {@link ElasticsearchRepositoryFactoryBean}. 98 | * 99 | * @return 100 | */ 101 | Class repositoryFactoryBeanClass() default ElasticsearchRepositoryFactoryBean.class; 102 | 103 | // Elasticsearch specific configuration 104 | 105 | /** 106 | * Configures the name of the {@link ElasticsearchTemplate} bean definition to be used to create repositories discovered 107 | * through this annotation. Defaults to {@code elasticsearchTemplate}. 108 | * 109 | * @return 110 | */ 111 | String elasticsearchTemplateRef() default "elasticsearchTemplate"; 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/query/AbstractElasticsearchRepositoryQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.query; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | import org.springframework.data.repository.query.QueryMethod; 20 | import org.springframework.data.repository.query.RepositoryQuery; 21 | 22 | /** 23 | * AbstractElasticsearchRepositoryQuery 24 | * 25 | * @author Rizwan Idrees 26 | * @author Mohsin Husen 27 | */ 28 | 29 | public abstract class AbstractElasticsearchRepositoryQuery implements RepositoryQuery { 30 | 31 | protected ElasticsearchQueryMethod queryMethod; 32 | protected ElasticsearchOperations elasticsearchOperations; 33 | 34 | public AbstractElasticsearchRepositoryQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations) { 35 | this.queryMethod = queryMethod; 36 | this.elasticsearchOperations = elasticsearchOperations; 37 | } 38 | 39 | @Override 40 | public QueryMethod getQueryMethod() { 41 | return queryMethod; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchPartQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.query; 17 | 18 | 19 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 20 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 21 | import org.springframework.data.elasticsearch.core.query.CriteriaQuery; 22 | import org.springframework.data.elasticsearch.repository.query.parser.ElasticsearchQueryCreator; 23 | import org.springframework.data.mapping.context.MappingContext; 24 | import org.springframework.data.repository.query.ParametersParameterAccessor; 25 | import org.springframework.data.repository.query.parser.PartTree; 26 | 27 | /** 28 | * ElasticsearchPartQuery 29 | * 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery{ 34 | 35 | private final PartTree tree; 36 | private final MappingContext mappingContext; 37 | 38 | 39 | public ElasticsearchPartQuery(ElasticsearchQueryMethod method, ElasticsearchOperations elasticsearchOperations) { 40 | super(method, elasticsearchOperations); 41 | this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType()); 42 | this.mappingContext = elasticsearchOperations.getElasticsearchConverter().getMappingContext(); 43 | } 44 | 45 | @Override 46 | public Object execute(Object[] parameters) { 47 | ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); 48 | CriteriaQuery query = createQuery(accessor); 49 | if(queryMethod.isPageQuery()){ 50 | query.setPageable(accessor.getPageable()); 51 | return elasticsearchOperations.queryForPage(query, queryMethod.getEntityInformation().getJavaType()); 52 | } else if (queryMethod.isCollectionQuery()) { 53 | if(accessor.getPageable() != null){ 54 | query.setPageable(accessor.getPageable()); 55 | } 56 | return elasticsearchOperations.queryForList(query,queryMethod.getEntityInformation().getJavaType()); 57 | } 58 | return elasticsearchOperations.queryForObject(query, queryMethod.getEntityInformation().getJavaType()); 59 | } 60 | 61 | public CriteriaQuery createQuery(ParametersParameterAccessor accessor) { 62 | return new ElasticsearchQueryCreator(tree, accessor, mappingContext).createQuery(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchQueryMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.query; 17 | 18 | import org.springframework.core.annotation.AnnotationUtils; 19 | import org.springframework.data.elasticsearch.annotations.Query; 20 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformation; 21 | import org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformationCreator; 22 | import org.springframework.data.repository.core.RepositoryMetadata; 23 | import org.springframework.data.repository.query.QueryMethod; 24 | import org.springframework.util.StringUtils; 25 | 26 | import java.lang.reflect.Method; 27 | 28 | /** 29 | * ElasticsearchQueryMethod 30 | * 31 | * @author Rizwan Idrees 32 | * @author Mohsin Husen 33 | */ 34 | public class ElasticsearchQueryMethod extends QueryMethod { 35 | 36 | private final ElasticsearchEntityInformation entityInformation; 37 | private Method method; 38 | 39 | public ElasticsearchQueryMethod(Method method, RepositoryMetadata metadata, ElasticsearchEntityInformationCreator elasticsearchEntityInformationCreator) { 40 | super(method, metadata); 41 | this.entityInformation = elasticsearchEntityInformationCreator.getEntityInformation(metadata.getReturnedDomainClass(method)); 42 | this.method = method; 43 | } 44 | 45 | public boolean hasAnnotatedQuery() { 46 | return getQueryAnnotation() != null; 47 | } 48 | 49 | public String getAnnotatedQuery() { 50 | String query = (String) AnnotationUtils.getValue(getQueryAnnotation(), "value"); 51 | return StringUtils.hasText(query) ? query : null; 52 | } 53 | 54 | private Query getQueryAnnotation() { 55 | return this.method.getAnnotation(Query.class); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.query; 17 | 18 | 19 | import org.springframework.core.convert.support.GenericConversionService; 20 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 21 | import org.springframework.data.elasticsearch.core.convert.DateTimeConverters; 22 | import org.springframework.data.elasticsearch.core.query.StringQuery; 23 | import org.springframework.data.repository.query.ParametersParameterAccessor; 24 | import org.springframework.util.Assert; 25 | 26 | import java.util.regex.Matcher; 27 | import java.util.regex.Pattern; 28 | 29 | /** 30 | * ElasticsearchStringQuery 31 | * 32 | * @author Rizwan Idrees 33 | * @author Mohsin Husen 34 | */ 35 | public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery{ 36 | 37 | private static final Pattern PARAMETER_PLACEHOLDER = Pattern.compile("\\?(\\d+)"); 38 | private String query; 39 | 40 | private final GenericConversionService conversionService = new GenericConversionService(); 41 | 42 | { 43 | if (!conversionService.canConvert(java.util.Date.class, String.class)) { 44 | conversionService.addConverter(DateTimeConverters.JavaDateConverter.INSTANCE); 45 | } 46 | if (!conversionService.canConvert(org.joda.time.ReadableInstant.class, String.class)) { 47 | conversionService.addConverter(DateTimeConverters.JodaDateTimeConverter.INSTANCE); 48 | } 49 | if (!conversionService.canConvert(org.joda.time.LocalDateTime.class, String.class)) { 50 | conversionService.addConverter(DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE); 51 | } 52 | 53 | } 54 | 55 | public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations, String query) { 56 | super(queryMethod, elasticsearchOperations); 57 | Assert.notNull(query, "Query cannot be empty"); 58 | this.query = query; 59 | } 60 | 61 | @Override 62 | public Object execute(Object[] parameters) { 63 | ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); 64 | StringQuery stringQuery = createQuery(accessor); 65 | if(queryMethod.isPageQuery()){ 66 | stringQuery.setPageable(accessor.getPageable()); 67 | return elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType()); 68 | } else if(queryMethod.isCollectionQuery()) { 69 | if(accessor.getPageable() != null) { 70 | stringQuery.setPageable(accessor.getPageable()); 71 | } 72 | return elasticsearchOperations.queryForList(stringQuery,queryMethod.getEntityInformation().getJavaType()); 73 | } 74 | 75 | return elasticsearchOperations.queryForObject(stringQuery, queryMethod.getEntityInformation().getJavaType()); 76 | } 77 | 78 | 79 | protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) { 80 | String queryString = replacePlaceholders(this.query, parameterAccessor); 81 | return new StringQuery(queryString); 82 | } 83 | 84 | private String replacePlaceholders(String input, ParametersParameterAccessor accessor) { 85 | Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input); 86 | String result = input; 87 | while (matcher.find()) { 88 | String group = matcher.group(); 89 | int index = Integer.parseInt(matcher.group(1)); 90 | result = result.replace(group, getParameterWithIndex(accessor, index)); 91 | } 92 | return result; 93 | } 94 | 95 | private String getParameterWithIndex(ParametersParameterAccessor accessor, int index) { 96 | Object parameter = accessor.getBindableValue(index); 97 | if (parameter == null) { 98 | return "null"; 99 | } 100 | if (conversionService.canConvert(parameter.getClass(), String.class)) { 101 | return conversionService.convert(parameter, String.class); 102 | } 103 | return parameter.toString(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import org.springframework.data.repository.core.EntityInformation; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * @param 24 | * @param 25 | * 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | */ 29 | public interface ElasticsearchEntityInformation extends EntityInformation { 30 | 31 | String getIdAttribute(); 32 | String getIndexName(); 33 | String getType(); 34 | Long getVersion(T entity); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import java.io.Serializable; 19 | 20 | /** 21 | * ElasticsearchEntityInformationCreator 22 | * 23 | * @author Rizwan Idrees 24 | * @author Mohsin Husen 25 | */ 26 | public interface ElasticsearchEntityInformationCreator { 27 | 28 | ElasticsearchEntityInformation getEntityInformation(Class domainClass); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; 19 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 20 | import org.springframework.data.mapping.context.MappingContext; 21 | import org.springframework.util.Assert; 22 | 23 | import java.io.Serializable; 24 | 25 | /** 26 | * ElasticsearchEntityInformationCreatorImpl 27 | * 28 | * @author Rizwan Idrees 29 | * @author Mohsin Husen 30 | */ 31 | public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchEntityInformationCreator { 32 | 33 | private final MappingContext, ElasticsearchPersistentProperty> mappingContext; 34 | 35 | public ElasticsearchEntityInformationCreatorImpl( 36 | MappingContext, ElasticsearchPersistentProperty> mappingContext) { 37 | Assert.notNull(mappingContext); 38 | this.mappingContext = mappingContext; 39 | } 40 | 41 | @Override 42 | @SuppressWarnings("unchecked") 43 | public ElasticsearchEntityInformation getEntityInformation(Class domainClass) { 44 | ElasticsearchPersistentEntity persistentEntity = (ElasticsearchPersistentEntity) mappingContext 45 | .getPersistentEntity(domainClass); 46 | return new MappingElasticsearchEntityInformation(persistentEntity); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | import org.springframework.data.repository.Repository; 20 | import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; 21 | import org.springframework.data.repository.core.support.RepositoryFactorySupport; 22 | import org.springframework.util.Assert; 23 | 24 | import java.io.Serializable; 25 | 26 | /** 27 | * Spring {@link org.springframework.beans.factory.FactoryBean} implementation to ease container based configuration for XML namespace and JavaConfig. 28 | * 29 | * @author Rizwan Idrees 30 | * @author Mohsin Husen 31 | */ 32 | public class ElasticsearchRepositoryFactoryBean, S, ID extends Serializable> extends 33 | RepositoryFactoryBeanSupport { 34 | 35 | private ElasticsearchOperations operations; 36 | 37 | /** 38 | * Configures the {@link ElasticsearchOperations} to be used to create Elasticsearch repositories. 39 | * 40 | * @param operations the operations to set 41 | */ 42 | public void setElasticsearchOperations(ElasticsearchOperations operations) { 43 | Assert.notNull(operations); 44 | this.operations = operations; 45 | } 46 | 47 | /* 48 | * (non-Javadoc) 49 | * @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#afterPropertiesSet() 50 | */ 51 | @Override 52 | public void afterPropertiesSet() { 53 | super.afterPropertiesSet(); 54 | Assert.notNull(operations, "ElasticsearchOperations must be configured!"); 55 | } 56 | 57 | 58 | @Override 59 | protected RepositoryFactorySupport createRepositoryFactory() { 60 | return new ElasticsearchRepositoryFactory(operations); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import java.io.Serializable; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; 23 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 24 | import org.springframework.data.mapping.model.BeanWrapper; 25 | import org.springframework.data.repository.core.support.AbstractEntityInformation; 26 | import org.springframework.util.Assert; 27 | 28 | /** 29 | * Elasticsearch specific implementation of {@link org.springframework.data.repository.core.support.AbstractEntityInformation} 30 | * 31 | * @param 32 | * @param 33 | * 34 | * @author Rizwan Idrees 35 | * @author Mohsin Husen 36 | * @author Ryan Henszey 37 | */ 38 | public class MappingElasticsearchEntityInformation extends AbstractEntityInformation 39 | implements ElasticsearchEntityInformation { 40 | 41 | private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class); 42 | private final ElasticsearchPersistentEntity entityMetadata; 43 | private final String indexName; 44 | private final String type; 45 | private Class idClass; 46 | 47 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity entity) { 48 | this(entity, null, null); 49 | } 50 | 51 | public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity entity, String indexName, String type) { 52 | super(entity.getType()); 53 | this.entityMetadata = entity; 54 | this.indexName = indexName; 55 | this.type = type; 56 | this.idClass = entity.getIdProperty().getType(); 57 | } 58 | 59 | @SuppressWarnings("unchecked") 60 | @Override 61 | public ID getId(T entity) { 62 | ElasticsearchPersistentProperty id = entityMetadata.getIdProperty(); 63 | try { 64 | return (ID) BeanWrapper.create(entity, null).getProperty(id); 65 | } catch (Exception e) { 66 | throw new IllegalStateException("ID could not be resolved", e); 67 | } 68 | } 69 | 70 | @SuppressWarnings("unchecked") 71 | @Override 72 | public Class getIdType() { 73 | return (Class)idClass; 74 | } 75 | 76 | @Override 77 | public String getIdAttribute() { 78 | Assert.notNull(entityMetadata.getIdProperty(),"Unable to identify 'id' property in class " + entityMetadata.getType().getSimpleName() +". Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId' "); 79 | return entityMetadata.getIdProperty().getFieldName(); 80 | } 81 | 82 | @Override 83 | public String getIndexName() { 84 | return indexName != null? indexName : entityMetadata.getIndexName(); 85 | } 86 | 87 | @Override 88 | public String getType() { 89 | return type != null? type : entityMetadata.getIndexType(); 90 | } 91 | 92 | @Override 93 | public Long getVersion(T entity) { 94 | ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty(); 95 | try { 96 | if(versionProperty != null){ 97 | return (Long) BeanWrapper.create(entity, null).getProperty(versionProperty); 98 | } 99 | } catch (Exception e) { 100 | throw new IllegalStateException("failed to load version field", e); 101 | } 102 | return null; 103 | } 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/NumberKeyedRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | 20 | /** 21 | * Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory} 22 | * 23 | * 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | * @author Ryan Henszey 27 | */ 28 | public class NumberKeyedRepository extends AbstractElasticsearchRepository { 29 | 30 | 31 | public NumberKeyedRepository() { 32 | super(); 33 | } 34 | 35 | public NumberKeyedRepository(ElasticsearchEntityInformation metadata,ElasticsearchOperations elasticsearchOperations) { 36 | super(metadata, elasticsearchOperations); 37 | } 38 | 39 | public NumberKeyedRepository(ElasticsearchOperations elasticsearchOperations) { 40 | super(elasticsearchOperations); 41 | } 42 | 43 | 44 | 45 | @Override 46 | protected String stringIdRepresentation(ID id) { 47 | return String.valueOf(id); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 19 | 20 | /** 21 | * Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory} 22 | * 23 | * 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | * @author Ryan Henszey 27 | */ 28 | public class SimpleElasticsearchRepository extends AbstractElasticsearchRepository { 29 | 30 | public SimpleElasticsearchRepository() { 31 | super(); 32 | } 33 | 34 | public SimpleElasticsearchRepository(ElasticsearchEntityInformation metadata,ElasticsearchOperations elasticsearchOperations) { 35 | super(metadata, elasticsearchOperations); 36 | } 37 | 38 | public SimpleElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) { 39 | super(elasticsearchOperations); 40 | } 41 | 42 | @Override 43 | protected String stringIdRepresentation(String id) { 44 | return id; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension: -------------------------------------------------------------------------------- 1 | org.springframework.data.elasticsearch.repository.cdi.ElasticsearchRepositoryExtension -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.handlers: -------------------------------------------------------------------------------- 1 | http\://www.springframework.org/schema/data/elasticsearch=org.springframework.data.elasticsearch.config.ElasticsearchNamespaceHandler 2 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.schemas: -------------------------------------------------------------------------------- 1 | http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd 2 | http\://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd=org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd 3 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.tooling: -------------------------------------------------------------------------------- 1 | # Tooling related information for the Elasticsearch namespace 2 | http\://www.springframework.org/schema/data/elasticsearch@name=Elasticsearch Namespace 3 | http\://www.springframework.org/schema/data/elasticsearch@prefix=elasticsearch 4 | http\://www.springframework.org/schema/data/elasticsearch@icon=org/springframework/jdbc/config/spring-jdbc.gif 5 | -------------------------------------------------------------------------------- /src/main/resources/changelog.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SpringDataElasticsearchDevs/spring-data-elasticsearch/a73fa6142184ae4332b65b4be1f65232391e7dfa/src/main/resources/changelog.txt -------------------------------------------------------------------------------- /src/main/resources/notice.txt: -------------------------------------------------------------------------------- 1 | Spring Data Elasticsearch 2 | 3 | This product is licensed to you under the Apache License, Version 2.0 (the "License"). 4 | You may not use this product except in compliance with the License. 5 | 6 | This product may include a number of subcomponents with 7 | separate copyright notices and license terms. Your use of the source 8 | code for the these subcomponents is subject to the terms and 9 | conditions of the subcomponent's license, as noted in the LICENSE file. -------------------------------------------------------------------------------- /src/main/resources/org/springframework/data/elasticsearch/config/spring-elasticsearch-1.0.xsd: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/Author.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | /** 18 | * @author Rizwan Idrees 19 | * @author Mohsin Husen 20 | */ 21 | public class Author { 22 | 23 | private String id; 24 | private String name; 25 | 26 | public String getId() { 27 | return id; 28 | } 29 | 30 | public void setId(String id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/Book.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | import org.springframework.data.annotation.Id; 19 | import org.springframework.data.elasticsearch.annotations.Document; 20 | /** 21 | * @author Rizwan Idrees 22 | * @author Mohsin Husen 23 | */ 24 | @Document(indexName = "book",type = "book") 25 | public class Book { 26 | 27 | @Id 28 | private String id; 29 | private String name; 30 | private Author author; 31 | 32 | public String getId() { 33 | return id; 34 | } 35 | 36 | public void setId(String id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public Author getAuthor() { 49 | return author; 50 | } 51 | 52 | public void setAuthor(Author author) { 53 | this.author = author; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/DoubleIDEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | import org.springframework.data.annotation.Id; 19 | import org.springframework.data.annotation.Version; 20 | import org.springframework.data.elasticsearch.annotations.Document; 21 | 22 | 23 | @Document(indexName = "double-keyed-entity", type = "double-keyed-entity") 24 | public class DoubleIDEntity { 25 | 26 | 27 | @Id 28 | private Double id; 29 | private String type; 30 | private String message; 31 | @Version 32 | private Long version; 33 | 34 | public Double getId() { 35 | return id; 36 | } 37 | 38 | public void setId(Double id) { 39 | this.id = id; 40 | } 41 | 42 | public String getType() { 43 | return type; 44 | } 45 | 46 | public void setType(String type) { 47 | this.type = type; 48 | } 49 | 50 | public String getMessage() { 51 | return message; 52 | } 53 | 54 | public void setMessage(String message) { 55 | this.message = message; 56 | } 57 | 58 | public Long getVersion() { 59 | return version; 60 | } 61 | 62 | public void setVersion(Long version) { 63 | this.version = version; 64 | } 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/IntegerIDEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | import java.math.BigInteger; 19 | 20 | import org.springframework.data.annotation.Id; 21 | import org.springframework.data.annotation.Version; 22 | import org.springframework.data.elasticsearch.annotations.Document; 23 | 24 | 25 | @Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity") 26 | public class IntegerIDEntity { 27 | 28 | 29 | @Id 30 | private Integer id; 31 | private String type; 32 | private String message; 33 | @Version 34 | private Long version; 35 | 36 | public Integer getId() { 37 | return id; 38 | } 39 | 40 | public void setId(Integer id) { 41 | this.id = id; 42 | } 43 | 44 | public String getType() { 45 | return type; 46 | } 47 | 48 | public void setType(String type) { 49 | this.type = type; 50 | } 51 | 52 | public String getMessage() { 53 | return message; 54 | } 55 | 56 | public void setMessage(String message) { 57 | this.message = message; 58 | } 59 | 60 | public Long getVersion() { 61 | return version; 62 | } 63 | 64 | public void setVersion(Long version) { 65 | this.version = version; 66 | } 67 | 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/NestedObjectTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.springframework.data.elasticsearch.repositories.SampleElasticSearchBookRepository; 22 | import org.springframework.test.context.ContextConfiguration; 23 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 24 | 25 | import javax.annotation.Resource; 26 | 27 | 28 | import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; 29 | import static org.hamcrest.Matchers.is; 30 | import static org.hamcrest.Matchers.notNullValue; 31 | import static org.junit.Assert.assertThat; 32 | /** 33 | * @author Rizwan Idrees 34 | * @author Mohsin Husen 35 | */ 36 | @RunWith(SpringJUnit4ClassRunner.class) 37 | @ContextConfiguration("classpath:/repository-test-nested-object.xml") 38 | public class NestedObjectTest{ 39 | 40 | @Resource 41 | private SampleElasticSearchBookRepository repository; 42 | 43 | 44 | @Test 45 | public void shouldIndexNestedObject(){ 46 | //given 47 | String id = randomAlphanumeric(5); 48 | Book book = new Book(); 49 | book.setId(id); 50 | book.setName("xyz"); 51 | Author author = new Author(); 52 | author.setId("1"); 53 | author.setName("ABC"); 54 | book.setAuthor(author); 55 | //when 56 | repository.save(book); 57 | //then 58 | assertThat(repository.findOne(id), is(notNullValue())); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/NonDocumentEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | 19 | import org.springframework.data.annotation.Id; 20 | /** 21 | * @author Rizwan Idrees 22 | * @author Mohsin Husen 23 | */ 24 | public class NonDocumentEntity { 25 | 26 | @Id 27 | private String someId; 28 | private String someField1; 29 | private String someField2; 30 | 31 | public String getSomeField1() { 32 | return someField1; 33 | } 34 | 35 | public void setSomeField1(String someField1) { 36 | this.someField1 = someField1; 37 | } 38 | 39 | public String getSomeField2() { 40 | return someField2; 41 | } 42 | 43 | public void setSomeField2(String someField2) { 44 | this.someField2 = someField2; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/NonDocumentEntityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch; 17 | 18 | 19 | import org.junit.Test; 20 | import org.springframework.beans.factory.BeanCreationException; 21 | import org.springframework.context.support.ClassPathXmlApplicationContext; 22 | import org.springframework.data.elasticsearch.repositories.NonDocumentEntityRepository; 23 | /** 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | */ 27 | public class NonDocumentEntityTest { 28 | 29 | 30 | @Test(expected = BeanCreationException.class) 31 | public void shouldNotInitialiseRepositoryWithNonDocument(){ 32 | //when 33 | ClassPathXmlApplicationContext ctx = 34 | new ClassPathXmlApplicationContext("/repository-non-document-entity.xml"); 35 | ctx.getBean(NonDocumentEntityRepository.class); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/SampleEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch; 18 | 19 | import org.apache.commons.lang.builder.EqualsBuilder; 20 | import org.apache.commons.lang.builder.HashCodeBuilder; 21 | import org.springframework.data.annotation.Id; 22 | import org.springframework.data.annotation.Version; 23 | import org.springframework.data.elasticsearch.annotations.Document; 24 | 25 | /** 26 | * @author Rizwan Idrees 27 | * @author Mohsin Husen 28 | */ 29 | @Document(indexName = "test-index", type = "test-type") 30 | public class SampleEntity { 31 | 32 | @Id 33 | private String id; 34 | private String type; 35 | private String message; 36 | private int rate; 37 | private boolean available; 38 | @Version 39 | private Long version; 40 | 41 | public String getId() { 42 | return id; 43 | } 44 | 45 | public void setId(String id) { 46 | this.id = id; 47 | } 48 | 49 | public String getType() { 50 | return type; 51 | } 52 | 53 | public void setType(String type) { 54 | this.type = type; 55 | } 56 | 57 | public String getMessage() { 58 | return message; 59 | } 60 | 61 | public void setMessage(String message) { 62 | this.message = message; 63 | } 64 | 65 | public int getRate() { 66 | return rate; 67 | } 68 | 69 | public void setRate(int rate) { 70 | this.rate = rate; 71 | } 72 | 73 | public boolean isAvailable() { 74 | return available; 75 | } 76 | 77 | public void setAvailable(boolean available) { 78 | this.available = available; 79 | } 80 | 81 | public Long getVersion() { 82 | return version; 83 | } 84 | 85 | public void setVersion(Long version) { 86 | this.version = version; 87 | } 88 | 89 | @Override 90 | public boolean equals(Object obj) { 91 | if (!(obj instanceof SampleEntity)) { 92 | return false; 93 | } 94 | if (this == obj) { 95 | return true; 96 | } 97 | SampleEntity rhs = (SampleEntity) obj; 98 | return new EqualsBuilder().append(this.id, rhs.id) 99 | .append(this.type, rhs.type) 100 | .append(this.message, rhs.message) 101 | .append(this.rate,rhs.rate) 102 | .append(this.available,rhs.available) 103 | .append(this.version,rhs.version) 104 | .isEquals(); 105 | } 106 | 107 | @Override 108 | public int hashCode() { 109 | return new HashCodeBuilder() 110 | .append(id) 111 | .append(type) 112 | .append(message) 113 | .append(rate) 114 | .append(available) 115 | .append(version) 116 | .toHashCode(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/SampleMappingEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch; 18 | 19 | 20 | import org.springframework.data.annotation.Id; 21 | import org.springframework.data.elasticsearch.annotations.Document; 22 | import org.springframework.data.elasticsearch.annotations.Field; 23 | 24 | /** 25 | * @author Rizwan Idrees 26 | * @author Mohsin Husen 27 | */ 28 | @Document(indexName = "test-mapping", type = "mapping") 29 | public class SampleMappingEntity { 30 | 31 | @Id 32 | private String id; 33 | @Field(type = "string",index = "not_analyzed", store = true, searchAnalyzer = "standard", indexAnalyzer = "standard") 34 | private String message; 35 | 36 | private NestedEntity nested; 37 | 38 | public String getId() { 39 | return id; 40 | } 41 | 42 | public void setId(String id) { 43 | this.id = id; 44 | } 45 | 46 | public String getMessage() { 47 | return message; 48 | } 49 | 50 | public void setMessage(String message) { 51 | this.message = message; 52 | } 53 | 54 | 55 | static class NestedEntity{ 56 | @Field(type = "string") 57 | private String someField; 58 | 59 | public String getSomeField() { 60 | return someField; 61 | } 62 | 63 | public void setSomeField(String someField) { 64 | this.someField = someField; 65 | } 66 | } 67 | 68 | } 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/config/ElasticsearchNamespaceHandlerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.config; 17 | 18 | import org.junit.Test; 19 | import org.junit.runner.RunWith; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.context.ApplicationContext; 22 | import org.springframework.data.elasticsearch.repositories.SampleElasticsearchRepository; 23 | import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; 24 | import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; 25 | import org.springframework.test.context.ContextConfiguration; 26 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 27 | 28 | import static org.hamcrest.CoreMatchers.instanceOf; 29 | import static org.hamcrest.CoreMatchers.notNullValue; 30 | import static org.hamcrest.core.Is.is; 31 | import static org.junit.Assert.assertThat; 32 | 33 | /** 34 | * @author Rizwan Idrees 35 | * @author Mohsin Husen 36 | */ 37 | 38 | @RunWith(SpringJUnit4ClassRunner.class) 39 | @ContextConfiguration("namespace.xml") 40 | public class ElasticsearchNamespaceHandlerTest { 41 | 42 | @Autowired 43 | private ApplicationContext context; 44 | 45 | @Test 46 | public void shouldCreatesNodeClient() { 47 | assertThat(context.getBean(NodeClientFactoryBean.class), is(notNullValue())); 48 | assertThat(context.getBean(NodeClientFactoryBean.class), is(instanceOf(NodeClientFactoryBean.class))); 49 | } 50 | 51 | @Test 52 | public void shouldCreateTransportClient() { 53 | assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue())); 54 | assertThat(context.getBean(TransportClientFactoryBean.class), is(instanceOf(TransportClientFactoryBean.class))); 55 | } 56 | 57 | @Test 58 | public void shouldCreateRepository(){ 59 | assertThat(context.getBean(TransportClientFactoryBean.class), is(notNullValue())); 60 | assertThat(context.getBean(SampleElasticsearchRepository.class), is(instanceOf(SampleElasticsearchRepository.class))); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/config/EnableElasticsearchRepositoriesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.config; 17 | 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.Configuration; 24 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 25 | import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; 26 | import org.springframework.data.elasticsearch.repositories.SampleElasticsearchRepository; 27 | import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; 28 | import org.springframework.test.context.ContextConfiguration; 29 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 30 | 31 | import static org.elasticsearch.node.NodeBuilder.nodeBuilder; 32 | import static org.hamcrest.CoreMatchers.is; 33 | import static org.hamcrest.CoreMatchers.notNullValue; 34 | import static org.junit.Assert.assertThat; 35 | 36 | /** 37 | * @author Rizwan Idrees 38 | * @author Mohsin Husen 39 | */ 40 | @RunWith(SpringJUnit4ClassRunner.class) 41 | @ContextConfiguration 42 | public class EnableElasticsearchRepositoriesTest { 43 | 44 | @Configuration 45 | @EnableElasticsearchRepositories(basePackages = "org.springframework.data.elasticsearch.repositories") 46 | static class Config { 47 | 48 | @Bean 49 | public ElasticsearchOperations elasticsearchTemplate() { 50 | return new ElasticsearchTemplate(nodeBuilder().local(true).node().client()); 51 | } 52 | } 53 | 54 | @Autowired 55 | private SampleElasticsearchRepository repository; 56 | 57 | @Test 58 | public void bootstrapsRepository() { 59 | assertThat(repository, is(notNullValue())); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/core/convert/DateTimeConvertersTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.convert; 17 | 18 | import org.joda.time.DateTime; 19 | import org.joda.time.DateTimeZone; 20 | import org.joda.time.LocalDateTime; 21 | import org.junit.Assert; 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 25 | 26 | import java.util.Calendar; 27 | import java.util.TimeZone; 28 | 29 | /** 30 | * @author Rizwan Idrees 31 | * @author Mohsin Husen 32 | */ 33 | public class DateTimeConvertersTest { 34 | @Test 35 | public void testJodaDateTimeConverterWithNullValue() { 36 | Assert.assertNull(DateTimeConverters.JodaDateTimeConverter.INSTANCE.convert(null)); 37 | } 38 | 39 | @Test 40 | public void testJodaDateTimeConverter() { 41 | DateTime dateTime = new DateTime(2013, 1,24 , 6, 35, 0, DateTimeZone.UTC); 42 | Assert.assertEquals("2013-01-24T06:35:00.000Z", 43 | DateTimeConverters.JodaDateTimeConverter.INSTANCE.convert(dateTime)); 44 | } 45 | 46 | @Test 47 | public void testJodaLocalDateTimeConverterWithNullValue() { 48 | Assert.assertNull(DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE.convert(null)); 49 | } 50 | 51 | @Test 52 | public void testJodaLocalDateTimeConverter() { 53 | LocalDateTime dateTime = new LocalDateTime(new DateTime(2013, 1,24, 6, 35, 0, DateTimeZone.UTC).getMillis(), 54 | DateTimeZone.UTC); 55 | Assert.assertEquals("2013-01-24T06:35:00.000Z", 56 | DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE.convert(dateTime)); 57 | } 58 | 59 | @Test 60 | public void testJavaDateConverterWithNullValue() { 61 | Assert.assertNull(DateTimeConverters.JavaDateConverter.INSTANCE.convert(null)); 62 | } 63 | 64 | @Test 65 | public void testJavaDateConverter() { 66 | DateTime dateTime = new DateTime(2013, 1,24, 6, 35, 0, DateTimeZone.UTC); 67 | Calendar calendar = Calendar.getInstance(); 68 | calendar.setTimeZone(TimeZone.getTimeZone("UTC")); 69 | calendar.setTimeInMillis(dateTime.getMillis()); 70 | 71 | Assert.assertEquals("2013-01-24T06:35:00.000Z", 72 | DateTimeConverters.JavaDateConverter.INSTANCE.convert(calendar.getTime())); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.core.convert; 17 | 18 | 19 | import org.junit.Test; 20 | import org.springframework.core.convert.ConversionService; 21 | import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; 22 | import org.springframework.data.mapping.context.MappingContext; 23 | 24 | import static org.hamcrest.Matchers.*; 25 | import static org.junit.Assert.assertThat; 26 | /** 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | public class MappingElasticsearchConverterTest { 31 | 32 | @Test(expected = IllegalArgumentException.class) 33 | public void shouldFailToInitializeGivenMappingContextIsNull(){ 34 | //given 35 | new MappingElasticsearchConverter(null); 36 | } 37 | 38 | @Test 39 | public void shouldReturnMappingContextWithWhichItWasInitialized(){ 40 | //given 41 | MappingContext mappingContext = new SimpleElasticsearchMappingContext(); 42 | MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext); 43 | //then 44 | assertThat(converter.getMappingContext(), is(notNullValue())); 45 | assertThat(converter.getMappingContext(), is(sameInstance(mappingContext))); 46 | } 47 | 48 | @Test 49 | public void shouldReturnDefaultConversionService(){ 50 | //given 51 | MappingElasticsearchConverter converter = new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext()); 52 | //when 53 | ConversionService conversionService = converter.getConversionService(); 54 | //then 55 | assertThat(conversionService, is(notNullValue())); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntityTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.core.mapping; 18 | 19 | import org.junit.Test; 20 | import org.springframework.data.annotation.Version; 21 | import org.springframework.data.mapping.model.MappingException; 22 | import org.springframework.data.mapping.model.SimpleTypeHolder; 23 | import org.springframework.data.util.ClassTypeInformation; 24 | import org.springframework.data.util.TypeInformation; 25 | 26 | import java.beans.IntrospectionException; 27 | import java.beans.PropertyDescriptor; 28 | /** 29 | * @author Rizwan Idrees 30 | * @author Mohsin Husen 31 | */ 32 | public class SimpleElasticsearchPersistentEntityTest { 33 | 34 | @Test(expected = IllegalArgumentException.class) 35 | public void shouldThrowExceptionGivenVersionPropertyIsNotLong() throws NoSuchFieldException, IntrospectionException { 36 | //given 37 | TypeInformation typeInformation = ClassTypeInformation.from(EntityWithWrongVersionType.class); 38 | SimpleElasticsearchPersistentProperty persistentProperty = 39 | new SimpleElasticsearchPersistentProperty(EntityWithWrongVersionType.class.getDeclaredField("version"), 40 | new PropertyDescriptor("version", EntityWithWrongVersionType.class), 41 | new SimpleElasticsearchPersistentEntity(typeInformation), 42 | new SimpleTypeHolder()); 43 | 44 | //when 45 | new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty); 46 | } 47 | 48 | 49 | @Test(expected = MappingException.class) 50 | public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent() throws NoSuchFieldException, IntrospectionException { 51 | //given 52 | TypeInformation typeInformation = ClassTypeInformation.from(EntityWithMultipleVersionField.class); 53 | SimpleElasticsearchPersistentProperty persistentProperty1 = 54 | new SimpleElasticsearchPersistentProperty(EntityWithMultipleVersionField.class.getDeclaredField("version1"), 55 | new PropertyDescriptor("version1", EntityWithMultipleVersionField.class), 56 | new SimpleElasticsearchPersistentEntity(typeInformation), 57 | new SimpleTypeHolder()); 58 | 59 | SimpleElasticsearchPersistentProperty persistentProperty2 = 60 | new SimpleElasticsearchPersistentProperty(EntityWithMultipleVersionField.class.getDeclaredField("version2"), 61 | new PropertyDescriptor("version2", EntityWithMultipleVersionField.class), 62 | new SimpleElasticsearchPersistentEntity(typeInformation), 63 | new SimpleTypeHolder()); 64 | 65 | SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity(typeInformation); 66 | simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty1); 67 | //when 68 | simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty2); 69 | } 70 | 71 | 72 | private class EntityWithWrongVersionType { 73 | @Version 74 | private String version; 75 | 76 | public String getVersion() { 77 | return version; 78 | } 79 | 80 | public void setVersion(String version) { 81 | this.version = version; 82 | } 83 | } 84 | 85 | private class EntityWithMultipleVersionField{ 86 | 87 | @Version 88 | private Long version1; 89 | @Version 90 | private Long version2; 91 | 92 | public Long getVersion1() { 93 | return version1; 94 | } 95 | 96 | public void setVersion1(Long version1) { 97 | this.version1 = version1; 98 | } 99 | 100 | public Long getVersion2() { 101 | return version2; 102 | } 103 | 104 | public void setVersion2(Long version2) { 105 | this.version2 = version2; 106 | } 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/DoubleIDRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repositories; 17 | 18 | import org.springframework.data.elasticsearch.DoubleIDEntity; 19 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 20 | 21 | public interface DoubleIDRepository extends ElasticsearchRepository { 22 | 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/IntegerIDRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repositories; 17 | 18 | import org.springframework.data.elasticsearch.IntegerIDEntity; 19 | import org.springframework.data.elasticsearch.SampleEntity; 20 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 21 | 22 | public interface IntegerIDRepository extends ElasticsearchRepository { 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/NonDocumentEntityRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repositories; 17 | 18 | import org.springframework.data.elasticsearch.NonDocumentEntity; 19 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 20 | 21 | /** 22 | * @author Rizwan Idrees 23 | * @author Mohsin Husen 24 | */ 25 | public interface NonDocumentEntityRepository extends ElasticsearchRepository { 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/SampleCustomMethodRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repositories; 17 | 18 | 19 | import org.springframework.data.domain.Page; 20 | import org.springframework.data.domain.Pageable; 21 | import org.springframework.data.elasticsearch.SampleEntity; 22 | import org.springframework.data.elasticsearch.annotations.Query; 23 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 24 | 25 | import java.util.List; 26 | /** 27 | * @author Rizwan Idrees 28 | * @author Mohsin Husen 29 | */ 30 | public interface SampleCustomMethodRepository extends ElasticsearchRepository { 31 | 32 | Page findByType(String type, Pageable pageable); 33 | 34 | Page findByTypeNot(String type, Pageable pageable); 35 | 36 | @Query("{\"bool\" : {\"must\" : {\"field\" : {\"message\" : \"?0\"}}}}") 37 | Page findByMessage(String message, Pageable pageable); 38 | 39 | @Query("{\"bool\" : {\"must\" : {\"field\" : {\"message\" : \"?0\"}}}}") 40 | List findByMessage(String message); 41 | 42 | Page findByAvailable(boolean available, Pageable pageable); 43 | 44 | Page findByRateLessThan(int rate, Pageable pageable); 45 | 46 | Page findByRateBefore(int rate, Pageable pageable); 47 | 48 | Page findByRateAfter(int rate, Pageable pageable); 49 | 50 | Page findByMessageLike(String message, Pageable pageable); 51 | 52 | Page findByMessageStartingWith(String message, Pageable pageable); 53 | 54 | Page findByMessageEndingWith(String message, Pageable pageable); 55 | 56 | Page findByMessageContaining(String message, Pageable pageable); 57 | 58 | Page findByIdIn(List ids, Pageable pageable); 59 | 60 | Page findByIdNotIn(List messages, Pageable pageable); 61 | 62 | Page findByAvailableTrue(Pageable pageable); 63 | 64 | Page findByAvailableFalse(Pageable pageable); 65 | 66 | Page findByMessageOrderByTypeAsc(String message,Pageable pageable); 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/SampleElasticSearchBookRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repositories; 17 | 18 | import org.springframework.data.elasticsearch.Book; 19 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * @author Rizwan Idrees 25 | * @author Mohsin Husen 26 | */ 27 | public interface SampleElasticSearchBookRepository extends ElasticsearchRepository { 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repositories/SampleElasticsearchRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.springframework.data.elasticsearch.repositories; 18 | 19 | import org.springframework.data.elasticsearch.SampleEntity; 20 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 21 | /** 22 | * @author Rizwan Idrees 23 | * @author Mohsin Husen 24 | */ 25 | public interface SampleElasticsearchRepository extends ElasticsearchRepository { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchRepositoryFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.data.elasticsearch.repository.support; 17 | 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.mockito.Mock; 23 | import org.mockito.runners.MockitoJUnitRunner; 24 | import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 25 | import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; 26 | import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; 27 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; 28 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; 29 | import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; 30 | import org.springframework.data.mapping.context.MappingContext; 31 | import org.springframework.data.querydsl.QueryDslPredicateExecutor; 32 | import org.springframework.data.repository.core.RepositoryMetadata; 33 | import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; 34 | 35 | import static org.mockito.Mockito.when; 36 | /** 37 | * @author Rizwan Idrees 38 | * @author Mohsin Husen 39 | */ 40 | @RunWith(MockitoJUnitRunner.class) 41 | public class ElasticsearchRepositoryFactoryTest { 42 | 43 | @Mock 44 | private ElasticsearchOperations operations; 45 | private ElasticsearchConverter converter; 46 | private ElasticsearchRepositoryFactory factory; 47 | MappingContext, ElasticsearchPersistentProperty> mappingContext= new SimpleElasticsearchMappingContext(); 48 | 49 | @Before 50 | public void before(){ 51 | converter = new MappingElasticsearchConverter(mappingContext); 52 | when(operations.getElasticsearchConverter()).thenReturn(converter); 53 | factory = new ElasticsearchRepositoryFactory(operations); 54 | } 55 | 56 | 57 | 58 | @Test(expected = IllegalArgumentException.class) 59 | public void shouldThrowExceptionGivenQueryDslRepository(){ 60 | //given 61 | RepositoryMetadata metadata = new DefaultRepositoryMetadata(QueryDslPredicateExecutor.class); 62 | //when 63 | factory.getRepositoryBaseClass(metadata); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/resources/custom-method-repository-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/test/resources/elasticsearch-template-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d %5p %40.40c:%4L - %m%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/test/resources/org/springframework/data/elasticsearch/config/namespace.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/test/resources/repository-non-document-entity.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/repository-test-nested-object.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/test/resources/simple-repository-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | --------------------------------------------------------------------------------