Last active
July 11, 2016 06:47
-
-
Save mosuka/1a91fbbffbb816b85c819d488fdadc26 to your computer and use it in GitHub Desktop.
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 ( | |
"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 | |
bytesIndexMapping := []byte(` | |
{ | |
"types": { | |
"Product": { | |
"enabled": true, | |
"dynamic": true, | |
"properties": { | |
"Category": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "text", | |
"analyzer": "keyword", | |
"store": true, | |
"index": true, | |
"include_term_vectors": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
}, | |
"Description": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "text", | |
"analyzer": "ja", | |
"store": true, | |
"index": true, | |
"include_term_vectors": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
}, | |
"Name": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "text", | |
"analyzer": "ja", | |
"store": true, | |
"index": true, | |
"include_term_vectors": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
}, | |
"Popularity": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "number", | |
"store": true, | |
"index": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
}, | |
"Release": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "datetime", | |
"store": true, | |
"index": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
}, | |
"Type": { | |
"enabled": true, | |
"dynamic": true, | |
"fields": [ | |
{ | |
"type": "text", | |
"analyzer": "keyword", | |
"store": true, | |
"index": true, | |
"include_term_vectors": true, | |
"include_in_all": true | |
} | |
], | |
"default_analyzer": "" | |
} | |
}, | |
"default_analyzer": "" | |
} | |
}, | |
"default_mapping": { | |
"enabled": true, | |
"dynamic": true, | |
"default_analyzer": "" | |
}, | |
"type_field": "Type", | |
"default_type": "Product", | |
"default_analyzer": "standard", | |
"default_datetime_parser": "dateTimeOptional", | |
"default_field": "_all", | |
"store_dynamic": true, | |
"index_dynamic": true, | |
"analysis": {} | |
} | |
`) | |
indexMapping := bleve.NewIndexMapping() | |
err = json.Unmarshal(bytesIndexMapping, indexMapping) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
/* | |
japaneseTextFieldMapping := bleve.NewTextFieldMapping() | |
japaneseTextFieldMapping.Analyzer = ja.AnalyzerName | |
keywordFieldMapping := bleve.NewTextFieldMapping() | |
keywordFieldMapping.Analyzer = keyword_analyzer.Name | |
numericFieldMapping := bleve.NewNumericFieldMapping() | |
dateTimeFieldMapping := bleve.NewDateTimeFieldMapping() | |
productMapping := bleve.NewDocumentMapping() | |
productMapping.AddFieldMappingsAt("Name", japaneseTextFieldMapping) | |
productMapping.AddFieldMappingsAt("Description", japaneseTextFieldMapping) | |
productMapping.AddFieldMappingsAt("Category", keywordFieldMapping) | |
productMapping.AddFieldMappingsAt("Popularity", numericFieldMapping) | |
productMapping.AddFieldMappingsAt("Release", dateTimeFieldMapping) | |
productMapping.AddFieldMappingsAt("Type", keywordFieldMapping) | |
indexMapping := bleve.NewIndexMapping() | |
indexMapping.AddDocumentMapping("Product", productMapping) | |
indexMapping.TypeField = "Type" | |
indexMapping.DefaultAnalyzer = "standard" | |
// show index mapping | |
bytesIndexMapping, err = json.Marshal(indexMapping) | |
fmt.Println(string(bytesIndexMapping)) | |
*/ | |
// 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 | |
bytesDocuments := []byte(` | |
{ | |
"1": { | |
"Name": "Bleve", | |
"Description": "Goで書かれた全文検索ライブラリ", | |
"Category": "ライブラリ", | |
"Popularity": 1.0, | |
"Release": "2014-04-18T00:00:00Z", | |
"Type": "Product" | |
}, | |
"2": { | |
"Name": "Lucene", | |
"Description": "Javaで書かれた全文検索ライブラリ", | |
"Category": "ライブラリ", | |
"Popularity": 2.5, | |
"Release": "2001-09-01T00:00:00Z", | |
"Type": "Product" | |
}, | |
"3": { | |
"Name": "Solr", | |
"Description": "Luceneをコアにした全文検索サーバー", | |
"Category": "サーバー", | |
"Popularity": 4.5, | |
"Release": "2004-07-21T00:00:00Z", | |
"Type": "Product" | |
}, | |
"4": { | |
"Name": "Elasticsearch", | |
"Description": "Luceneをコアにした全文検索サーバー", | |
"Category": "サーバー", | |
"Popularity": 5.0, | |
"Release": "2010-02-08T00:00:00Z", | |
"Type": "Product" | |
} | |
} | |
`) | |
var docs interface{} | |
err = json.Unmarshal(bytesDocuments, &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 | |
bytesSearchRequest := []byte(` | |
{ | |
"query": { | |
"query": "Description:検索" | |
}, | |
"size": 10, | |
"from": 0, | |
"fields": [ | |
"Name", | |
"Description", | |
"Category", | |
"Popularity", | |
"Release", | |
"Type" | |
], | |
"facets": { | |
"カテゴリ": { | |
"size": 10, | |
"field": "Category" | |
}, | |
"人気度": { | |
"size": 10, | |
"field": "Popularity", | |
"numeric_ranges": [ | |
{ | |
"name": "1未満", | |
"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 | |
}, | |
{ | |
"name": "5以上", | |
"min": 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": [ | |
"Name", | |
"Description" | |
] | |
} | |
} | |
`) | |
searchRequest := bleve.NewSearchRequest(nil) | |
err = json.Unmarshal(bytesSearchRequest, searchRequest) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// search index | |
searchResult, err := index.Search(searchRequest) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// show result | |
bytesSearchResult, err := json.Marshal(searchResult) | |
fmt.Println(string(bytesSearchResult)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment