0
votes

I'm building a search application with Elastic Search. I am planning to use Angular to send queries to the application using Angular's $http.get function and the elastic search URL query. So to get all records where the field userID = 1, I would query:

Set angular $http.get variable to:

elasticsearch:9200/application/_search?q=userID:1

This works fine, however things get awkward when I want include other Bool searches within the same query.

So, if I want to query for all records where the userID = 1 AND userName = person that do not contain the word "hello", I build the Angular $http.get to be:

elasticsearch:9200/application/_search?q=!Content:hello&userID:1&userName:person&default_operator=AND

But this does not return the desired output.

I think this is an issue with the first expression being a NOT search, which seems to set the whole query to a NOT query, rather than (NOT Content hello) AND (userID = 1) AND (username = person) which is what I want to find.

The documentation on elasticsearch for using the URL query is limited and I cant find anything about how to build up this kind of search.

Should I be using a propper DSL from the elasticsearch java client and not using the URL query at all?

1

1 Answers

1
votes

Try this:

_search?q=!Content:hello AND userID:1 AND userName:person

Remember that ampersands are used to separate parameters in the URL query string (which is unfortunately named the same as the ElasticSearch query string, which but refers to the entire set of parameters following the ? sign in the URL.) I think your URL as written is being parsed as

  • URL parameter 1: q=!Content:hello
  • URL parameter 2: userID:1
  • URL parameter 3: userName:person
  • URL parameter 4: default_operator=AND

Since userID:1 and userName:person are not being interpreted as part of the q parameter, which contains the ElasticSearch query string, ElasticSearch discards them.

As far as documentation goes, the q parameter contains an ElasticSearch query string, which is documented on its own page.