Browse Source

Added support for a simple search result json

Abhinav Sarkar 7 years ago
parent
commit
2bf175e705

+ 12
- 3
src/main/scala/net/abhinavsarkar/ircsearch/Server.scala View File

@@ -199,21 +199,30 @@ object SearchHandler extends HttpRequestHandler {
199 199
         val query = params("query")(0)
200 200
         val page = params.get("page").collect({ case l => l.get(0) })
201 201
         val pageSize = params.get("pageSize").collect({ case l => l.get(0) })
202
+        val details = params.get("details").collect({ case l => l.get(0) })
203
+
202 204
         var sr = new SearchRequest(server, channel, botName, query)
203 205
         if (page.isDefined)
204 206
           sr = sr.copy(page = page.get.toInt)
205 207
         if (pageSize.isDefined)
206 208
           sr = sr.copy(pageSize = pageSize.get.toInt)
209
+        if (details.isDefined)
210
+          sr = sr.copy(details = details.get.toBoolean)
207 211
         sr
208 212
       } else {
209 213
         throw new UnsupportedOperationException("HTTP method " + method + " is not supported")
210 214
       }
211 215
 
212
-      Searcher.search(searchRequest)
216
+      (searchRequest, Searcher.search(searchRequest))
213 217
     }
214 218
     f onSuccess {
215
-      case searchResult =>
216
-        logRequest(ctx, request, sendSuccess(ctx, request, Serialization.write(searchResult)))
219
+      case (searchRequest, searchResult) =>
220
+        logRequest(ctx, request,
221
+          sendSuccess(ctx, request,
222
+            if (searchRequest.details)
223
+              Serialization.write(searchResult)
224
+            else
225
+              Serialization.write(searchResult.toSimpleSearchResult)))
217 226
     }
218 227
     f onFailure { case e : Exception => {
219 228
       logger.error("Error", e)

+ 15
- 3
src/main/scala/net/abhinavsarkar/ircsearch/model.scala View File

@@ -18,16 +18,28 @@ case class IndexRequest(
18 18
 
19 19
 case class SearchRequest(
20 20
     server : String, channel : String, botName : String, query: String,
21
-    page : Int = 0, pageSize : Int = 10)
21
+    page : Int = 0, pageSize : Int = 10, details : Boolean = false)
22 22
 
23 23
 case class SearchResult(
24 24
     server : String, channel : String, botName : String, query: String,
25
-    page : Int, pageSize : Int, totalResults : Int, chatLines : List[ChatLine])
25
+    page : Int, pageSize : Int, totalResults : Int, chatLines : List[ChatLine]) {
26
+  def toSimpleSearchResult =
27
+    SimpleSearchResult(server, channel, botName, query, page, pageSize, totalResults,
28
+      chatLines map {
29
+        case mline@ChatLine(_, _, _, contextBefore, contextAfter) =>
30
+          ((contextBefore :+ mline) ++ contextAfter) map { line =>
31
+            List(line.timestamp.toString, line.user, line.message)
32
+          }
33
+      })
34
+}
26 35
 
27 36
 object SearchResult {
28 37
   def fromSearchRequest(searchRequest : SearchRequest) = searchRequest match {
29
-    case SearchRequest(server, channel, botName, query, page, pageSize) =>
38
+    case SearchRequest(server, channel, botName, query, page, pageSize, _) =>
30 39
       new SearchResult(server, channel, botName, query, page, pageSize, 0, List())
31 40
   }
32 41
 }
33 42
 
43
+case class SimpleSearchResult(
44
+    server : String, channel : String, botName : String, query: String,
45
+    page : Int, pageSize : Int, totalResults : Int, lines : List[List[List[String]]])

Loading…
Cancel
Save