split up sqb.fromClause() prior to adding recursiveness
This should make it a bit easier to reuse parts in the upcoming
recurviseness (n levels deep) lookup
@@ -35,7 +35,6 @@ func newSubQueryBuilder(r *Repo) *subQueryBuilder {
}
func (s *subQueryBuilder) fromClause(ctx context.Context, clause *filters.Clause) ([]storageIdentifier, error) {
-
innerFilter := &filters.LocalFilter{
Root: &filters.Clause{
Operator: clause.Operator,
@@ -51,14 +50,26 @@ func (s *subQueryBuilder) fromClause(ctx context.Context, clause *filters.Clause
k := s.kindOfClass(clause.On.Child.Class.String())
index := classIndexFromClassName(k, clause.On.Child.Class.String())
+ s.logRequest(index, innerFilter)
- s.repo.logger.
- WithField("action", "esvector_filter_subquery").
- WithField("index", index).
- WithField("filters", innerFilter).
- Debug("starting subquery to build search query to esvector")
+ res, err := s.buildBodyAndDoRequest(ctx, filterQuery, k, index)
+ if err != nil {
+ return nil, fmt.Errorf("subquery: %v", err)
+ }
- s.repo.requestCounter.Inc()
+ results, err := s.extractStorageIdentifierFromResults(res)
+ if err != nil {
+ if _, ok := err.(SubQueryNoResultsErr); ok {
+ return nil, err
+ }
+ return nil, fmt.Errorf("subquery result extraction: %v", err)
+ }
+
+ return results, nil
+}
+
+func (s *subQueryBuilder) buildBodyAndDoRequest(ctx context.Context,
+ filterQuery map[string]interface{}, k kind.Kind, index string) (*esapi.Response, error) {
body := map[string]interface{}{
"query": filterQuery,
@@ -67,28 +78,15 @@ func (s *subQueryBuilder) fromClause(ctx context.Context, clause *filters.Clause
}
var buf bytes.Buffer
- err = json.NewEncoder(&buf).Encode(body)
+ err := json.NewEncoder(&buf).Encode(body)
if err != nil {
- return nil, fmt.Errorf("subquery: encode json: %v", err)
+ return nil, fmt.Errorf("encode json: %v", err)
}
- res, err := s.repo.client.Search(
+ return s.repo.client.Search(
s.repo.client.Search.WithContext(ctx),
s.repo.client.Search.WithIndex(index),
s.repo.client.Search.WithBody(&buf),
)
- if err != nil {
- return nil, fmt.Errorf("subquery search: %v", err)
- }
-
- results, err := s.extractStorageIdentifierFromResults(res)
- if err != nil {
- if _, ok := err.(SubQueryNoResultsErr); ok {
- return nil, err
- }
- return nil, fmt.Errorf("subquery result extraction: %v", err)
- }
-
- return results, nil
}
func (s subQueryBuilder) extractStorageIdentifierFromResults(res *esapi.Response) ([]storageIdentifier, error) {
@@ -146,3 +144,13 @@ func storageIdentifiersToBeaconBoolFilter(in []storageIdentifier, propName strin
},
}
}
+
+func (s *subQueryBuilder) logRequest(index string, innerFilter *filters.LocalFilter) {
+ s.repo.logger.
+ WithField("action", "esvector_filter_subquery").
+ WithField("index", index).
+ WithField("filters", innerFilter).
+ Debug("starting subquery to build search query to esvector")
+
+ s.repo.requestCounter.Inc()
+}
