2017-02-09 37 views
1

我使用ElasticSearch-2.3.5。我想在創建索引時將我的自定義分析器添加到映射中。如何添加自定義分析器來映射ElasticSearch-2.3.5進行部分搜索?

PUT /library

{ 
    "settings": { 
    "analysis": { 
     "tokenizer": { 
     "ngram_tokenizer": { 
      "type": "nGram", 
      "min_gram": "1", 
      "max_gram": "15", 
      "token_chars": [ 
      "letter", 
      "digit" 
      ] 
     } 
     }, 
     "analyzer": { 
     "index_ngram_analyzer": { 
      "type": "custom", 
      "tokenizer": "ngram_tokenizer", 
      "filter": [ 
      "lowercase" 
      ] 
     } 
     }, 
     "search_term_analyzer": { 
     "type": "custom", 
     "tokenizer": "keyword", 
     "filter": "lowercase" 
     } 
    } 
    }, 
    "mappings": { 
     "book": { 
     "properties": { 
      "Id": { 
      "type": "long", 
      "search_analyzer": "search_term_analyzer", 
      "index_analyzer": "index_ngram_analyzer", 
      "term_vector":"with_positions_offsets" 
      }, 
      "Title": { 
      "type": "string", 
      "search_analyzer": "search_term_analyzer", 
      "index_analyzer": "index_ngram_analyzer", 
      "term_vector":"with_positions_offsets" 
      } 
     } 
     } 
    } 
} 

我從official guide模板實例。

{ 
    "settings" : { 
     "number_of_shards" : 1 
    }, 
    "mappings" : { 
     "type1" : { 
      "properties" : { 
       "field1" : { "type" : "string", "index" : "not_analyzed" } 
      } 
     } 
    } 
} 

但我得到一個錯誤,試圖執行第一部分的代碼。還有就是我的錯誤:

{ 
    "error": { 
    "root_cause": [ 
     { 
     "type": "mapper_parsing_exception", 
     "reason": "analyzer [search_term_analyzer] not found for field [Title]" 
     } 
    ], 
    "type": "mapper_parsing_exception", 
    "reason": "Failed to parse mapping [book]: analyzer [search_term_analyzer] not found for field [Title]", 
    "caused_by": { 
     "type": "mapper_parsing_exception", 
     "reason": "analyzer [search_term_analyzer] not found for field [Title]" 
    } 
    }, 
    "status": 400 
} 

我能做到這一點,如果我把我的mappingssettings裏面,但我認爲這是錯誤的方式。所以我試着用標題的一部分來找到我的書。例如,我有「亞瑟王」書。我的查詢是這樣的:

POST /library/book/_search

{ 
    "query": { 
     "match": { 
      "Title": "kin" 
     } 
    } 
} 

什麼都不會被發現。我做錯了什麼?你可以幫幫我嗎?看來我的分析器和標記器不起作用。我怎樣才能得到「k」,「i」,「ki」,「king」等術語?因爲我認爲我現在只有兩個條款。有'國王'和'阿瑟'。

回答

1

您弄丟了search_term_analyzer分析儀,它應該是analyzer部分

PUT /library 
{ 
    "settings": { 
    "analysis": { 
     "tokenizer": { 
     "ngram_tokenizer": { 
      "type": "nGram", 
      "min_gram": "1", 
      "max_gram": "15", 
      "token_chars": [ 
      "letter", 
      "digit" 
      ] 
     } 
     }, 
     "analyzer": { 
     "index_ngram_analyzer": { 
      "type": "custom", 
      "tokenizer": "ngram_tokenizer", 
      "filter": [ 
      "lowercase" 
      ] 
     }, 
     "search_term_analyzer": { 
      "type": "custom", 
      "tokenizer": "keyword", 
      "filter": "lowercase" 
     } 
     } 
    } 
    }, 
    "mappings": { 
     "book": { 
     "properties": { 
      "Id": { 
      "type": "long",   <---- you probably need to make this a string or remove the analyzers 
      "search_analyzer": "search_term_analyzer", 
      "analyzer": "index_ngram_analyzer", 
      "term_vector":"with_positions_offsets" 
      }, 
      "Title": { 
      "type": "string", 
      "search_analyzer": "search_term_analyzer", 
      "analyzer": "index_ngram_analyzer", 
      "term_vector":"with_positions_offsets" 
      } 
     } 
     } 
    } 
} 

還要確保使用analyzer代替index_analyzer,後者在ES 2.x的

+0

我被棄用內對不起,但出了點問題。也許我應該把我的搜索分析器放在分析器之前' 「錯誤」: }' –

+0

你只需要 「時search_analyzer設置必須設置現場[標題分析]」:{ 「ROOT_CAUSE」: { 「類型」: 「mapper_parsing_exception」, 「理由」用'analyzer'替換'index_analyzer',因爲前者在ES 2.x中不推薦使用。 – Val

+0

也許是API有另一個變化,因爲我得到了另一個錯誤:「類型」:「mapper_parsing_exception」, 「原因」:「[Id]的映射定義具有不支持的參數:[search_analyzer:search_term_analyzer] [analyzer:index_ngram_analyzer] [term_vector:with_positions_offsets]「 }' –

相關問題