Skip to content

Commit 3704d03

Browse files
committed
Merging PR #47
2 parents 9e2d0cf + 001f46b commit 3704d03

File tree

8 files changed

+93
-48
lines changed

8 files changed

+93
-48
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Module | Playframework | Elasticsearch | Comments | Diff
2626
0.8-SNAPSHOT | 2.2.1 | 0.90.12 | Upgrade to ES 0.90.12 & play 2.2.1
2727
0.8.1 | 2.2.1 | 0.90.12 | Upgrade to ES 0.90.12 & play 2.2.1
2828
0.8.2 | 2.2.1 | 0.90.12 | Small fixes
29+
1.1.0 | 2.2.1 | 1.1.0 | Upgrading the ES 1.1.0 (#47)
2930

3031
## Install
3132

module/app/com/github/cleverage/elasticsearch/IndexQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public SearchRequestBuilder getSearchRequestBuilder(IndexQueryPath indexQueryPat
220220
.prepareSearch(indexQueryPath.index)
221221
.setTypes(indexQueryPath.type)
222222
.setSearchType(SearchType.QUERY_THEN_FETCH)
223-
.setFilter(filter);
223+
.setPostFilter(filter);
224224

225225
// set Query
226226
if (StringUtils.isNotBlank(query)) {

module/app/com/github/cleverage/elasticsearch/IndexService.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.cleverage.elasticsearch;
22

33
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
4-
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
54
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
65
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
76
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
@@ -26,14 +25,14 @@
2625
import org.elasticsearch.cluster.ClusterState;
2726
import org.elasticsearch.cluster.metadata.MappingMetaData;
2827
import org.elasticsearch.common.xcontent.XContentBuilder;
29-
import org.elasticsearch.index.percolator.PercolatorService;
3028
import org.elasticsearch.index.query.FilterBuilder;
3129
import org.elasticsearch.index.query.QueryBuilder;
3230
import org.elasticsearch.indices.IndexMissingException;
3331
import play.Logger;
3432
import play.libs.F;
3533

3634
import java.io.IOException;
35+
import java.util.ArrayList;
3736
import java.util.Collection;
3837
import java.util.List;
3938
import java.util.Map;
@@ -44,7 +43,7 @@
4443
public abstract class IndexService {
4544

4645
public static final String INDEX_DEFAULT = IndexClient.config.indexNames[0];
47-
public static final String INDEX_PERCOLATOR = PercolatorService.INDEX_NAME;
46+
public static final String PERCOLATOR_TYPE = ".percolator";
4847

4948
/**
5049
* get indexRequest to index from a specific request
@@ -504,7 +503,7 @@ public static PutMappingResponse createMapping(String indexName, String indexTyp
504503
public static String getMapping(String indexName, String indexType) {
505504
ClusterState state = IndexClient.client.admin().cluster()
506505
.prepareState()
507-
.setFilterIndices(IndexService.INDEX_DEFAULT)
506+
.setIndices(IndexService.INDEX_DEFAULT)
508507
.execute().actionGet().getState();
509508
MappingMetaData mappingMetaData = state.getMetaData().index(indexName).mapping(indexType);
510509
if (mappingMetaData != null) {
@@ -592,24 +591,27 @@ public static void flush(String indexName) {
592591
* @param namePercolator
593592
* @param queryBuilder
594593
* @return
595-
* @throws IOException
596594
*/
597595
public static IndexResponse createPercolator(String namePercolator, QueryBuilder queryBuilder) {
596+
return createPercolator(INDEX_DEFAULT, namePercolator, queryBuilder,false);
597+
}
598598

599+
public static IndexResponse createPercolator(String indexName, String queryName, QueryBuilder queryBuilder, boolean immediatelyAvailable) {
599600
XContentBuilder source = null;
600601
try {
601602
source = jsonBuilder().startObject()
602603
.field("query", queryBuilder)
603604
.endObject();
604605
} catch (IOException e) {
605-
Logger.error("Elasticsearch : Erreur when create percolator from a queryBuilder", e);
606+
Logger.error("Elasticsearch : Error when creating percolator from a queryBuilder", e);
606607
}
607608

608609
IndexRequestBuilder percolatorRequest =
609-
IndexClient.client.prepareIndex(INDEX_PERCOLATOR,
610-
IndexService.INDEX_DEFAULT,
611-
namePercolator)
612-
.setSource(source);
610+
IndexClient.client.prepareIndex(indexName,
611+
PERCOLATOR_TYPE,
612+
queryName)
613+
.setSource(source)
614+
.setRefresh(immediatelyAvailable);
613615

614616
return percolatorRequest.execute().actionGet();
615617
}
@@ -623,24 +625,33 @@ public static IndexResponse createPercolator(String namePercolator, QueryBuilder
623625
* @throws IOException
624626
*/
625627
public static IndexResponse createPercolator(String namePercolator, String query) {
628+
return createPercolator(INDEX_DEFAULT,namePercolator,query,false);
629+
}
626630

631+
public static IndexResponse createPercolator(String indexName, String queryName, String query, boolean immediatelyAvailable) {
627632
IndexRequestBuilder percolatorRequest =
628-
IndexClient.client.prepareIndex(INDEX_PERCOLATOR,
629-
IndexService.INDEX_DEFAULT,
630-
namePercolator)
631-
.setSource("{\"query\": " + query + "}");
633+
IndexClient.client.prepareIndex(indexName,
634+
PERCOLATOR_TYPE,
635+
queryName)
636+
.setSource("{\"query\": " + query + "}")
637+
.setRefresh(immediatelyAvailable);
632638

633639
return percolatorRequest.execute().actionGet();
634640
}
635641

642+
636643
/**
637644
* Check if a percolator exists
638645
* @param namePercolator
639646
* @return
640647
*/
641-
public static boolean precolatorExists(String namePercolator) {
648+
public static boolean percolatorExists(String namePercolator) {
649+
return percolatorExistsInIndex(namePercolator, INDEX_DEFAULT);
650+
}
651+
652+
public static boolean percolatorExistsInIndex(String namePercolator, String indexName){
642653
try {
643-
GetResponse responseExist = IndexService.getPercolator(namePercolator);
654+
GetResponse responseExist = IndexService.getPercolator(indexName, namePercolator);
644655
return (responseExist.isExists());
645656
} catch (IndexMissingException e) {
646657
return false;
@@ -654,12 +665,18 @@ public static boolean precolatorExists(String namePercolator) {
654665
* @return
655666
*/
656667
public static DeleteResponse deletePercolator(String namePercolator) {
657-
return delete(new IndexQueryPath(INDEX_PERCOLATOR, IndexService.INDEX_DEFAULT), namePercolator);
668+
return deletePercolator(IndexService.INDEX_DEFAULT, namePercolator);
658669
}
659670

660-
/**
671+
public static DeleteResponse deletePercolator(String indexName, String namePercolator) {
672+
return delete(new IndexQueryPath(indexName, PERCOLATOR_TYPE), namePercolator);
673+
}
674+
675+
676+
// See important notes section on http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html
677+
/*/**
661678
* Delete all percolators
662-
*/
679+
*//*
663680
public static void deletePercolators() {
664681
try {
665682
DeleteIndexResponse deleteIndexResponse = IndexClient.client.admin().indices().prepareDelete(INDEX_PERCOLATOR).execute().actionGet();
@@ -671,15 +688,25 @@ public static void deletePercolators() {
671688
} catch (Exception e) {
672689
Logger.error("ElasticSearch : Index drop error : " + e.toString());
673690
}
674-
}
691+
}*/
675692

676693
/**
677694
* Get the percolator details
678-
* @param name
695+
* @param queryName
679696
* @return
680697
*/
681-
public static GetResponse getPercolator(String name) {
682-
return get(INDEX_PERCOLATOR, IndexService.INDEX_DEFAULT, name);
698+
public static GetResponse getPercolator(String queryName) {
699+
return getPercolator(INDEX_DEFAULT, queryName);
700+
}
701+
702+
/**
703+
* Get the percolator details on an index
704+
* @param indexName
705+
* @param queryName
706+
* @return
707+
*/
708+
public static GetResponse getPercolator(String indexName, String queryName){
709+
return get(indexName, PERCOLATOR_TYPE, queryName);
683710
}
684711

685712
/**
@@ -691,7 +718,8 @@ public static GetResponse getPercolator(String name) {
691718
*/
692719
public static List<String> getPercolatorsForDoc(Index indexable) {
693720

694-
PercolateRequestBuilder percolateRequestBuilder = new PercolateRequestBuilder(IndexClient.client, indexable.getIndexPath().index, indexable.getIndexPath().type);
721+
PercolateRequestBuilder percolateRequestBuilder = new PercolateRequestBuilder(IndexClient.client);
722+
percolateRequestBuilder.setDocumentType(indexable.getIndexPath().type);
695723

696724
XContentBuilder doc = null;
697725
try {
@@ -713,6 +741,11 @@ public static List<String> getPercolatorsForDoc(Index indexable) {
713741
if (percolateResponse == null) {
714742
return null;
715743
}
716-
return percolateResponse.getMatches();
744+
List<String> matchedQueryIds = new ArrayList<String>();
745+
PercolateResponse.Match[] matches = percolateResponse.getMatches();
746+
for(PercolateResponse.Match match : matches){
747+
matchedQueryIds.add(match.getId().string());
748+
}
749+
return matchedQueryIds;
717750
}
718751
}

module/app/com/github/cleverage/elasticsearch/plugin/IndexPlugin.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ public void onStart()
6464
}
6565
}
6666

67-
// Create "_percolator" index if not exists
68-
if (!IndexService.existsIndex(IndexService.INDEX_PERCOLATOR)) {
69-
IndexService.createIndex(IndexService.INDEX_PERCOLATOR);
70-
}
71-
7267
Logger.info("ElasticSearch : Plugin has started");
7368

7469
} catch (NoNodeAvailableException e) {

module/build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import xerial.sbt.Sonatype._
55

66
name := "play2-elasticsearch"
77

8-
version := "0.8-SNAPSHOT"
8+
version := "1.1-SNAPSHOT"
99

1010
libraryDependencies ++= Seq(
1111
javaCore,
1212
// Add your project dependencies here
13-
"org.elasticsearch" % "elasticsearch" % "0.90.12",
13+
"org.elasticsearch" % "elasticsearch" % "1.1.0",
1414
"org.apache.commons" % "commons-lang3" % "3.1"
1515
)
1616

module/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
play clean
1+
play clean-all
22
play reload
3+
play test
34
play compile
45
play publish-local

module/test/AsynchronousSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class AsynchronousSpec extends Specification with ElasticsearchTestHelper with S
7474
val future3 = SampleIndexableManager.deleteAsync("3")
7575
val future = Future.sequence(List(future1, future2, future3))
7676
val results = Await.result(future, Duration(10, SECONDS))
77-
results.forall(_.isNotFound) must beFalse
77+
results.forall(_.isFound) must beTrue
7878
results.map {_.getId()} must beEqualTo(List("1", "2", "3"))
7979
}
8080
}

module/test/ElasticsearchTestJava.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.elasticsearch.action.update.UpdateResponse;
88
import org.elasticsearch.common.geo.GeoPoint;
99
import org.elasticsearch.common.unit.DistanceUnit;
10-
import org.elasticsearch.index.get.GetField;
1110
import org.elasticsearch.index.query.FilterBuilders;
1211
import org.elasticsearch.index.query.GeoDistanceFilterBuilder;
1312
import org.elasticsearch.index.query.QueryBuilders;
@@ -65,6 +64,12 @@ public void run() {
6564
type1.category = category;
6665
type1.dateCreate = date;
6766

67+
// this is to go around the known bug in elasticsearch 1.1:
68+
// https://github.com/elasticsearch/elasticsearch/issues/5680#issuecomment-39794200
69+
// Issue is fixed on ES 1.1.1
70+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
71+
type1.location = location;
72+
6873
// indexing
6974
IndexResponse index = type1.index();
7075

@@ -117,7 +122,12 @@ public void asynchronousIndex() {
117122
@Override
118123
public void run() {
119124
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
125+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
126+
index1Type1.location = location;
127+
120128
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date());
129+
index1Type1Bis.location = location;
130+
121131
F.Promise<IndexResponse> promise1 = index1Type1.indexAsync();
122132
F.Promise<IndexResponse> promise2 = index1Type1Bis.indexAsync();
123133

@@ -133,9 +143,10 @@ public void asynchronousIndexBulk() {
133143
running(esFakeApplication(), new Runnable() {
134144
@Override
135145
public void run() {
136-
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
137-
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date());
138-
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date());
146+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
147+
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date(),location);
148+
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date(),location);
149+
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date(),location);
139150

140151
F.Promise<BulkResponse> promise = IndexService.indexBulkAsync(
141152
index1Type1.getIndexPath(),
@@ -153,9 +164,10 @@ public void asynchronousDelete() {
153164
running(esFakeApplication(), new Runnable() {
154165
@Override
155166
public void run() {
156-
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
157-
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date());
158-
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date());
167+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
168+
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date(), location);
169+
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date(), location);
170+
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date(), location);
159171
IndexService.indexBulk(index1Type1.getIndexPath(), Arrays.asList(index1Type1, index1Type1Bis, index1Type1Ter));
160172

161173
F.Promise<DeleteResponse> promise1 = index1Type1.deleteAsync();
@@ -175,9 +187,10 @@ public void asynchronousGet() {
175187
running(esFakeApplication(), new Runnable() {
176188
@Override
177189
public void run() {
178-
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
179-
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date());
180-
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date());
190+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
191+
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date(), location);
192+
Index1Type1 index1Type1Bis = new Index1Type1("2", "name2", "category", new Date(), location);
193+
Index1Type1 index1Type1Ter = new Index1Type1("3", "name3", "category", new Date(), location);
181194
IndexService.indexBulk(index1Type1.getIndexPath(), Arrays.asList(index1Type1, index1Type1Bis, index1Type1Ter));
182195

183196
F.Promise<Index1Type1> promise1 = IndexService.getAsync(index1Type1.getIndexPath(), Index1Type1.class, "1");
@@ -200,7 +213,8 @@ public void asynchronousSearch() {
200213
running(esFakeApplication(), new Runnable() {
201214
@Override
202215
public void run() {
203-
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
216+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
217+
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date(), location);
204218
index1Type1.index();
205219
IndexService.refresh();
206220

@@ -227,7 +241,8 @@ public void update() {
227241
@Override
228242
public void run() {
229243
// Blocking
230-
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date());
244+
GeoPoint location = new GeoPoint(30.6943566,-88.0430541);
245+
Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", new Date(),location);
231246
index1Type1.index();
232247
Map<String, Object> fieldNewValues = new HashMap<>();
233248
fieldNewValues.put("name", "new-name");
@@ -282,4 +297,4 @@ public void run() {
282297
}
283298
});
284299
}
285-
}
300+
}

0 commit comments

Comments
 (0)