Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request contains unrecognized parameter:[type] in elastic search while making request for getting suggestion

I am new to elastic search in node js + express and i am struggling getting suggestion in elastic search.

Below things, i have completed before making request for es suggestions.

1) I have created es index(name- "movies") and type("movie") and mapping for index.

Index is :

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

mapping is :

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) Pushed some data from my db to es index "movies" with type "movie".

function for adding data in index :

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) Now i am trying to get suggestion results with following created function -

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);        
      }      
    })
  }

But i am getting below error while making request for getting suggestions for movie result.

{ 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}}}',

NOTE: i tried removing the type:"movie" from getMovieSuggestion function then it gives me following error :

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

Any help would be appreciated. Thanks.

like image 308
Siyaram Malav Avatar asked Nov 07 '22 17:11

Siyaram Malav


1 Answers

You shouldn't use

type:"movie"

in the call to elasticClient.suggest.

See here for reference, https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-suggest

It should just be:

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);        
      }      
    })
  }

Also, as per the OP's use of ES 5.5, the ES JS API still uses the _suggest endpoint, which is deprecated. The use of the _search endpoint with the appropriate params works as indicated in the comments.

like image 50
Animesh Avatar answered Nov 14 '22 22:11

Animesh