0

I'm trying to search for keyword and then add nested queries for amenities which is a nested field of an array of objects.

With the query below I am able to search when I'm only matching one amenity id but when I have more than one it doesn't return anything.

Anyone have an idea what is wrong with my query ?

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "_geo_distance": {
        "geolocation": [
          100,
          10
        ],
        "order": "asc",
        "unit": "m",
        "mode": "min",
        "distance_type": "sloppy_arc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "fields": [
              "name^2",
              "city",
              "state",
              "zip"
            ],
            "fuzziness": 5,
            "query": "complete"
          }
        },
        {
          "nested": {
            "path": "amenities",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "amenities.id": "1"
                    }
                  },
                  {
                    "term": {
                      "amenities.id": "2"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
1
  • Can you show a document sample which suppose to match this query? Commented Jul 2, 2017 at 15:50

1 Answer 1

1

When you do:

"must": [
  {
    "term": {
      "amenities.id": "1"
    }
  },
  {
    "term": {
      "amenities.id": "2"
    }
  }]

What you're actually saying is find me any document where "amenities.id"="1" and "amenities.id"="2" which unless "amenities.id" is a list of values it won't work. What you probably want to say is find me any document where "amenities.id"="1" or "amenities.id"="2" To do that you should use should instead of must:

"should": [
      {
        "term": {
          "amenities.id": "1"
        }
      },
      {
        "term": {
          "amenities.id": "2"
        }
      }]
Sign up to request clarification or add additional context in comments.

2 Comments

Amenities is an array of objects, so I'm trying to match records than contain both of the amenities. Is this possible ? or do I need to restructure my data ?
Can you give an example of such document?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.