-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'public-api-refactoring'
* public-api-refactoring: v0.3.0 update README display some stats in -verbose mode breaking change: make quiet the default add support for gzipped input files update inline doc long tokens work better with reader fix Options package name starting public API refactorings
- Loading branch information
Showing
5 changed files
with
120 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,73 @@ | ||
package esbulk | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// Application Version | ||
const Version = "0.2.4" | ||
const Version = "0.3.0" | ||
|
||
// Options represents bulk indexing options | ||
type Options struct { | ||
Host string | ||
Port int | ||
Index string | ||
DocType string | ||
BatchSize int | ||
Verbose bool | ||
} | ||
|
||
// BulkIndex takes a set of documents as strings and indexes them into elasticsearch | ||
func BulkIndex(docs []string, options Options) error { | ||
url := fmt.Sprintf("http://%s:%d/%s/%s/_bulk", options.Host, options.Port, options.Index, options.DocType) | ||
header := fmt.Sprintf(`{"index": {"_index": "%s", "_type": "%s"}}`, options.Index, options.DocType) | ||
var lines []string | ||
for _, doc := range docs { | ||
if len(strings.TrimSpace(doc)) == 0 { | ||
continue | ||
} | ||
lines = append(lines, header) | ||
lines = append(lines, doc) | ||
} | ||
body := fmt.Sprintf("%s\n", strings.Join(lines, "\n")) | ||
response, err := http.Post(url, "application/json", strings.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
// > Caller should close resp.Body when done reading from it. | ||
// Results in a resource leak otherwise. | ||
response.Body.Close() | ||
return nil | ||
} | ||
|
||
// Worker will batch index documents that come in on the lines channel | ||
func Worker(id string, options Options, lines chan string, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
var docs []string | ||
counter := 0 | ||
for s := range lines { | ||
docs = append(docs, s) | ||
counter++ | ||
if counter%options.BatchSize == 0 { | ||
err := BulkIndex(docs, options) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if options.Verbose { | ||
log.Printf("[%s] @%d\n", id, counter) | ||
} | ||
docs = docs[:0] | ||
} | ||
} | ||
err := BulkIndex(docs, options) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if options.Verbose { | ||
log.Printf("[%s] @%d\n", id, counter) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Package: esbulk | ||
Version: 0.2.4 | ||
Version: 0.3.0 | ||
Section: utils | ||
Priority: optional | ||
Architecture: amd64 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters