-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DEXPRO-Solutions-GmbH/feat/search
Add function to search documents
- Loading branch information
Showing
4 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package easclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type SearchRequest struct { | ||
Query string `json:"query"` | ||
} | ||
|
||
func (request SearchRequest) ToQuery() map[string]string { | ||
return map[string]string{ | ||
"query": request.Query, | ||
} | ||
} | ||
|
||
type Link struct { | ||
Type string `json:"type"` | ||
Title string `json:"title"` | ||
Href string `json:"href"` | ||
} | ||
|
||
type SearchResult struct { | ||
Title bool `json:"title"` | ||
Score float64 `json:"score"` | ||
Id string `json:"id"` | ||
FileLink Link `json:"fileLink"` | ||
ExplainLink Link `json:"explainLink"` | ||
CheckVersionLink Link `json:"checkVersionLink"` | ||
HistoryLink Link `json:"historyLink"` | ||
VerifyLink Link `json:"verifyLink"` | ||
HeaderFields HeaderFields `json:"headerFields"` | ||
RecordFields RecordFields `json:"recordFields"` | ||
} | ||
|
||
type SearchResponse struct { | ||
Query string `json:"query"` | ||
TotalHits int `json:"totalHits"` | ||
ItemsPerPage int `json:"itemsPerPage"` | ||
StartIndex int `json:"startIndex"` | ||
Topn int `json:"topn"` | ||
EffectiveResults int `json:"effectiveResults"` | ||
Result []*SearchResult `json:"result"` | ||
} | ||
|
||
func (c *StoreClient) Search(ctx context.Context, request *SearchRequest) (*SearchResponse, error) { | ||
req, err := c.newRequest(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result SearchResponse | ||
|
||
req.SetResult(&result) | ||
|
||
req.SetQueryParams(request.ToQuery()) | ||
res, err := req.Get("") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if status := res.StatusCode(); status != 200 { | ||
return nil, fmt.Errorf("unexpected response status %v", status) | ||
} | ||
|
||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package easclient_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/DEXPRO-Solutions-GmbH/easclient" | ||
"github.com/DEXPRO-Solutions-GmbH/easclient/eastest" | ||
"github.com/DEXPRO-Solutions-GmbH/easclient/internal" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStoreClient_Search(t *testing.T) { | ||
internal.TestPrelude(t) | ||
eastest.SkipInCI(t) | ||
|
||
ctx := context.Background() | ||
user := easclient.NewUserClaims("test@dexpro.de") | ||
ctx = user.SetOnContext(ctx) | ||
|
||
request := &easclient.SearchRequest{ | ||
Query: "Amazo*", | ||
} | ||
|
||
response, err := eastest.DefaultClient().Search(ctx, request) | ||
require.NoError(t, err) | ||
require.NotNil(t, response) | ||
|
||
assert.Equal(t, "Amazo*", response.Query) | ||
assert.Greater(t, response.TotalHits, 0) | ||
assert.Greater(t, response.EffectiveResults, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package easclient | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type HeaderFields struct { | ||
DocumentType string `json:"_documentType"` | ||
MasterId uuid.UUID `json:"_masterId"` | ||
ArchiveDateTime time.Time `json:"_archiveDateTime"` | ||
Id uuid.UUID `json:"_id"` | ||
Version string `json:"_version"` | ||
ArchiverLogin string `json:"_archiverLogin"` | ||
InitialArchiverLogin string `json:"_initialArchiverLogin"` | ||
InitialArchiveDateTime time.Time `json:"_initialArchiveDateTime"` | ||
} | ||
|
||
type RecordFields map[string]string | ||
|
||
type Record struct { | ||
HeaderFields HeaderFields `json:"headerFields"` | ||
RecordFields RecordFields `json:"recordFields"` | ||
Attachments []*RecordAttachment `json:"attachments"` | ||
} | ||
|
||
// GetHeaderField returns either the value of the given header field or an empty string if the field does not exist. | ||
func (rec *Record) GetHeaderField(name string) string { | ||
return rec.RecordFields[name] | ||
} |