Skip to content

feat: add paging support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ This project is a storage plugin for the [atest](https://github.com/linuxsuren/a

- Seamless integration with the `api-testing` framework.
- Web UI for interacting with Elasticsearch.
- Lucence query
- Lucene query or wildcard search

## Screenshots

![image](https://github.com/user-attachments/assets/f92e58e3-b4f6-4f51-bc02-e7fd348cbea5)
1 change: 1 addition & 0 deletions e2e/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
environment:
- discovery.type=single-node
- ELASTIC_PASSWORD=root
- ES_JAVA_OPTS=-Xms512m -Xmx512m
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9200"]
interval: 3s
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.23.8

require (
github.com/apache/iotdb-client-go v1.3.3
github.com/linuxsuren/api-testing v0.0.20-0.20250331081733-9d3e7ad6d47b
github.com/linuxsuren/api-testing v0.0.20-0.20250411152222-db22459df50b
github.com/spf13/cobra v1.9.1
google.golang.org/protobuf v1.36.6
)
Expand Down Expand Up @@ -106,3 +106,5 @@ require (
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
)

//replace github.com/linuxsuren/api-testing => ../api-testing
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ github.com/linuxsuren/api-testing v0.0.19 h1:lDpXdJzqXZhxycZbGRVmKy7tMZqwk+1zUzg
github.com/linuxsuren/api-testing v0.0.19/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
github.com/linuxsuren/api-testing v0.0.20-0.20250331081733-9d3e7ad6d47b h1:4auUdm3mIzqXu/l0kA7/OpccLTENxgEq32W3gjBJ1Po=
github.com/linuxsuren/api-testing v0.0.20-0.20250331081733-9d3e7ad6d47b/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
github.com/linuxsuren/api-testing v0.0.20-0.20250411141829-3cd89d157c3b h1:QABWKn/4Agg1xdlFW6Lmn1tI79U9xQNZ/NcUzp3LdyE=
github.com/linuxsuren/api-testing v0.0.20-0.20250411141829-3cd89d157c3b/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
github.com/linuxsuren/api-testing v0.0.20-0.20250411143208-6fccc908933b h1:hgk4bdSTGWt/nBFUsR5Yj/GscWG1r1IKNUsITC8YAfo=
github.com/linuxsuren/api-testing v0.0.20-0.20250411143208-6fccc908933b/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
github.com/linuxsuren/api-testing v0.0.20-0.20250411152222-db22459df50b h1:3EJ0d2S/pha+m34hLSWK/DPkdyZOdsmFE+ocEpNVwVQ=
github.com/linuxsuren/api-testing v0.0.20-0.20250411152222-db22459df50b/go.mod h1:igcyUJb5Q463tMpdEJxgURLLPR2pTcV7HoRyuvyP/2Y=
github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw=
github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
github.com/linuxsuren/go-service v0.0.0-20231225060426-efabcd3a5161 h1:dSL/ah6zaRGqH3FW0ogtMjP6xCFXX5NsgWJTaNIofI4=
Expand Down
21 changes: 12 additions & 9 deletions pkg/data_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *

var dataResult *server.DataQueryResult
now := time.Now()
if dataResult, err = sqlQuery(ctx, []string{query.Key}, query.Sql, db); err == nil {
if dataResult, err = sqlQuery(ctx, query, db); err == nil {
result.Items = dataResult.Items
result.Meta.Duration = time.Since(now).String()
}
Expand Down Expand Up @@ -161,16 +161,19 @@ func createCountRequests(ctx context.Context, db *elasticsearch.Client, index []
return searchRequests
}

func createSearchRequests(ctx context.Context, db *elasticsearch.Client, index []string, sql string) []func(*esapi.SearchRequest) {
func createSearchRequests(ctx context.Context, db *elasticsearch.Client, query *server.DataQuery) []func(*esapi.SearchRequest) {
searchRequests := []func(*esapi.SearchRequest){
db.Search.WithContext(ctx),
db.Search.WithSize(100),
db.Search.WithTrackTotalHits(true),
db.Search.WithIndex(index...),
db.Search.WithIndex(query.Key),
db.Search.WithFrom(int(query.Offset)),
db.Search.WithSize(int(query.Limit)),
db.Search.WithPretty(),
}

// https://www.elastic.co/guide/en/kibana/current/lucene-query.html
if !isLuceneQuery(sql) {
if !isLuceneQuery(query.Sql) {
searchRequests = append(searchRequests, db.Search.WithBody(strings.NewReader(fmt.Sprintf(`{
"query": {
"wildcard": {
Expand All @@ -179,9 +182,9 @@ func createSearchRequests(ctx context.Context, db *elasticsearch.Client, index [
}
}
}
}`, sql))))
}`, query.Sql))))
} else {
searchRequests = append(searchRequests, db.Search.WithQuery(sql))
searchRequests = append(searchRequests, db.Search.WithQuery(query.Sql))
}
return searchRequests
}
Expand All @@ -192,15 +195,15 @@ func isLuceneQuery(query string) bool {
return matched
}

func sqlQuery(ctx context.Context, index []string, sql string, db *elasticsearch.Client) (result *server.DataQueryResult, err error) {
func sqlQuery(ctx context.Context, query *server.DataQuery, db *elasticsearch.Client) (result *server.DataQueryResult, err error) {
result = &server.DataQueryResult{
Data: []*server.Pair{},
Items: make([]*server.Pairs, 0),
Meta: &server.DataMeta{},
}

fmt.Printf("query from index [%v], sql [%s]\n", index, sql)
searchRequests := createSearchRequests(ctx, db, index, sql)
fmt.Printf("query from index [%v], sql [%s]\n", query.Key, query.Sql)
searchRequests := createSearchRequests(ctx, db, query)

var res *esapi.Response
if res, err = db.Search(searchRequests...); err != nil {
Expand Down
Loading