You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
go get github.com/JanFalkin/tantivy-jpc/go-client/tantivy
A Simple Example
package main
import (
"fmt""io/ioutil""os""github.com/JanFalkin/tantivy-jpc/go-client/tantivy"
)
constofMiceAndMen=`A few miles south of Soledad, the Salinas River drops in close to the hillsidebank and runs deep and green. The water is warm too, for it has slipped twinklingover the yellow sands in the sunlight before reaching the narrow pool. On oneside of the river the golden foothill slopes curve up to the strong and rockyGabilan Mountains, but on the valley side the water is lined with trees—willowsfresh and green with every spring, carrying in their lower leaf junctures thedebris of the winter's flooding; and sycamores with mottled, white, recumbentlimbs and branches that arch over the pool`constoldMan="He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish."funcmain() {
tantivy.LibInit()
td, err:=ioutil.TempDir("", "tindex")
deferfunc() {
iferr==nil {
os.RemoveAll(td)
}
}()
builder, err:=tantivy.NewBuilder(td)
iferr!=nil {
panic(err)
}
idxFieldTitle, err:=builder.AddTextField("title", tantivy.TEXT, true)
iferr!=nil {
panic(err)
}
idxFieldBody, err:=builder.AddTextField("body", tantivy.TEXT, true)
iferr!=nil {
panic(err)
}
doc, err:=builder.Build()
iferr!=nil {
panic(err)
}
doc1, err:=doc.Create()
iferr!=nil {
panic(err)
}
doc2, err:=doc.Create()
iferr!=nil {
panic(err)
}
doc.AddText(idxFieldTitle, "The Old Man and the Sea", doc1)
doc.AddText(idxFieldBody, oldMan, doc1)
doc.AddText(idxFieldTitle, "Of Mice and Men", doc2)
doc.AddText(idxFieldBody, ofMiceAndMen, doc2)
idx, err:=doc.CreateIndex()
iferr!=nil {
panic(err)
}
idw, err:=idx.CreateIndexWriter()
iferr!=nil {
panic(err)
}
_, err=idw.AddDocument(doc1)
iferr!=nil {
panic(err)
}
_, err=idw.AddDocument(doc2)
iferr!=nil {
panic(err)
}
_, err=idw.Commit()
iferr!=nil {
panic(err)
}
rb, err:=idx.ReaderBuilder()
iferr!=nil {
panic(err)
}
qp, err:=rb.Searcher()
iferr!=nil {
panic(err)
}
_, err=qp.ForIndex([]string{"title", "body"})
iferr!=nil {
panic(err)
}
searcher, err:=qp.ParseQuery("Old Man")
iferr!=nil {
panic(err)
}
varsrmap[string][]strings, err:=searcher.Search()
iferr!=nil {
panic(err)
}
err=json.Unmarshal([]byte(s), &sr)
iferr!=nil {
panic(err)
}
ifsr["title"][1] !=oldMan {
panic("expcted value not received")
}
searcherAgain, err:=qp.ParseQuery("Mice Men")
iferr!=nil {
panic(err)
}
s, err=searcherAgain.Search()
iferr!=nil {
panic(err)
}
err=json.Unmarshal([]byte(s), &sr)
iferr!=nil {
panic(err)
}
ifsr["title"][1] !=ofMiceAndMen {
panic("expcted value not received")
}
}