diff --git a/backend/search/query.go b/backend/search/query.go index 50e81d7a..7e7d9032 100644 --- a/backend/search/query.go +++ b/backend/search/query.go @@ -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{ @@ -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 + } qResult, err := client.Search().Index(indexName).Raw(jsonQuery).Do(ctx) if err != nil { @@ -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, "") } diff --git a/backend/search/query_test.go b/backend/search/query_test.go index 270c2b12..492bae0b 100644 --- a/backend/search/query_test.go +++ b/backend/search/query_test.go @@ -3,12 +3,17 @@ package search import ( "context" "flag" + "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/types" "github.com/rs/zerolog" "github.com/stretchr/testify/assert" + "gopkg.in/guregu/null.v4" + "math/rand" "os" "testing" + "time" ) var elasticTestsEnabled = false @@ -22,18 +27,53 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +// https://github.com/bcc-code/bcc-media-platform/pull/1021 func Test_ElasticQueryBasic(t *testing.T) { - if !elasticTestsEnabled { - t.Skip("Ealstic tests not enabled. Enable with -elastic flag") - } + rand.Seed(time.Now().UnixNano()) + randomNumber := rand.Intn(1000000000000000) + testIndexName := fmt.Sprintf("bccm-integration-test-%d", randomNumber) ctx := context.Background() client := newElasticClient(ctx, ElasticConfig{ - URL: "http://localhost:9200/", - Username: "elastic", - Password: "bccm123", + CloudID: CloudId, + ApiKey: ApiKey, }) + mapping := types.NewNestedProperty() + mapping.Properties = map[string]types.Property{ + "total": types.NewIntegerNumberProperty(), + "free": types.NewIntegerNumberProperty(), + "used": types.NewIntegerNumberProperty(), + } + + client.Indices.Create(testIndexName).Do(ctx) + //client.Indices.PutMapping(testIndexName).Request(&putmapping.Request{Properties: map[string]types.Property{ + // "title": mapping, + //}}) + + 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" @@ -49,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) }