0

I have just started with es and I want to know how can I write a query to perform a search like "3 beds in city 1" or "2 baths in City 1 near School 2" As per my understanding we can handle 3 beds/bed/bd/bed room using synonyms but how do i know that "3 beds in city 1" means I need to search 3 on beds field and City1 in city fields?

Here is my sample dataset.

{
    Address :
         {
            City : "City 1",
            Zip : 21222
         },
    Beds : 3,
    Baths : 2,
    Schools :   
         [
            "School 1",
            "School 2"
         ]

},
{
    Address :
         {
            City : "City 2",
            Zip : 21220
         },
    Beds : 1,
    Baths : 1,
    Schools :   
         [
            "School 3",
            "School 2"
         ]

}

Thanks in Advance!

Ankit Sarkar
  • 547
  • 1
  • 6
  • 20

1 Answers1

0

3 beds in city 1 is like a AND condition between 2 clauses. Use bool and must to construct the query. Try reading bool clauses : must, should and must not. Their combinations would serve what you are looking out.

query: {
  bool: {
   must: [{
         term: {
          address.city : {
           value: City1
           }
          },{
           term: {
            beds : {
            value: 3
           }
         }]
     }}
piyushGoyal
  • 1,079
  • 1
  • 11
  • 26