ElasticSearch ajax request always returns the same result -


i trying query elasticsearch using ajax, getting same result always, if change query remains same. seems match part not getting executed. because if add more field release date, getting data.

here html code:

<html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> </head> <body>         <h1>search result!</h1>         <br />         <input id="searchterm" />     <button id="search">search</button>     <div id="results"></div>     <script>       $("#search").click(function(e){         var data = {                 query:{match:{_all:$('#searchterm').val()}},                 pretty: true,                 fields: '_id'                 };                  alert(json.stringify(data));                $.ajax({                 url: 'http://localhost:9200/movies/_search',                 datatype: 'jsonp',                 type: 'post',                 crossdomain: true,                 data: data,                success: function(data) {                   //alert(""+json.parse(data.hits.total));                   $("#results").empty();                   $("#results").append("<p>results <b>" + $("#searchterm").val() + "</b></p>");                   $.each(data.hits.hits, function(i,item){                     $("#results").append("<div><a href='#'>" + item._id + "</a></div>");                   });                 }               });             });     </script>  </body> </html> 

output:

search result!

results furious

insurgent (2015)

star wars: episode vii - force awakens (2015)

ant-man (2015)

jurassic world (2015)

cinderella (2015)

daredevil (2003)

get hard (2015)

avengers: age of ultron (2015)

batman v superman: dawn of justice (2016)

home (2015)

the result not relevant entered keyword, output should movies contains furious in movie name. but, still getting first 10 movies.

any suggestion great help.

i got thing work, have listed changes below:

  1. change jsonp json
  2. added contenttype: 'application/json' in ajax request
  3. remove pretty: true data, giving parsing error.
  4. change data format json.stringify(data)

analyzed monitoring request header in chrome developer tool.

here working code:

<html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> </head> <body>         <h1>search result!</h1>         <br />         <input id="searchterm" />     <button id="search">search</button>     <div id="results"></div>     <script>       $("#search").click(function(e){         var data = {                 query:{match:{_all:$('#searchterm').val()}},                 fields: '_id'                 };                  alert(json.stringify(data));                $.ajax({                 url: 'http://localhost:9200/movies/_search',                 datatype: 'json',                 type: 'post',                 contenttype: 'application/json',                 crossdomain: true,                 data: json.stringify(data),                success: function(data) {                   //alert(""+json.parse(data.hits.total));                   $("#results").empty();                   $("#results").append("<p>results <b>" + $("#searchterm").val() + "</b></p>");                   $.each(data.hits.hits, function(i,item){                     $("#results").append("<div><a href='#'>" + item._id + "</a></div>");                   });                 }               });             });     </script>  </body> </html> 

i hope helps somebody.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -