2015-03-19 105 views
0

我自己編寫了一個彈性分析器,但配置分析器時遇到了一些問題。Elasticsearch自定義分析器配置

我安裝了我的分析儀bin/plugin --url file:///[path_to_thulac.jar] --install analysis-smartcn(基於smartcn,所以它的名字是smartcn)。並通過

curl -XPUT 'http://localhost:9200/about-index/_mapping/about' -d ' 
{ 
    "properties": { 
     "searchable_text": { 
      "type": "string", 
      "analyzer": "smartcn" 
     } 
    } 
}' 

當我打電話curl -XGET 'localhost:9200/_analyze?analyzer=smartcn&pretty' -d '心理學概論',我得到了配置映射「心理學」 &「概論」,它是我想要的答案。

但是當我打電話搜索API

curl 'http://localhost:9200/title-index/_search?pretty=true' -d '{ 
    "query" : { 
     "query_string": { 
      "default_field": "searchable_text", 
      "query": "心理", 
      "analyzer": "smartcn" 
     } 
    }, 
    "script_fields": { 
     "terms" : { 
      "script": "doc[field].values", 
      "params": { 
       "field": "searchable_text" 
      } 
     } 
    } 
}' 

terms: ["2014", "心理", "概論", "理學", "秋"]我對這個問題很困惑,有人可以告訴我爲什麼?謝謝。

回答

0

您的映射設置不正確。使用適當的設置映射,該記錄甚至不應該由您的查詢返回。如果你申請的分析如下圖所示的例子:

curl -XDELETE "localhost:9200/test-idx?pretty" 
curl -XPUT "localhost:9200/test-idx?pretty" -d '{ 
    "settings": { 
     "index": { 
      "number_of_shards": 1, 
      "number_of_replicas": 0 
     } 
    }, 
    "mappings": { 
     "doc": { 
      "properties": { 
       "searchable_text": { "type": "string", "analyzer": "smartcn" } 
      } 
     } 
    } 
} 
' 
curl -XPUT "localhost:9200/test-idx/doc/1?pretty" -d '{ 
    "searchable_text": "心理學概論2014秋" 
}' 
curl -XPOST "localhost:9200/test-idx/_refresh?pretty" 

以下搜索請求

curl "localhost:9200/test-idx/_search?pretty=true" -d '{ 
    "query" : { 
     "query_string": { 
      "default_field": "searchable_text", 
      "query": "心理學" 
     } 
    }, 
    "script_fields": { 
     "terms" : { 
      "script": "doc[field].values", 
      "params": { 
       "field": "searchable_text" 
      } 
     } 
    } 
} 
' 

將返回:

"fields" : { 
    "terms" : [ [ "2014", "心理學", "概論", "秋" ] ] 
} 

你從分析儀相同的結果,以及:

curl -XGET 'localhost:9200/test-idx/_analyze?field=doc.searchable_text&pretty' -d '心理學概論2014秋' 

{ 
    "tokens" : [ { 
    "token" : "心理學", 
    "start_offset" : 0, 
    "end_offset" : 3, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "概論", 
    "start_offset" : 3, 
    "end_offset" : 5, 
    "type" : "word", 
    "position" : 2 
    }, { 
    "token" : "2014", 
    "start_offset" : 5, 
    "end_offset" : 9, 
    "type" : "word", 
    "position" : 3 
    }, { 
    "token" : "秋", 
    "start_offset" : 9, 
    "end_offset" : 10, 
    "type" : "word", 
    "position" : 4 
    } ] 
} 

執行以下命令,以確保您的映射正確應用:

curl -XGET 'http://localhost:9200/about-index/_mapping' 
+0

文檔的searchable_text字段是「心理學概論2014秋」,我認爲令牌是由分詞器分割後的結果。 – dreamszl 2015-03-20 05:27:27

+0

@dreamszl看起來映射沒有正確應用,我已經更新了我的答案。 – imotov 2015-03-20 16:34:32

相關問題