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

**URL:** <https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221>\
**Category:** Elasticsearch\
**Created:** [March 13, 2019, 11:14pm UTC](https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221 "2019-03-13T23:14:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RMNYC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rmnyc/32/41785_2.png) [@RMNYC](https://discuss.elastic.co/u/RMNYC)\
**Post date:** [March 13, 2019, 11:14pm UTC](https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221/1 "2019-03-13T23:14:25Z")

</div>

```auto
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:

```auto
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`:

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

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

```

results:

```auto
{
  "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?

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [March 14, 2019, 1:59pm UTC](https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221/2 "2019-03-14T13:59:07Z")

</div>

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](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](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)

Following seems to work OK:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![RMNYC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rmnyc/32/41785_2.png) [@RMNYC](https://discuss.elastic.co/u/RMNYC)\
**Post date:** [March 14, 2019, 2:00pm UTC](https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221/3 "2019-03-14T14:00:05Z")

</div>

Thanks! makes sense.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 11, 2019, 2:00pm UTC](https://discuss.elastic.co/t/multi-field-search-nested-properties-cant-get-it-to-work/172221/4 "2019-04-11T14:00:20Z")

</div>

Closed on April 11, 2019, 2:00pm UTC

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