Skip to content

Instantly share code, notes, and snippets.

@mosuka
Created July 10, 2016 06:04
Show Gist options
  • Save mosuka/941466572289a0e562c69484e51e719a to your computer and use it in GitHub Desktop.
Save mosuka/941466572289a0e562c69484e51e719a to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"github.com/blevesearch/bleve"
_ "github.com/blevesearch/bleve/analysis/analyzers/keyword_analyzer"
_ "github.com/blevesearch/blevex/lang/ja"
"os"
)
func main() {
var err error
// index mapping
jsonIndexMapping := []byte(`
{
"types": {
"Product": {
"enabled": true,
"dynamic": true,
"properties": null,
"fields": [
{
"name": "Title",
"type": "text",
"analyzer": "ja",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Body",
"type": "text",
"analyzer": "ja",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Category",
"type": "text",
"analyzer": "keyword",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Popularity",
"type": "number",
"store": true,
"index": true,
"include_in_all": true
},
{
"name": "Release",
"type": "datetime",
"store": true,
"index": true,
"include_in_all": true,
"date_format": "dateTimeOptional"
},
{
"name": "Type",
"type": "text",
"analyzer": "keyword",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
}
],
"default_analyzer": "standard"
}
},
"default_mapping": {
"enabled": true,
"dynamic": true,
"properties": null,
"fields": [
{
"name": "Title",
"type": "text",
"analyzer": "ja",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Body",
"type": "text",
"analyzer": "ja",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Category",
"type": "text",
"analyzer": "keyword",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
},
{
"name": "Popularity",
"type": "number",
"store": true,
"index": true,
"include_in_all": true
},
{
"name": "Release",
"type": "datetime",
"store": true,
"index": true,
"include_in_all": true,
"date_format": "dateTimeOptional"
},
{
"name": "Type",
"type": "text",
"analyzer": "keyword",
"store": true,
"index": true,
"include_term_vectors": true,
"include_in_all": true
}
],
"default_analyzer": "standard"
},
"type_field": "Type",
"default_type": "_default",
"default_analyzer": "standard",
"default_datetime_parser": "dateTimeOptional",
"default_field": "_all",
"store_dynamic": true,
"index_dynamic": true
}
`)
indexMapping := bleve.NewIndexMapping()
err = json.Unmarshal(jsonIndexMapping, indexMapping)
if err != nil {
fmt.Println(err)
return
}
// open a new index
indexPath := "example.bleve"
var index bleve.Index
_, err = os.Stat(indexPath)
if err == nil {
index, err = bleve.Open(indexPath)
} else {
index, err = bleve.New(indexPath, indexMapping)
}
if err != nil {
fmt.Println(err)
return
}
// read data file
jsonDocs := []byte(`
{
"1": {
"Title": "Bleve",
"Body": "Goで書かれた全文検索ライブラリ",
"Category": "ライブラリ",
"Popularity": 2.0,
"Release": "2014-04-18T00:00:00Z",
"Type": "Product"
},
"2": {
"Title": "Lucene",
"Body": "Javaで書かれた全文検索ライブラリ",
"Category": "ライブラリ",
"Popularity": 2.0,
"Release": "2001-09-01T00:00:00Z",
"Type": "Product"
},
"3": {
"Title": "Solr",
"Body": "Luceneをコアにした全文検索サーバー",
"Category": "サーバー",
"Popularity": 3.0,
"Release": "2004-07-21T00:00:00Z",
"Type": "Product"
},
"4": {
"Title": "Elasticsearch",
"Body": "Luceneをコアにした全文検索サーバー",
"Category": "サーバー",
"Popularity": 3.0,
"Release": "2010-02-08T00:00:00Z",
"Type": "Product"
}
}
`)
var docs interface{}
err = json.Unmarshal(jsonDocs, &docs)
if err != nil {
fmt.Println(err)
return
}
// index data
for ID, doc := range docs.(map[string]interface{}) {
if err != nil {
fmt.Println(err)
}
index.Index(ID, doc)
}
// search request
jsonSearchRequest := []byte(`
{
"query": {
"query": "Body:検索"
},
"size": 10,
"from": 0,
"fields": [
"Title",
"Body",
"Category",
"Popularity",
"Release",
"Type"
],
"facets": {
"カテゴリ": {
"size": 10,
"field": "Category"
},
"人気度": {
"size": 10,
"field": "Popularity",
"numeric_ranges": [
{
"name": "0-1",
"min": 0,
"max": 1
},
{
"name": "1-2",
"min": 1,
"max": 2
},
{
"name": "2-3",
"min": 2,
"max": 3
},
{
"name": "3-4",
"min": 3,
"max": 4
},
{
"name": "4-5",
"min": 4,
"max": 5
}
]
},
"リリース": {
"size": 10,
"field": "Release",
"date_ranges": [
{
"name": "2001-2010",
"start": "2001-01-01T00:00:00Z",
"end": "2010-12-31T23:59:59Z"
},
{
"name": "2011-2020",
"start": "2011-01-01T00:00:00Z",
"end": "2020-12-31T23:59:59Z"
}
]
}
},
"highlight": {
"style": "html",
"fields": [
"Title",
"Body"
]
}
}
`)
searchRequest := bleve.NewSearchRequest(nil)
err = json.Unmarshal(jsonSearchRequest, searchRequest)
if err != nil {
fmt.Println(err)
return
}
// search index
searchResult, err := index.Search(searchRequest)
if err != nil {
fmt.Println(err)
return
}
// show result
bytesSearchRequest, err := json.Marshal(searchResult)
fmt.Println(string(bytesSearchRequest))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment