Skip to content

Commit

Permalink
Add searchResponce struct, more methods to elastic.
Browse files Browse the repository at this point in the history
Signed-off-by: Daksh Chauhan <dak-x@outlook.com>
  • Loading branch information
dak-x committed Aug 23, 2021
1 parent fdb70fe commit b104fe1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
# vendor/

.env
/config
/config
/soke-vise/
1 change: 1 addition & 0 deletions controller/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// MAKE A DISCORD SOURCE.
40 changes: 38 additions & 2 deletions controller/elastic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package elastic

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"sokemotor/models"
Expand All @@ -9,6 +12,7 @@ import (
"time"

"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)

var client *elasticsearch.Client
Expand Down Expand Up @@ -55,23 +59,55 @@ func Client() *elasticsearch.Client {

func createIndex(mapping string) error {

res, err := client.Indices.Exists([]string{models.IndexName})
exists, err := client.Indices.Exists([]string{models.IndexName})

if err != nil {
return err
}

var res *esapi.Response
// Index not found, so Creating one.
if res.IsError() {
if exists.IsError() {
res, err = client.Indices.Create("myindex", client.Indices.Create.WithBody(strings.NewReader(mapping)))
log.Println("INDEX CREATED")
}

// Index Creation Error.
if err != nil {
return err

} else if res.IsError() {
return fmt.Errorf("error: %s", res)
}

return nil
}

// Inserts the given htmlDocument into the ES Index.
func InsertIntoIndex(data models.HtmlDocument) error {

dataBytes, err := json.Marshal(data)

if err != nil {
return err
}

req := esapi.IndexRequest{
Index: "test",
Body: bytes.NewReader(dataBytes),
Refresh: "true",
}

res, err := req.Do(context.Background(), client)
defer res.Body.Close()

if err != nil {
return err
}

if res.IsError() {
return fmt.Errorf("Error Inserting into Index")
}

return nil
}
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
}

func searchHandler(c *gin.Context) {

}

func documentHandler(c *gin.Context) {
Expand All @@ -35,13 +36,21 @@ func processHTML(c *gin.Context) {
err := c.Bind(&v)

if err != nil {
log.Printf("Error %v", err)
} else {
log.Printf("Recevied: %v", v.Url)
log.Printf("indexHTML Error: %v", err)
return
}

err = elastic.InsertIntoIndex(v)

if err != nil {
log.Printf("indexHTML Error: %v", err)
return
}

// Acknowledge that document is created.
c.JSON(http.StatusCreated, gin.H{
"url": v.Url,
"Created": "true",
})

}
8 changes: 8 additions & 0 deletions models/searchResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

type SearchResponse struct {
Url string `json:"url"`
Title string `json:"title"`
BoxContent string `json:"content"`
LastAccessed string `json:"lastaccessed"`
}

0 comments on commit b104fe1

Please sign in to comment.