Skip to content
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

feat: expose all available terms #2097

Closed
Closed
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
11 changes: 11 additions & 0 deletions search/facets_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ func (tf *TermFacets) Terms() []*TermFacet {
return tf.termFacets
}

func (tf *TermFacets) AvailableTerms() []*TermFacet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you add a supporting unit test for this please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @abhinavdangeti added a test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottlepp Could you accomplish what you need with the existing Terms() func on TermFacets?

Copy link
Author

@scottlepp scottlepp Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Terms() func returns termFacets, which is empty. However you can see the termsLookup contains the terms.

I'm faceting on a field that is a string array.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems odd, I'm having trouble seeing how termFacets and termLookup could be out of sync.

termLookup is only referenced in Add(). Add() accumulates terms into both termLookup and termFacets so they should be consistent.

I've been using Terms() for years for the exact use case desribed in this PR and have never run into this condition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remembered that there are some other methods that change termFacets. Fixup() and TrimToTopN() can both remove (or entirely truncate) the termFacets slice.

I'm not that familiar with how these functions are called but, if I had to guess, it might only happen when the size of the facet request is very small (or zero).

bleve.NewFacetRequest("foo", 0)

As an aside, there doesn't appear to be a validation on the size of the facet request so perhaps there's a good reason to allow zero (I can't think of one, but again I'm only passingly familiar with the bleve code base).

Copy link
Author

@scottlepp scottlepp Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tylerkovacs you're right. the limit was defaulting to 0. Closing this. Sorry for the noise!

if tf == nil {
return []*TermFacet{}
}
terms := make([]*TermFacet, 0, len(tf.termLookup))
for _, term := range tf.termLookup {
terms = append(terms, term)
}
return terms
}

func (tf *TermFacets) TrimToTopN(n int) {
tf.termFacets = tf.termFacets[:n]
}
Expand Down
25 changes: 25 additions & 0 deletions search/facets_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,28 @@ func TestDateFacetResultsMerge(t *testing.T) {
t.Errorf("expected %#v, got %#v", expectedFrs, frs1)
}
}

func TestAvailableTerms(t *testing.T) {
tfs := &TermFacets{}
tfs.Add(
&TermFacet{
Term: "term",
Count: 10,
},
)
tfs.Add(
&TermFacet{
Term: "term",
Count: 20,
},
)

terms := tfs.AvailableTerms()
if len(terms) != 1 {
t.Errorf("expected 1 terms, got %d", len(terms))
}

if terms[0].Count != 30 {
t.Errorf("expected 60, got %d", terms[0].Count)
}
}
Loading