yanyiwu / gojieba

"结巴"中文分词的Golang版本
MIT License
2.42k stars 302 forks source link

整合到bleve,发现没起作用 #73

Closed senlinmuvs closed 3 weeks ago

senlinmuvs commented 4 years ago

按照demo像下面这样不行,分词效果还是bleve默认的:

func (h *CreateIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    // find the name of the index to create
    var indexName string
    if h.IndexNameLookup != nil {
        indexName = h.IndexNameLookup(req)
    }
    if indexName == "" {
        showError(w, req, "index name is required", 400)
        return
    }

    indexMapping := bleve.NewIndexMapping()

    //
    err := indexMapping.AddCustomTokenizer("gojieba",
        map[string]interface{}{
            "dictpath":     gojieba.DICT_PATH,
            "hmmpath":      gojieba.HMM_PATH,
            "userdictpath": gojieba.USER_DICT_PATH,
            "type":         "gojieba",
            "idf":          gojieba.IDF_PATH,
            "stop_words":   gojieba.STOP_WORDS_PATH,
        },
    )
    if err != nil {
        panic(err)
    }
    err = indexMapping.AddCustomAnalyzer("gojieba",
        map[string]interface{}{
            "type":      "gojieba",
            "tokenizer": "gojieba",
        },
    )
    if err != nil {
        panic(err)
    }
    indexMapping.DefaultAnalyzer = "gojieba"

    // read the request body
    requestBody, err := ioutil.ReadAll(req.Body)
    if err != nil {
        showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
        return
    }

    // interpret request body as index mapping
    if len(requestBody) > 0 {
        err := json.Unmarshal(requestBody, &indexMapping)
        if err != nil {
            showError(w, req, fmt.Sprintf("error parsing index mapping: %v", err), 400)
            return
        }
    }

    newIndex, err := bleve.New(h.indexPath(indexName), indexMapping)
    if err != nil {
        showError(w, req, fmt.Sprintf("error creating index: %v", err), 500)
        return
    }
    newIndex.SetName(indexName)
    bhttp.RegisterIndexName(indexName, newIndex)
    rv := struct {
        Status string `json:"status"`
    }{
        Status: "ok",
    }
    mustEncode(w, rv)
}
github-actions[bot] commented 1 month ago

This issue has not been updated for over 1 year and will be marked as stale. If the issue still exists, please comment or update the issue, otherwise it will be closed after 7 days.

github-actions[bot] commented 3 weeks ago

This issue has been automatically closed due to inactivity. If the issue still exists, please reopen it.