]> git.plutz.net Git - rigidfind/blobdiff - index.cgi
bugfix: time calculation
[rigidfind] / index.cgi
index 68d0ef2c21d126b8d8e1d5c08a5d8a4368b53455..945d133985a5ddc242bc062a2ce1d39b280f762e 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -1,11 +1,13 @@
 #!/bin/sh
 
+read _DATE _date_n <<-EOF
+       $(date +"%s     %N")
+       EOF
+
 . "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
 . "${_EXEC:-${0%/*}}/cgilite/storage.sh"
 . "${_EXEC:-${0%/*}}/cgilite/json.sh"
 
-[ "$_DATE" ] || _DATE="$(date +%s)"
-
 debug "$REQUEST_METHOD $REQUEST_URI    $SERVER_PROTOCOL        $_DATE"
 
 ingest() {
@@ -211,13 +213,13 @@ elif [ "$REQUEST_METHOD" = "DELETE" ]; then
   exit 0
 
 elif [ "$REQUEST_METHOD" = "POST" ]; then
-  J="$(json_load "$(head -c "${CONTENT_LENGTH:-0}" |debug)")"
+  J="$(json_load "$(head -c "${CONTENT_LENGTH:-0}")")"
   J="$(json_get "$J" query.bool.must.bool.should)"
 
   words="$(
     for j in $(DB2 "$J" iterate @); do
       json_get "$(UNSTRING "$j")" match_phrase_prefix.content
-    done 2>/dev/null
+    done 2>/dev/null |tr \\n ' '
   )"
   debug "Search words: $words"
 
@@ -234,17 +236,18 @@ elif [ "$REQUEST_METHOD" = "POST" ]; then
   )"
   results="${results%  }"
 
+  t="$(( $(date +%s%N) - ${_DATE}${_date_n} ))"
+
   cat <<-EOF
        Status: 200 OK\r
        X-elastic-product: Elasticsearch\r
        Content-Type: application/vnd.elasticsearch+json;compatible-with=8\r
        \r
-       { "took":0,
+       { "took":$((t / 1000000)),
          "timed_out":false,
-         "_shards":{"total":1,"successful":1,"skipped":0,"failed":0},
          "hits": {
            "total":{"value": $(DB2 "$results" count @) ,"relation":"eq"},
-           "max_score": $(json_get "arr:$results" '[0]._score' 2>&- || printf 0),
+           "max_score": $(json_get "arr:$results" '[0]._score' 2>/dev/null || printf 0),
            "hits": $(json_dump "arr:$results")
          }
        }