2017-09-02 68 views
2

我是js + express中彈性搜索的新手,我正在努力獲得彈性搜索中的建議。請求包含無法識別的參數:[type]在彈性搜索的同時提出建議請求

下面的東西,我已經完成,然後提出es建議的請求。

1)我創建了es索引(名稱 - 「電影」)和類型(「電影」)和索引映射。

指數:

return elasticClient.indices.create({ 
     index:"movies", 
     body : { 
     "settings" : { 
      "index" : { 
       "number_of_shards" : 5, 
       "number_of_replicas" : 2 
      } 
     } 
     } 
    }); 
    } 

映射:

createMovieMapping:function(){ 
    return elasticClient.indices.putMapping({ 
     index:"movies", 
     type:"movie", 
     body:{ 
      properties:{ 
      movieName : {type:"string"}, 
      suggest:{ 
       type:"completion", 
       analyzer:"simple", 
       search_analyzer:"simple", 
       payloads:true 
      } 
      } 
     } 
    }) 
    } 

2)被推離我的分貝一些數據來上課指數 「電影」,類型爲 「電影」。

在索引添加數據功能:

addDataToMovieEsIndex:function(document){ 
    return elasticClient.index({ 
     index:"movies", 
     type:"movie", 
     id:document._id, // for preventing duplicate insertion 
     body:{ 
     movieName:document.movie_name, 
     suggest:{ 
      input : document.movie_name.split(" "), 
      output : document.movie_name, 
      payload : document 
     } 
     } 
    }) 
    } 

3)我現在想獲得建議的結果與下面創建的函數 -

getMovieSuggestion: function(input){ 
    elasticClient.suggest({ 
     index:"movies", 
     type:"movie", 
     body : { 
     moviesuggest:{ 
      text: input, 
      completion:{ 
      field : "suggest", 
      fuzzy : true 
      } 
     } 
     } 
    },function(err,resp){ 
     if(resp){ 
     console.log('response ',resp) 
     } 
     if(err){ 
     console.log('error ',err);   
     }  
    }) 
    } 

但我得到以下錯誤,而用於製作要求獲得電影結果的建議。

{ Error: [illegal_argument_exception] request [/movies/_suggest] contains unrecognized parameter: [type] 
    at respond (/Users/siyaram.malav/malav/my_apps/movie-info-app/node_modules/elasticsearch/src/lib/transport.js:307:15) 
    at checkRespForFailure (/Users/siyaram.malav/malav/my_apps/movie-info-app/node_modules/elasticsearch/src/lib/transport.js:266:7) 
    at HttpConnector.<anonymous> (/Users/siyaram.malav/malav/my_apps/movie-info-app/node_modules/elasticsearch/src/lib/connectors/http.js:159:7) 
    at IncomingMessage.bound (/Users/siyaram.malav/malav/my_apps/movie-info-app/node_modules/lodash/dist/lodash.js:729:21) 
    at emitNone (events.js:91:20) 
    at IncomingMessage.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:974:12) 
    at _combinedTickCallback (internal/process/next_tick.js:80:11) 
    at process._tickCallback (internal/process/next_tick.js:104:9) 
    status: 400, 
    displayName: 'BadRequest', 
    message: '[illegal_argument_exception] request [/movies/_suggest] contains unrecognized parameter: [type]', 
    path: '/movies/_suggest', 
    query: { type: 'movie' }, 
    body: '{"moviesuggest":{"text":"Sultan","completion":{"field":"suggest","fuzzy":true}}}', 

注:我嘗試刪除類型:從getMovieSuggestion函數,那麼它給了我「電影」以下錯誤:

Error: [illegal_argument_exception] no mapping found for field [suggest] 

任何幫助,將不勝感激。 謝謝。

回答

1

在調用elasticClient.suggest你不應該使用

type:"movie" 

看到這裏作參考,https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-suggest

應該僅僅是:

getMovieSuggestion: function(input){ 
    elasticClient.suggest({ 
     index:"movies", 
     body : { 
     moviesuggest:{ 
      text: input, 
      completion:{ 
      field : "suggest", 
      fuzzy : true 
      } 
     } 
     } 
    },function(err,resp){ 
     if(resp){ 
     console.log('response ',resp) 
     } 
     if(err){ 
     console.log('error ',err);   
     }  
    }) 
    } 

而且,按照OP的使用ES 5.5,ES JS API仍然使用_suggest端點,它被廢棄。具有適當參數的_search端點的使用如註釋中所示。

+0

嗨Animesh感謝信息,但我試過。它給我錯誤: 錯誤:[illegal_argument_exception]找不到字段[建議]的映射。 我在關注這個參考:https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/ – SrmHitter9062

+0

Hi @ SrmHitter9062,成功創建了映射嗎?你能否獲取索引「電影」的映射「電影」並檢查。另外,您正在使用哪種Elasticsearch版本?我發現您的API調用正在使用最新的5.x版本中已棄用的ES _suggest端點。如果您使用的是Elasticsearch 5.x,那麼您應該在ES客戶端構造函數中設置「apiVersion」配置選項(https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration的.html#配置選項)。您正在使用的參考文章已使用Elasticsearch 2.x以及相應的JS API – Animesh

+0

映射已成功創建。 我正在使用5.5.0 ES版本並且是的,它已在最新的5.x版本中被棄用。 我試過_search終點,它的工作: 回報elasticClient.search({ 指數: '電影', 類型: 「電影」, 正文:{ 查詢:{ 匹配:{ 的movieName:輸入 } } } }); 但我不明白在ES客戶端構造函數中設置apiVersion? – SrmHitter9062