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

create index as part of the test #1084

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions backend/search/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ var typeToIndexMap = map[string]string{
}

// doElasticSearch is its own function so that we don't have to deal with gin.Context when testing.
func doElasticSearch(ctx context.Context, client *elasticsearch.TypedClient, query common.SearchQuery, roles []string, languages []string) (common.SearchResult, error) {
func doElasticSearch(ctx context.Context, client *elasticsearch.TypedClient, query common.SearchQuery, roles []string, languages []string, indexNameParameter string) (common.SearchResult, error) {
now := time.Now().Unix()

templateParams := &elasticQueryParams{
Expand Down Expand Up @@ -243,6 +243,9 @@ func doElasticSearch(ctx context.Context, client *elasticsearch.TypedClient, que
if query.Type != nil {
indexName = typeToIndexMap[*query.Type]
}
if indexNameParameter != "" {
indexName = indexNameParameter
}
Comment on lines 243 to +248
Copy link
Member

Choose a reason for hiding this comment

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

The index is set based on what you are queriying. You should not be able to query for episodes and spec to search the season index.
If that is needed for testing then initialization of the client should accept index config.


qResult, err := client.Search().Index(indexName).Raw(jsonQuery).Do(ctx)
if err != nil {
Expand Down Expand Up @@ -347,5 +350,5 @@ func (service *Service) SearchElastic(ctx *gin.Context, query common.SearchQuery

languages := user.GetLanguagesFromCtx(ctx)

return doElasticSearch(ctx, service.elasticClient, query, roles, languages)
return doElasticSearch(ctx, service.elasticClient, query, roles, languages, "")
}
37 changes: 31 additions & 6 deletions backend/search/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/log"
"github.com/elastic/go-elasticsearch/v8/typedapi/indices/putmapping"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v4"
"math/rand"
"os"
"testing"
Expand Down Expand Up @@ -47,12 +47,32 @@ func Test_ElasticQueryBasic(t *testing.T) {
}

client.Indices.Create(testIndexName).Do(ctx)
client.Indices.PutMapping(testIndexName).Request(&putmapping.Request{Properties: map[string]types.Property{
"title": mapping,
}})
client.Indices.Delete(testIndexName).Do(ctx)
//client.Indices.PutMapping(testIndexName).Request(&putmapping.Request{Properties: map[string]types.Property{
// "title": mapping,
//}})

t.Skip(fmt.Sprintf("should use %d as index name", randomNumber))
item := searchItem{
ID: "123",
LegacyID: nil,
Published: true,
Type: "",
Roles: nil,
Tags: nil,
Image: nil,
Title: common.LocaleString{"en": null.NewString("Jesus", true)},
Description: nil,
Header: nil,
AgeRating: nil,
Duration: nil,
AvailableFrom: 0,
AvailableTo: 0,
ShowID: nil,
ShowTitle: nil,
SeasonID: nil,
SeasonTitle: nil,
}

_, err := client.Index(testIndexName).Id(item.ID).Request(item.toSearchObject()).Do(ctx)

limit := 10
searchType := "episode"
Expand All @@ -69,12 +89,17 @@ func Test_ElasticQueryBasic(t *testing.T) {
[]string{
"no", "en", "de",
},
testIndexName,
)

client.Indices.Delete(testIndexName).Do(ctx)

assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, 6, res.HitCount)

t.Skip(fmt.Sprintf("should use %d as index name", randomNumber))

// If you want to see the results in detail in terminal
//spew.Dump(res)
}