ElasticSearch not failing but returning incorrect

2019-08-26 22:42发布

问题:

I switched to POST searches using ajax in my application so I can start using the date range. However it seems no matter what I post it keeps returning the first 10 results in my index. The true results are in the 30k range.

    amplify.request.define("searchPostRequest", "ajax", {
        url: "http://leServer:9200/people/person/_search",
        type: "POST",
        dataType: 'jsonp',
        contentType: 'application/json'
    });
    self.advancedSearchPostQuery = {
        query: {
           term: {
               locationNumber:479
            }
        }
    };
    console.log(self.advancedSearchPostQuery);
    amplify.request({
        resourceId: "searchPostRequest",
        data: JSON.stringify(self.advancedSearchPostQuery),
        success: function (data) {
            console.log(data.hits.hits.length);
        }
    });

回答1:

If this is your actual code, your problem might simply be that your advancedSearchPostQuery isn't valid JSON.

You need to apply quotes:

advancedSearchPostQuery = {
    "query": {
        "term": {
            "locationNumber": 479
        }
    }
}

And I'm not sure if you need to stringify the object, but I'm not familiar with amplifyJS, so double check on that as well if amplifyjs is expecting an object or a string.

If that doesn't help check if your query returns the correct results when running from command line through curl.



回答2:

After doing more debugging I found that the request was being sent as a GET even though I had explicitly set it to post. Moving the data type to json, from jsonp let the request be sent as a POST, which resolved the issue. However this causes an issue in IE where the request is not sent at all due to the request being sent to another domain.

    amplify.request.define("searchPostRequest", "ajax", {
        url: "http://leServer:9200/people/person/_search",
        type: "POST",
        dataType: 'json',
        contentType: 'application/json'
    });