Files
go-onlyoffice/filestore_es_text_gator_test.go
eSlider 3f7505892f
Release / GoReleaser (push) Skipped
Tests / Secret scan (gitleaks) (push) Skipped
Tests / Test (Go 1.25) (push) Skipped
Tests / Test (Go stable) (push) Skipped
Tests / Test (Go stable) (pull_request) Failing after 3s
Tests / Secret scan (gitleaks) (pull_request) Successful in 4s
Tests / Test (Go 1.25) (pull_request) Successful in 1m30s
feat(search): gator fields in oo_docs_text + field filters (#86)
- TextDoc carries gator keyword fields (doc_sha, number, seller, total, typ,
  issued_at, created_at, status, url); content is now omitempty so a partial
  doc can be written.
- oo_docs_text mapping extended with the keyword fields.
- ESTextIndex.UpsertFields: bulk update + doc_as_upsert, merging gator fields
  into an existing text doc without clobbering title/content.
- SearchQuery gains Seller/Number/Status term filters; SearchHit.Fields exposes
  the gator values. Pure builders unit-tested.
2026-10-07 08:20:29 +00:00

72 lines
2.1 KiB
Go

package onlyoffice
import (
"encoding/json"
"strings"
"testing"
)
func TestESUpdateBulkBodyDocAsUpsert(t *testing.T) {
got := string(esUpdateBulkBody([]TextDoc{{ID: "42", Number: "RE-1", Seller: "ACME"}}))
if !strings.Contains(got, `{"update":{"_id":"42"}}`) {
t.Errorf("missing update action: %s", got)
}
if !strings.Contains(got, `"doc_as_upsert":true`) {
t.Errorf("missing doc_as_upsert: %s", got)
}
if !strings.Contains(got, `"number":"RE-1"`) || !strings.Contains(got, `"seller":"ACME"`) {
t.Errorf("gator fields missing: %s", got)
}
}
func TestESTextSearchRequestGatorFilters(t *testing.T) {
body, err := json.Marshal(esTextSearchRequest(SearchQuery{
Text: "rechnung", Seller: "ACME", Number: "RE-1", Status: "ok",
}))
if err != nil {
t.Fatal(err)
}
s := string(body)
for _, want := range []string{
`"seller":"ACME"`, `"number":"RE-1"`, `"status":"ok"`,
} {
if !strings.Contains(s, want) {
t.Errorf("filter %s missing in %s", want, s)
}
}
for _, src := range []string{`"seller"`, `"number"`, `"total"`, `"status"`, `"url"`} {
if !strings.Contains(s, src) {
t.Errorf("_source field %s missing in %s", src, s)
}
}
}
func TestGatorFields(t *testing.T) {
if got := gatorFields(TextDoc{ID: "1", Title: "x"}); got != nil {
t.Errorf("empty doc fields = %v, want nil", got)
}
got := gatorFields(TextDoc{ID: "1", Number: "RE-1", Seller: "ACME", Status: "ok"})
if got["number"] != "RE-1" || got["seller"] != "ACME" || got["status"] != "ok" {
t.Errorf("gatorFields = %v", got)
}
if _, ok := got["total"]; ok {
t.Errorf("empty total should be omitted: %v", got)
}
}
func TestParseESTextResponseGatorFields(t *testing.T) {
raw := []byte(`{"hits":{"total":{"value":1},"hits":[
{"_id":"42","_score":1.0,"_source":{"id":"42","title":"a.pdf","seller":"ACME","number":"RE-1","status":"ok"}}
]}}`)
hits, err := parseESTextResponse(raw)
if err != nil {
t.Fatal(err)
}
if len(hits) != 1 {
t.Fatalf("hits = %d", len(hits))
}
if hits[0].Fields["seller"] != "ACME" || hits[0].Fields["number"] != "RE-1" {
t.Errorf("Fields = %v", hits[0].Fields)
}
}