]> git.plutz.net Git - rigidfind/blobdiff - index.cgi
avoid indexing empty content
[rigidfind] / index.cgi
index 61f811e2a5b2b24623e6a7c34f87a7806e0c1820..9ba696abe827ad71aeafd48f8bc178e96905d575 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -6,6 +6,8 @@
 
 [ "$_DATE" ] || _DATE="$(date +%s)"
 
+debug "$REQUEST_METHOD $REQUEST_URI    $SERVER_PROTOCOL        $_DATE"
+
 ingest() {
   local J="$1"
 
@@ -120,15 +122,23 @@ if   [ "$REQUEST_METHOD" = "PUT" ]; then
     s;,[ \t\r\n]*"content"[ \t\r\n]*:[ \t\r\n]*"[^"]*";;
   ')"
   J="$(json_load "${J}")"
-  
-  ingest "$J" "$content"\
-  | "${_EXEC}/concordance.sh" \
-    "$_DATA/$_INDEX/" "$(STRING "$_doc")       $_DATE"
+
+  debug "Content: ${#content} bytes"
+  debug "$(json_dump "$J")"
+
+  if [ "${#content}" -gt 0 ]; then
+    ingest "$J" "$content"\
+    | "${_EXEC}/concordance.sh" \
+      "$_DATA/$_INDEX/" "$(STRING "$_doc")     $_DATE"
+  fi
 
   J="${J#obj:}"
   J="$(DB2 "$J" set _indexdate num:"$_DATE")"
 
-  if   DBM "$_records" insert "$_doc" "$J"; then
+  if [ "${#content}" -eq 0 ]; then
+    printf '%s: %s\r\n' "Status" "200 OK"
+    result="updated"
+  elif DBM "$_records" insert "$_doc" "$J"; then
     printf '%s: %s\r\n' "Status" "201 Created" "Location" "/${_INDEX}/_doc/$(URL "$_doc")" \
     result="created"
   elif DBM "$_records" update "$_doc" "$J"; then
@@ -139,10 +149,10 @@ if   [ "$REQUEST_METHOD" = "PUT" ]; then
     exit 0
   fi
 
-  sed 's;$;\r;' <<-EOF
-       X-elastic-product: Elasticsearch
-       content-type: application/vnd.elasticsearch+json;compatible-with=8
-
+  cat <<-EOF
+       X-elastic-product: Elasticsearch\r
+       content-type: application/vnd.elasticsearch+json;compatible-with=8\r
+       \r
        { "_index": $(json_dump str:"${_INDEX}"),
          "_id": $(json_dump str:"$_doc"),
          "result": "$result",
@@ -167,10 +177,10 @@ elif [ "$REQUEST_METHOD" = "DELETE" ]; then
     result="not_found"
   fi
 
-  sed 's;$;\r;' <<-EOF
-       X-elastic-product: Elasticsearch
-       content-type: application/vnd.elasticsearch+json;compatible-with=8
-
+  cat <<-EOF
+       X-elastic-product: Elasticsearch\r
+       content-type: application/vnd.elasticsearch+json;compatible-with=8\r
+       \r
        { "_index": $(json_dump str:"${_INDEX}"),
          "_id": $(json_dump str:"$_doc"),
          "result": "$result",
@@ -192,7 +202,8 @@ elif [ "$REQUEST_METHOD" = "POST" ]; then
   results="@   $(
     search "${_DATA}/${_INDEX}" $words \
     | while read -r score id source; do
-      S="$(DB2   "" set _id     str:"$(UNSTRING "${id#/}")")"
+      S="$(DB2   "" set _index  str:"${_INDEX}")"
+      S="$(DB2 "$S" set _id     str:"$(UNSTRING "${id#/}")")"
       S="$(DB2 "$S" set _score  num:"$score")"
       S="$(DB2 "$S" set _source obj:"$(UNSTRING "$source")")"
       printf 'obj:%s\t' "$(STRING "$S")"
@@ -200,11 +211,11 @@ elif [ "$REQUEST_METHOD" = "POST" ]; then
   )"
   results="${results%  }"
 
-  sed 's;$;\r;' <<-EOF
-       Status: 200 OK
-       X-elastic-product: Elasticsearch
-       Content-Type: application/vnd.elasticsearch+json;compatible-with=8
-
+  cat <<-EOF
+       Status: 200 OK\r
+       X-elastic-product: Elasticsearch\r
+       Content-Type: application/vnd.elasticsearch+json;compatible-with=8\r
+       \r
        { "took":0,
          "timed_out":false,
          "_shards":{"total":1,"successful":1,"skipped":0,"failed":0},
@@ -221,11 +232,11 @@ elif [ "$REQUEST_METHOD" = "HEAD" ]; then
   [ ! "${accept#*"vnd.elasticsearch+json"*}" ] \
   && ctype="${accept}" || ctype="application/json"
 
-  sed 's;$;\r;' <<-EOF
-       HTTP/1.1 200 OK
-       X-elastic-product: Elasticsearch
-       content-type: ${ctype}
-
+  cat <<-EOF
+       HTTP/1.1 200 OK\r
+       X-elastic-product: Elasticsearch\r
+       content-type: ${ctype}\r
+       \r
        EOF
   exit 0
 
@@ -234,15 +245,15 @@ elif [ "$REQUEST_METHOD" = "GET" ]; then
   [ ! "${accept#*"vnd.elasticsearch+json"*}" ] \
   && ctype="${accept}" || ctype="application/json"
 
-  sed 's;$;\r;' <<-EOF
-       HTTP/1.1 200 OK
-       X-elastic-product: Elasticsearch
-       content-type: ${ctype}
-
+  cat <<-EOF
+       HTTP/1.1 200 OK\r
+       X-elastic-product: Elasticsearch\r
+       content-type: ${ctype}\r
+       \r
        EOF
        
   if [ "$PATH_INFO" = "/${_INDEX}/" ]; then
-  sed 's;$;\r;' <<-EOF
+  cat <<-EOF
        { $(json_dump str:"${_INDEX}"): {
            "aliases":{},
            "mappings": {
@@ -273,7 +284,7 @@ elif [ "$REQUEST_METHOD" = "GET" ]; then
        }
        EOF
   else
-    sed 's;$;\r;' <<-EOF
+    cat <<-EOF
        { "name" : "head",
          "cluster_name" : "elasticsearch",
          "version" : {