0

If you want to do a simple Elasticsearch (ES) query, you can make a GET request using the URI method. In cURL, an simple example would be:

curl -X GET "https://www.example.com/vulcanIndex/_search?q=name:Spock"

Even though cURL doesn't exist in .NET, this is pretty easy to translate so it will work with HttpWebRequest.

Unfortunately, at the time of this post, the ES documentation said: "Not all search options are exposed when executing a search using this mode..." -
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html

Apparently, in order to do more "advanced" searches like Query String Queries, you have to actually pass a body with the search request which is presently shown on this Example Page:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

So apparently, if I wanted to find all Vulcan's that were not human that graduated from MIT or Starfleet academy, I might run a cURL command like this:

curl -X GET "https://www.example.com/vulcanIndex/_search" -H 'Content-Type: application/json' -d'
{
    "query": {
        "query_string": {
            "query": "(institution:"Starfleet Academy" OR institution: MIT) AND (NOT alternateSpecies:Human)"
        }
    }
}
'

Unfortunately, I don't have easy access to cURL from within ASP.NET. Most posts I've seen on the internet about emulating cURL behavior suggest I try to use the HttpWebRequest for that purpose; unfortunately, unlike cURL, HttpWebRequest does not seem to allow GET requests to pass body data:
C# HTTP Body with GET method
This poses a problem for me.

How do I perform "advanced" ES searches like a Query String Query from ASP.NET (without NEST) if the HttpWebRequest object will not allow me to send body data via GET requests?

Shawn Eary
  • 684
  • 2
  • 7
  • 21
  • You can use POST – Nishant Jan 31 '19 at 04:31
  • Is POST the only way? If I give the user the ability to POST, then the user might be able to do bad things like update or change properties of the index. I don't want the user to be able to do *any* updating at all, so I don't want the user to have POST privileges. – Shawn Eary Feb 01 '19 at 02:47
  • 1
    Why would you like to give direct access to elastic. There should be an application layer which accepts the parameters based on which the application generated the query, send it to elastic to execute it and then return the response. This way by application layer you can manage any level of privileges. – Nishant Feb 01 '19 at 03:12

0 Answers0