Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 0ab8e59

Browse files
committed
works
1 parent 45484a1 commit 0ab8e59

File tree

1 file changed

+90
-4
lines changed
  • platform/model/typical_queries

1 file changed

+90
-4
lines changed

platform/model/typical_queries/hits.go

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/QuesmaOrg/quesma/platform/logger"
1515
"github.com/QuesmaOrg/quesma/platform/model"
1616
"github.com/QuesmaOrg/quesma/platform/util"
17+
"github.com/k0kubun/pp"
1718
"reflect"
1819
"sort"
1920
"strconv"
@@ -59,6 +60,8 @@ func (query Hits) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.
5960

6061
hits := make([]model.SearchHit, 0, len(rows))
6162

63+
logger.Warn().Msgf("Query Hits: %v", rows)
64+
6265
lookForCommonTableIndexColumn := true
6366

6467
for i, row := range rows {
@@ -89,9 +92,6 @@ func (query Hits) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.
8992
if query.addVersion {
9093
hit.Version = defaultVersion
9194
}
92-
if query.addSource {
93-
hit.Source = []byte(rows[i].String(query.ctx))
94-
}
9595
query.addAndHighlightHit(&hit, &row)
9696

9797
hit.ID = query.computeIdForDocument(hit, strconv.Itoa(i+1))
@@ -104,9 +104,31 @@ func (query Hits) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.
104104
logger.WarnWithCtx(query.ctx).Msgf("field %s not found in fields", fieldName)
105105
}
106106
}
107+
108+
// removeEmptyStringFields should be optional
109+
// (@trzysiek) I think it's best to not enable it by default, but only when
110+
// there's flag for it set in config to true
111+
//
112+
// If it's enabled, it should be at the end of our translation,
113+
// to not have to worry about e.g. sort working properly
114+
const removeEmptyStringFieldsInHitsQuery = true
115+
if removeEmptyStringFieldsInHitsQuery {
116+
query.removeEmptyStringFields(&hit)
117+
}
118+
119+
// `Source` should be filled at the end, as surprisingly Kibana displays hits
120+
// from `Source` field, and not from `Fields`.
121+
// So e.g. if we want to filter out empty strings, like above, we need to fill `Source` afterwards.
122+
if query.addSource {
123+
hit.Source = []byte(query.hitToString(&row, &hit))
124+
}
125+
126+
fmt.Println("&&& po remove2: ", hit.Fields)
107127
hits = append(hits, hit)
108128
}
109129

130+
pp.Println(hits)
131+
110132
return model.JsonMap{
111133
"hits": model.SearchHits{
112134
Total: &model.Total{
@@ -126,10 +148,11 @@ func (query Hits) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.
126148
func (query Hits) addAndHighlightHit(hit *model.SearchHit, resultRow *model.QueryResultRow) {
127149
toProperType := func(val interface{}) any {
128150
v := reflect.ValueOf(val)
151+
fmt.Println("KK toProperType1 val:", val, "v: ", v)
129152
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
130153
return val
131154
}
132-
155+
fmt.Println("LL toProperType1 val:", val, "v: ", v)
133156
resultArray := make([]interface{}, v.Len())
134157
for i := 0; i < v.Len(); i++ {
135158
resultArray[i] = v.Index(i).Interface()
@@ -223,6 +246,39 @@ func (query Hits) addAndHighlightHit(hit *model.SearchHit, resultRow *model.Quer
223246
hit.Fields[fieldName] = v
224247
}
225248
}
249+
250+
fmt.Println("--------------- koniec: ", hit.Fields)
251+
}
252+
253+
func (query Hits) removeEmptyStringFields(hit *model.SearchHit) {
254+
fieldNamesToRemove := make([]string, 0)
255+
for name, val := range hit.Fields {
256+
logger.Error().Msgf("KK hit field 1: %v, %v len(val): %v", name, val, len(val))
257+
// we only look for simple String/*String fields, so len == 1
258+
if len(val) != 1 {
259+
continue
260+
}
261+
262+
fmt.Printf("Typ: %T\n", val[0])
263+
264+
switch valT := val[0].(type) {
265+
case string:
266+
fmt.Println("???", valT == "")
267+
if valT == "" {
268+
fieldNamesToRemove = append(fieldNamesToRemove, name)
269+
}
270+
case *string:
271+
if valT != nil && *valT == "" {
272+
fieldNamesToRemove = append(fieldNamesToRemove, name)
273+
}
274+
}
275+
}
276+
277+
for _, name := range fieldNamesToRemove {
278+
fmt.Println("=== Removing ", name)
279+
delete(hit.Fields, name)
280+
}
281+
fmt.Println("--- Po removing: ", hit.Fields)
226282
}
227283

228284
func (query Hits) WithTimestampField(fieldName string) Hits {
@@ -298,6 +354,36 @@ func normalizeJSON(v interface{}) interface{} {
298354
}
299355
}
300356

357+
// More or less copy of: func (r *QueryResultRow) String(ctx context.Context) string
358+
// Some columns might already be excluded/removed, so we need a second implementation
359+
func (query Hits) hitToString(row *model.QueryResultRow, hit *model.SearchHit) string {
360+
str := strings.Builder{}
361+
str.WriteString(util.Indent(1) + "{\n")
362+
i := 0
363+
for _, col := range row.Cols {
364+
// skip internal columns
365+
if col.ColName == common_table.IndexNameColumn {
366+
continue
367+
}
368+
369+
// skip excluded fields
370+
if _, exists := hit.Fields[col.ColName]; !exists {
371+
continue
372+
}
373+
374+
colStr := col.String(query.ctx)
375+
if len(colStr) > 0 {
376+
if i > 0 {
377+
str.WriteString(",\n")
378+
}
379+
str.WriteString(util.Indent(2) + colStr)
380+
i++
381+
}
382+
}
383+
str.WriteString("\n" + util.Indent(1) + "}")
384+
return str.String()
385+
}
386+
301387
func (query Hits) String() string {
302388
return fmt.Sprintf("hits(indexes: %v)", strings.Join(query.indexes, ", "))
303389
}

0 commit comments

Comments
 (0)