Multi-field search + nested properties -- Can't get it to work

PUT test
{  
   "mappings":{  
      "folks":{  
         "properties":{  
            "works_at": {
              "type": "nested"
            }
         }
      }
   }
}


PUT /test/folks/1
{
  "type": "lawyer",
  "works_at": [
    { "location":"New York"},
    { "location":"Boston"}
  ]
}

PUT /test/folks/2
{
  "type": "lawyer",
  "works_at": [
       {"location":"Chicago"},
       {"location":"Boston"}
     ] 

}

PUT /test/folks/3
{
  "type": "writer",
  "works_at": [
       {"location":"San Francisco"},
       {"location":"Boston"}
     ] 
}

Then I search, Zero results:

GET /test/folks/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "works_at.location": "Boston"
          }
        },
        {
          "match": {
            "type": "lawyer"
          }
        }
      ]
    }
  }
}

If the location portion is taken off, the search works filtering on the type:

GET /test/folks/_search
{
  "query": {
    "bool": {
      "must": [

        {
          "match": {
            "type": "lawyer"
          }
        }
      ]
    }
  }
}

results:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "folks",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "type": "lawyer",
          "works_at": [
            {
              "location": "Chicago"
            },
            {
              "location": "Boston"
            }
          ]
        }
      },
      {
        "_index": "test",
        "_type": "folks",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "type": "lawyer",
          "works_at": [
            {
              "location": "New York"
            },
            {
              "location": "Boston"
            }
          ]
        }
      }
    ]
  }
}

What am I missing in my first multi-field search?

Hi @RMNYC,

your mapping defines the works_at field as nested. Using nested field type requires you to also use the nested query. See:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
and
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

Following seems to work OK:

{
  "query": {
    "bool": {
      "must": [
        { "match":
          { "type" : "lawyer" }
        },
	{ "nested": {
	    "path" : "works_at",
	    "query" : {
	      "match":
              { "works_at.location" : "Boston" }
	    }
	  }
        }
      ]
    }
  }
}

You could also consider using plain "object" mapping, unless you need the nested query functionality.

Thanks! makes sense.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.