0

i am trying to integrate geoshape query from elastic search into my angularjs code to make an http request to fetch the data that is residing on elasticsearch local instance but the console throws error that invalid XMLhttp parameters. I guess it is related to how i am adding the geojson with my URL. Following is the function where i am creating the http request

function spatialsearch() {
            var _url = '127.0.0.1:9201/_search?';

            var b = {
                "query": {
                    "bool": {
                        "must": {
                            "match_all": {}
                        },
                        "filter": {
                            "geo_shape": {
                                "metadata.o2r.spatial.geometry": {
                                    "shape": {
                                        "type": "polygon",
                                        "coordinates": [
                                            [
                                                [-22.0, 76.0],
                                                [-27.0, 65.0],
                                                [-57.0, 65.0],
                                                [-59.0, 76.0],
                                                [-22.0, 76.0]
                                            ]
                                        ]
                                    },
                                    "relation": "contains"
                                }
                            }
                        }
                    }
                }
            };
            _url += b;
            return $http.get(_url);
            console.log("hello");

        }

Here is how i am calling http request in my js file in angularjs

function callingspatialsearch(){
  var deferred = $q.defer();
 httpRequests.
 spatialsearch()
 .then(cb1)
 .catch(errorHandler);
return deferred.promise;


    function cb1(response){
      $log.debug('result of search: %o', response);
      deferred.resolve(response);
    }

    function errorHandler(e){
      $log.debug('search error: %o', e);
      deferred.resolve(e);
    }
  }

In my HTML i am adding a button so that when user clicks on button the results are displayed.

<md-button ng-click="vm.button()" class="search-button md-primary md-raised white-font">Spatial</md-button>
Rehan
  • 371
  • 1
  • 6
  • 29
  • You probably need to post the query in the body instead of appending it to the URL and sending it with get. – Val Apr 20 '17 at 11:22
  • @Val i could not understand what you meant by posting the query in body? can you please show a small example? – Rehan Apr 20 '17 at 13:17
  • I mean like this: `$http.post(_url, b)` and remove the line `_url += b;` – Val Apr 20 '17 at 13:19
  • @Val ok but i want to fetch the data through this query from a local elasticsearch server so i think in that case get will be used instead of post. right? – Rehan Apr 20 '17 at 13:21
  • Not right. GET vs POST has nothing to do with local vs remote. When you send a body, you send it with POST, whether locally or remotely – Val Apr 20 '17 at 13:23
  • @Val thanks. the POST request worked perfectly although i am still receiving the XMLHttp Request error for which i am using CORS extension in chrome. I tried adding http behing my URL but it did not work out. Is there a permanent solution for this thing? – Rehan Apr 24 '17 at 08:16

1 Answers1

0

i am using $http.post(_url) instead of GET and that helps me in retrieving results

Rehan
  • 371
  • 1
  • 6
  • 29