0

I am using Amplify to do my Ajax calls to elasticsearch, however I am having issues filtering my results. Since I am passing everything as a URL I am unsure how to format it. The following returns 4 results when I pass firstName:John as the searchCriteria.

self.url = "http://leServer:lePort/people/person/_search?q=" + self.searchCriteria;

If I have firstName:John&lastName:Smith it returns 6 results because there are 2 smith records with a first name other than John.

If I run the following from my command prompt I get one result which is to be expected.

curl -XGET "http://leServer:lePort/people/person/_search?pretty=true" -d "{
  \"query\": {
    \"filtered\": {
      \"query\": {
        \"text\": {
          \"firstName\": \"John\"
        }
      },
      \"filter\": {
        \"query\": {
          \"text\": {
            \"lastName\": \"Smith\"
          }
        }
      }
    }
  }
}"

I tried using the following as my Ajax call however it does not return anything. I also tried with the \" that the curl request had.

self.url = "http://leServer:lePort/people/person/_search?" +"-d"+"{query:{filtered:{query:{text:{firstName:John}},filter:{query:{text:{lastName:Smith}}}}}}"
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
zmanc
  • 5,201
  • 12
  • 45
  • 90

1 Answers1

1

The query should be url encoded +firstName:John +lastName:Smith. By the way, it might be a bad idea to expose your elasticsearch server to outside world.

imotov
  • 28,277
  • 3
  • 90
  • 82
  • I tried using _search?q=lastName:Smith+firstName:John and it returned 6 results, 4 with a first name John, and 2 with last name Smith. Also about the outside exposure, its an intranet site. – zmanc Jan 02 '13 at 16:25
  • The default operator for queries is OR, you can change it by adding default_operator=AND to URL or by adding `+` before each field. Try this: `?q=%2blastName:Smith+%2bfirstName:John` – imotov Jan 02 '13 at 16:31
  • That worked perfectly. However I am curious why I need to use both + and %2b, while ++ does not give the desired results. – zmanc Jan 02 '13 at 17:08