I cant search nested

hello , I am trying to search nested .
I dont know what I am doing wrong .

MY MAPPING

{
  "user-data-info": {
    "mappings": {
      "dynamic": "false",
      "properties": {
        "company": {
          "type": "nested"
        },
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd"
        }
      }
    }
  }
}

GET user-data-info/_search
{
  "query": {
    "match_all": {}
  }
}

RESULT - FOUND 1 RESULT

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user-data-info",
        "_id": "bb56iYkB8sgA-4K-OT_i",
        "_score": 1,
        "_source": {
          "timestamp": "2023-06-01",
          "company": {
            "id": 1,
            "name": "Nike"
          }
        }
      }
    ]
  }
}

SEACHING NESTED

GET /user-data-info/_search
{
  "query": {
    "nested": {
      "path": "company",
      "query": {
        "bool": {
          "must": [
            { "match": { "company.id": 1 } }
          ]
        }
      }
    }
  }
}

RESULT / FOUND 0

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

Hi @Murilo_Livorato,

The nested query that you're sending in looks right.

I duplicated the example that you gave, but my mappings looked a bit different from yours:

PUT /user-data-info/_mapping
{
  "properties": {
    "company": {
      "type": "nested"
    },
    "timestamp": {
      "type": "date",
      "format": "yyyy-MM-dd"
    }
  }
}

Using this mapping, and the following document:

POST user-data-info/_doc/1
{
  "company": {
    "id": 1,
    "name": "nike"
  }
}

The nested query you posted should work. Perhaps you can look at your index mappings and settings and make sure that it's correct?

Good luck!

I'm not sure that this really qualifies for nested docs:

{
  "timestamp": "2023-06-01",
  "company": {
    "id": 1,
    "name": "Nike"
  }
}

I'd do instead:

{
  "timestamp": "2023-06-01",
  "company": [{
    "id": 1,
    "name": "Nike"
  }]
}

I changed this field , and it worked -

"dynamic": "true " 

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