Skip to content

Commit

Permalink
chore: changeset
Browse files Browse the repository at this point in the history
feat: add cli reindex and delete index

feat: create reindex guideline

chore: update courseDoc

fix: invalid command

chore: update index and scraper
  • Loading branch information
samithiwat committed Oct 31, 2023
1 parent 6a815c1 commit 568d1d1
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-baboons-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'api': minor
---

fix: implement s/u filter in search
39 changes: 39 additions & 0 deletions apps/opensearch-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,42 @@ go run ./cli/app.go index create --index-name=<index name> --filename=<filename>
```shell
go run ./cli/app.go index create --index-name=course-dev-1 --filename=dev/course-index.json
```

# Reindex

```shell
go run ./cli/app.go index reindex --index-name=<source index name> --dest=<dest index name> --debug=<true/false>
```

| name | description |
| ---------- | ---------------------------------- |
| index name | name of index |
| dest | name of destination index |
| debug | enable debug mode (`true`/`false`) |

## Example

### Reindex from course-dev-1 to course-dev-2

```shell
go run ./cli/app.go index reindex --index-name=course-dev-1 --dest=course-dev-2
```

# Delete Index

```shell
go run ./cli/app.go index reindex --index-name=<source index name> --dest=<dest index name> --debug=<true/false>
```

| name | description |
|------------|------------------------------------|
| index name | name of index |
| debug | enable debug mode (`true`/`false`) |

## Example

### Delete index course-dev-1

```shell
go run ./cli/app.go index delete --index-name=course-dev-1
```
87 changes: 87 additions & 0 deletions apps/opensearch-cli/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/opensearch-project/opensearch-go/v2"
Expand All @@ -18,6 +19,7 @@ import (
var (
fileName string
indexName string
destName string
isDebug bool
indexCmd *flag.FlagSet
)
Expand All @@ -28,6 +30,7 @@ func init() {
indexCmd = flag.NewFlagSet("index", flag.ExitOnError)

indexCmd.StringVar(&indexName, "index-name", "", "Index name")
indexCmd.StringVar(&destName, "dest", "", "Destination index name")
indexCmd.StringVar(&fileName, "filename", "", "Index filename")
indexCmd.BoolVar(&isDebug, "debug", false, "Enable the debug log")
flag.Parse()
Expand Down Expand Up @@ -74,6 +77,26 @@ func main() {

fmt.Println(strings.Repeat("_", 65))
fmt.Printf("successfully create the index, %s \n", indexName)
case "reindex":
if err := handleReindex(client); err != nil {
logger.Fatal(
"Error while creating the index",
zap.Error(err),
)
}

fmt.Println(strings.Repeat("_", 65))
fmt.Printf("successfully reindex from %s to %s \n", indexName, destName)
case "delete":
if err := handleDeleteIndex(client); err != nil {
logger.Fatal(
"Error while creating the index",
zap.Error(err),
)
}

fmt.Println(strings.Repeat("_", 65))
fmt.Printf("successfully delete the index, %s \n", indexName)
default:
logger.Fatal("Invalid input (invalid command)")
}
Expand Down Expand Up @@ -146,3 +169,67 @@ func handleCreateIndex(client *opensearch.Client) error {

return nil
}

type ReIndexBody struct {
Source struct {
Index string `json:"index"`
} `json:"source"`

Dest struct {
Index string `json:"index"`
} `json:"dest"`
}

func handleReindex(client *opensearch.Client) error {
if err := indexCmd.Parse(os.Args[3:]); err != nil {
return err
}

req, err := json.Marshal(ReIndexBody{
Source: struct {
Index string `json:"index"`
}{Index: indexName},

Dest: struct {
Index string `json:"index"`
}{Index: destName},
})

if err != nil {
return err
}

res, err := client.Reindex(
bytes.NewReader(req),
)

if err != nil {
return err
}

if res.IsError() {
return errors.New(res.String())
}

return nil
}

func handleDeleteIndex(client *opensearch.Client) error {
if err := indexCmd.Parse(os.Args[3:]); err != nil {
return err
}

res, err := client.Indices.Delete(
[]string{indexName},
)

if err != nil {
return err
}

if res.IsError() {
return errors.New(res.String())
}

return nil
}
5 changes: 5 additions & 0 deletions apps/opensearch-cli/docs/reindex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Reindex Guideline

1. Create new index (destination index) by using command `go run ./cli/app.go index create --index-name=<new index name> --filename=<file path>`
2. Run reindex command `go run ./cli/app.go index reindex --index-name=<old index name> --dest=<new index name>`
3. Delete the old one `go run ./cli/app.go index delete --index-name=<old index name>`
8 changes: 7 additions & 1 deletion apps/opensearch-cli/index/beta/course-index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"aliases": {
"course-beta": {}
"cgr-course-beta": {}
},
"settings": {
"index": {
Expand Down Expand Up @@ -73,6 +73,9 @@
}
}
},
"suggestions": {
"type": "completion"
},
"abbrName": {
"type": "text",
"analyzer": "courseabbranal"
Expand Down Expand Up @@ -106,6 +109,9 @@
},
"academicYear": {
"type": "keyword"
},
"creditHours": {
"type": "keyword"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion apps/opensearch-cli/index/dev/course-index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"aliases": {
"course-dev": {}
"cgr-course-dev": {}
},
"settings": {
"index": {
Expand Down Expand Up @@ -73,6 +73,9 @@
}
}
},
"suggestions": {
"type": "completion"
},
"abbrName": {
"type": "text",
"analyzer": "courseabbranal"
Expand Down Expand Up @@ -106,6 +109,9 @@
},
"academicYear": {
"type": "keyword"
},
"creditHours": {
"type": "keyword"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion apps/opensearch-cli/index/prod/course-index.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"aliases": {
"course-prod": {}
"cgr-course-prod": {}
},
"settings": {
"index": {
Expand Down Expand Up @@ -73,6 +73,9 @@
}
}
},
"suggestions": {
"type": "completion"
},
"abbrName": {
"type": "text",
"analyzer": "courseabbranal"
Expand Down Expand Up @@ -106,6 +109,9 @@
},
"academicYear": {
"type": "keyword"
},
"creditHours": {
"type": "keyword"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface CourseDoc {
studyProgram: StudyProgram
semester: Semester
academicYear: string
creditHours: string
}

@Processor({
Expand Down Expand Up @@ -76,6 +77,13 @@ export class QueueConsumerService {
genEdType: course.genEdType,
semester: course.semester,
studyProgram: course.studyProgram,
creditHours: course.creditHours,
suggestions: [
{
input: [course.courseNo, course.abbrName, course.courseNameEn, course.courseNameTh],
weight: 20,
},
],
} as CourseDoc,
doc_as_upsert: true,
},
Expand Down

0 comments on commit 568d1d1

Please sign in to comment.