Pregunta

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}}}}}}"
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top