@@ -2,10 +2,10 @@ package api
22
33import api .Permission ._
44import services .{DatasetService , FileService , CollectionService , PreviewService , SpaceService ,
5- MultimediaQueryService , ElasticsearchPlugin }
5+ MultimediaQueryService , SearchService }
66import play .Logger
77import scala .collection .mutable .{ListBuffer , HashMap }
8- import util .{ SearchUtils , SearchResult }
8+ import util .SearchResult
99import play .api .libs .json .{JsObject , Json , JsValue }
1010import play .api .libs .json .Json .toJson
1111import javax .inject .{Inject , Singleton }
@@ -20,45 +20,32 @@ class Search @Inject() (
2020 collections : CollectionService ,
2121 previews : PreviewService ,
2222 queries : MultimediaQueryService ,
23- spaces : SpaceService ) extends ApiController {
23+ spaces : SpaceService ,
24+ searches : SearchService ) extends ApiController {
2425
2526 /** Search using a simple text string with filters */
2627 def search (query : String , resource_type : Option [String ], datasetid : Option [String ], collectionid : Option [String ],
2728 spaceid : Option [String ], folderid : Option [String ], field : Option [String ], tag : Option [String ],
2829 from : Option [Int ], size : Option [Int ], page : Option [Int ]) = PermissionAction (Permission .ViewDataset ) { implicit request =>
29- current.plugin[ElasticsearchPlugin ] match {
30- case Some (plugin) => {
31- // If from is specified, use it. Otherwise use page * size of page if possible, otherwise use 0.
32- val from_index = from match {
33- case Some (f) => from
34- case None => page match {
35- case Some (p) => Some (size.getOrElse(0 ) * p)
36- case None => None
37- }
30+ if (searches.isEnabled) {
31+ // If from is specified, use it. Otherwise use page * size of page if possible, otherwise use 0.
32+ val from_index = from match {
33+ case Some (f) => from
34+ case None => page match {
35+ case Some (p) => Some (size.getOrElse(0 ) * p)
36+ case None => None
3837 }
38+ }
3939
40- // TODO: Better way to build a URL?
41- val source_url = s " /api/search?query= $query" +
42- (resource_type match {case Some (x) => s " &resource_type= $x" case None => " " }) +
43- (datasetid match {case Some (x) => s " &datasetid= $x" case None => " " }) +
44- (collectionid match {case Some (x) => s " &collectionid= $x" case None => " " }) +
45- (spaceid match {case Some (x) => s " &spaceid= $x" case None => " " }) +
46- (folderid match {case Some (x) => s " &folderid= $x" case None => " " }) +
47- (field match {case Some (x) => s " &field= $x" case None => " " }) +
48- (tag match {case Some (x) => s " &tag= $x" case None => " " })
49-
50- // Add space filter to search here as a simple permissions check
51- val permitted = spaces.listAccess(0 , Set [Permission ](Permission .ViewSpace ), request.user, true , true , false , false ).map(sp => sp.id)
40+ // Add space filter to search here as a simple permissions check
41+ val permitted = spaces.listAccess(0 , Set [Permission ](Permission .ViewSpace ), request.user, true , true , false , false ).map(sp => sp.id)
5242
53- val response = plugin .search(query, resource_type, datasetid, collectionid, spaceid, folderid, field, tag, from_index, size, permitted, request.user)
43+ val result = searches .search(query, resource_type, datasetid, collectionid, spaceid, folderid, field, tag, from_index, size, permitted, request.user)
5444
55- val result = SearchUtils .prepareSearchResponse(response, source_url, request.user)
56- Ok (toJson(result))
57- }
58- case None => {
59- Logger .debug(" Search plugin not enabled" )
60- Ok (views.html.pluginNotEnabled(" Text search" ))
61- }
45+ Ok (toJson(result))
46+ } else {
47+ Logger .debug(" Search plugin not enabled" )
48+ Ok (views.html.pluginNotEnabled(" Text search" ))
6249 }
6350 }
6451
@@ -67,21 +54,14 @@ class Search @Inject() (
6754 implicit request =>
6855 implicit val user = request.user
6956
70- current.plugin[ElasticsearchPlugin ] match {
71- case Some (plugin) => {
72- val queryList = Json .parse(query).as[List [JsValue ]]
73- val response = plugin.search(queryList, grouping, from, size, user)
74-
75- // TODO: Better way to build a URL?
76- val source_url = s " /api/search?query= $query&grouping= $grouping"
77-
78- val result = SearchUtils .prepareSearchResponse(response, source_url, user)
79- Ok (toJson(result))
80- }
81- case None => {
82- BadRequest (" Elasticsearch plugin could not be reached" )
83- }
57+ if (searches.isEnabled) {
58+ val queryList = Json .parse(query).as[List [JsValue ]]
59+ val result = searches.search(queryList, grouping, from, size, user)
60+ Ok (toJson(result))
61+ } else {
62+ BadRequest (" Elasticsearch plugin could not be reached" )
8463 }
64+
8565 }
8666
8767 /**
0 commit comments