@@ -6,26 +6,28 @@ import org.elasticsearch.common.xcontent.XContentBuilder
66import org .elasticsearch .search .aggregations .AggregationBuilders
77import org .elasticsearch .search .aggregations .bucket .terms .StringTerms .Bucket
88import play .api .libs .json .Json ._
9+
910import scala .util .Try
10- import scala .collection .mutable .{MutableList , ListBuffer }
11+ import scala .collection .mutable .{ListBuffer , MutableList }
1112import scala .collection .immutable .List
12- import play .api .{Plugin , Logger , Application }
13+ import play .api .{Application , Logger , Plugin }
1314import org .elasticsearch .common .settings .Settings
1415import org .elasticsearch .client .transport .TransportClient
1516import org .elasticsearch .common .transport .InetSocketTransportAddress
1617import java .net .InetAddress
1718import java .util .regex .Pattern
1819
1920import org .elasticsearch .common .xcontent .XContentFactory ._
20- import org .elasticsearch .action .search .{SearchPhaseExecutionException , SearchType , SearchResponse }
21+ import org .elasticsearch .action .search .{SearchPhaseExecutionException , SearchResponse , SearchType }
2122import org .elasticsearch .client .transport .NoNodeAvailableException
2223import org .elasticsearch .ElasticsearchException
2324import org .elasticsearch .indices .IndexAlreadyExistsException
24-
25- import models .{Collection , Dataset , File , Folder , UUID , ResourceRef , Section , ElasticsearchResult , User }
25+ import org . elasticsearch . index . reindex .{ ReindexAction , ReindexPlugin , ReindexRequestBuilder }
26+ import models .{Collection , Dataset , ElasticsearchResult , File , Folder , ResourceRef , Section , UUID , User }
2627import play .api .Play .current
2728import play .api .libs .json ._
2829import _root_ .util .SearchUtils
30+ import org .elasticsearch .action .admin .indices .exists .indices .IndicesExistsRequest
2931
3032
3133/**
@@ -67,7 +69,7 @@ class ElasticsearchPlugin(application: Application) extends Plugin {
6769 } else {
6870 Settings .settingsBuilder().build()
6971 }
70- client = Some (TransportClient .builder().settings(settings).build()
72+ client = Some (TransportClient .builder().settings(settings).addPlugin( classOf [ ReindexPlugin ]). build()
7173 .addTransportAddress(new InetSocketTransportAddress (InetAddress .getByName(serverAddress), serverPort)))
7274 Logger .debug(" --- Elasticsearch Client is being created----" )
7375 client match {
@@ -324,16 +326,33 @@ class ElasticsearchPlugin(application: Application) extends Plugin {
324326 }
325327 case None =>
326328 }
329+ }
327330
331+ def swapIndex (idx : String ): Unit = {
332+ client match {
333+ case Some (x) => {
334+ // Check if swap index exists before swapping
335+ if (x.admin.indices.exists(new IndicesExistsRequest (idx)).get().isExists()) {
336+ Logger .debug(" Deleting " + nameOfIndex+ " index..." )
337+ deleteAll(nameOfIndex)
338+ createIndex(nameOfIndex)
339+ Logger .debug(" Replacing with " + idx+ " ..." )
340+ ReindexAction .INSTANCE .newRequestBuilder(x).source(idx).destination(nameOfIndex).get()
341+ Logger .debug(" Deleting " + idx+ " index..." )
342+ deleteAll(idx)
343+ }
344+ }
345+ case None =>
346+ }
328347 }
329348
330- /** Delete all indices */
331- def deleteAll {
349+ /** Delete all documents in default index */
350+ def deleteAll ( idx : String = nameOfIndex) {
332351 connect()
333352 client match {
334353 case Some (x) => {
335354 try {
336- val response = x.admin().indices().prepareDelete(nameOfIndex ).get()
355+ val response = x.admin().indices().prepareDelete(idx ).get()
337356 if (! response.isAcknowledged())
338357 Logger .error(" Did not delete all data from elasticsearch." )
339358 } catch {
@@ -398,51 +417,51 @@ class ElasticsearchPlugin(application: Application) extends Plugin {
398417 * Reindex the given collection, if recursive is set to true it will
399418 * also reindex all datasets and files.
400419 */
401- def index (collection : Collection , recursive : Boolean ) {
420+ def index (collection : Collection , recursive : Boolean , idx : Option [ String ] ) {
402421 connect()
403422 // Perform recursion first if necessary
404423 if (recursive) {
405424 for (dataset <- datasets.listCollection(collection.id.toString)) {
406- index(dataset, recursive)
425+ index(dataset, recursive, idx )
407426 }
408427 }
409- index(SearchUtils .getElasticsearchObject(collection))
428+ index(SearchUtils .getElasticsearchObject(collection), idx.getOrElse(nameOfIndex) )
410429 }
411430
412431 /**
413432 * Reindex the given dataset, if recursive is set to true it will
414433 * also reindex all files.
415434 */
416- def index (dataset : Dataset , recursive : Boolean ) {
435+ def index (dataset : Dataset , recursive : Boolean , idx : Option [ String ] ) {
417436 connect()
418437 // Perform recursion first if necessary
419438 if (recursive) {
420- files.get(dataset.files).found.foreach(f => index(f))
439+ files.get(dataset.files).found.foreach(f => index(f, idx ))
421440 for (folderid <- dataset.folders) {
422441 folders.get(folderid) match {
423442 case Some (f) => {
424- files.get(f.files).found.foreach(fi => index(fi))
443+ files.get(f.files).found.foreach(fi => index(fi, idx ))
425444 }
426445 case None => Logger .error(s " Error getting file $folderid for recursive indexing " )
427446 }
428447 }
429448 }
430- index(SearchUtils .getElasticsearchObject(dataset))
449+ index(SearchUtils .getElasticsearchObject(dataset), idx.getOrElse(nameOfIndex) )
431450 }
432451
433452 /** Reindex the given file. */
434- def index (file : File ) {
453+ def index (file : File , idx : Option [ String ] ) {
435454 connect()
436455 // Index sections first so they register for tag counts
437456 for (section <- file.sections) {
438- index(section)
457+ index(section, idx )
439458 }
440- index(SearchUtils .getElasticsearchObject(file))
459+ index(SearchUtils .getElasticsearchObject(file), idx.getOrElse(nameOfIndex) )
441460 }
442461
443- def index (section : Section ) {
462+ def index (section : Section , idx : Option [ String ] ) {
444463 connect()
445- index(SearchUtils .getElasticsearchObject(section))
464+ index(SearchUtils .getElasticsearchObject(section), idx.getOrElse(nameOfIndex) )
446465 }
447466
448467 /** Index document using an arbitrary map of fields. */
@@ -687,19 +706,12 @@ class ElasticsearchPlugin(application: Application) extends Plugin {
687706
688707 /** Return string-encoded JSON object describing field types */
689708 def getElasticsearchObjectMappings (): String = {
690- """ dynamic_templates": [{
691- "nonindexer": {
692- "match": "*",
693- "match_mapping_type":"string",
694- "mapping": {
695- "type": "string",
696- "index": "not_analyzed"
697- }
698- }
699- }
700- ],"""
701-
709+ /** The dynamic template will restrict all dynamic metadata fields to be indexed
710+ * as strings for datatypes besides Objects. In the future, this could
711+ * be removed, but only once the Search API better supports those data types (e.g. Date).
712+ */
702713 """ {"clowder_object": {
714+ |"date_detection": false,
703715 |"properties": {
704716 |"name": {"type": "string"},
705717 |"description": {"type": "string"},
0 commit comments