Skip to content

Commit

Permalink
Add index mapping, some refactor.
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 7, 2021
1 parent ca625b6 commit 5218ad6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
31 changes: 21 additions & 10 deletions elastic/elastic.go → controller/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,65 @@ import (

var client *elasticsearch.Client

// One Time Setup on init
func init() {

esCfg := elasticsearch.Config{
Addresses: []string{
"http://elastic:9200/",
},
}
var err error

client, err := elasticsearch.NewClient(esCfg)
// Get a new client
client, err = elasticsearch.NewClient(esCfg)

utils.HandleFatalError("Error creating client", err)

esInfo, err := client.Info()

var numRetries int = 0
// Retry till instance is up.
for err != nil {
// Wait for the instane to get up and running.
time.Sleep(3 * time.Second)
numRetries++
if numRetries == 5 {
if numRetries == 10 {
break
}
esInfo, err = client.Info()
}

utils.HandleFatalError("Coundn't connect to elastic instance", err)

// Connected to elaticSearch
log.Printf("%v", esInfo)

// Default Mapping the string.
err = createIndex(models.HtmlDocumentMapping)

utils.HandleFatalError("Index creation error:", err)

utils.HandleFatalError("Index Creation Error", err)
}

func GetEsClient() *elasticsearch.Client {
func Client() *elasticsearch.Client {
return client
}

func createIndex(mapping string) error {
res, err := client.Indices.Create("index01", client.Indices.Create.WithBody(strings.NewReader(mapping)))

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

if err != nil {
return err
}

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

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

return nil
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"log"
"net/http"

"sokemotor/elastic"
elastic "sokemotor/controller"
"sokemotor/models"

"github.com/gin-gonic/gin"
)

func main() {

_ = elastic.GetEsClient()
_ = elastic.Client()

app := gin.Default()
app.GET("/search/:query", searchHandler)
app.POST("/document", documentHandler)
app.POST("/indexHtml", processHtml)
app.POST("/indexHtml", processHTML)
app.Run("0.0.0.0:8080")
}

Expand All @@ -28,7 +28,7 @@ func documentHandler(c *gin.Context) {

}

func processHtml(c *gin.Context) {
func processHTML(c *gin.Context) {

var v models.HtmlDocument

Expand Down
34 changes: 28 additions & 6 deletions models/htmlDoc.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
package models

var IndexName = "myindex"

type HtmlDocument struct {
Url string `json:"url"`
Dom string `json:"dom"`
}

// For elastic-search indexing
const HtmlDocumentMapping string = `"mappings": {
"properties": {
"url" : {"type" : ""},
"htmltext" : {"type" : "text"},
"time_of_registry" : {"type" : "date"},
// Mapping
var HtmlDocumentMapping string = `{
"settings":{
"analysis":{
"analyzer":{
"my_analyzer":{
"type":"custom",
"tokenizer":"uax_url_email",
"filter":[
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"properties": {
"lastaccessed" : {"type" : "date"},
"url" : {"type" : "keyword"},
"htmltext" : {
"type" : "text",
"analyzer":"my_analyzer"
}
}
}
}`

0 comments on commit 5218ad6

Please sign in to comment.