Last active
December 22, 2015 01:19
-
-
Save philipsoutham/6395791 to your computer and use it in GitHub Desktop.
New example for new API design
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
package main | |
import ( | |
"fmt" | |
golucy "github.com/philipsoutham/golucy/v0.0.1" | |
"io/ioutil" | |
"os" | |
) | |
func main() { | |
ixLocation, _ := ioutil.TempDir("", "golucy_") | |
defer os.RemoveAll(ixLocation) | |
schema := golucy.NewSchema() | |
defer schema.Close() | |
schema.AddField(golucy.NewFTField("content", "en")) | |
schema.AddField(golucy.NewIdField("id")) | |
index := golucy.NewIndex(ixLocation, true /* create */, true /* truncate */, schema) | |
defer index.Close() | |
writer := index.NewIndexWriter() | |
defer writer.Close() | |
writer.AddDocs( | |
golucy.Document{"content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "id": "1"}, | |
golucy.Document{"content": "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "id": "2"}, | |
golucy.Document{"content": "Duis aute irure dolor in reprehenderit in voluptate velit essei cillum dolore eu fugiat nulla pariatur.", "id": "3"}, | |
golucy.Document{"content": "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "id": "4"}, | |
golucy.Document{"content": "a cat is an animal", "id": "5"}, | |
golucy.Document{"content": "a dog is an animal", "id": "6"}, | |
golucy.Document{"content": "a cat and a cat is an animal", "id": "7"}, | |
golucy.Document{"content": "cats and dogs are animals", "id": "8"}, | |
golucy.Document{"content": "you are an animal", "id": "9"}, | |
) | |
writer.Commit() | |
reader := index.NewIndexReader() | |
defer reader.Close() | |
query := reader.ParseQuery("(cat OR dog) animal") | |
numHits /*total num results found*/, hits /*[]*golucy.SearchResult*/ := reader.Search(query, | |
0, /*offset*/ | |
5, /*limit*/ | |
"id", /*field identifying document*/ | |
"content", /*document field to retrieve*/ | |
true, /*include matched terms*/ | |
) | |
for _, hit := range hits { | |
fmt.Printf("%d %+v\n", numHits, hit) | |
} | |
query.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment