Home > Back-end >  How to search nested properties in elasticsearch
How to search nested properties in elasticsearch

Time:05-29

I've got a set of records in one of my elastic indexes and I'm trying to execute a search query via postman, my current query looks like this and should be getting 2 results found, but getting zero. Anything wrong with it?

{
  "query": {
    "match": {
      "vehicle.CAR_WHEEL_DESCRIPTION": "A1=BB=C2C=D35"
    }
  }
} 

Current Response:

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

I tried doing something like this, but I'm getting not only exact matches, but some others as well, total 12 records (should be 2, exact match records)

{
  "query": {
    "nested": {
      "path": "vehicle",
      "query": {
        "bool": {
          "must": [
            { "match": { "vehicle.CAR_WHEEL_DESCRIPTION": 
"A1=BB=C2C=D35" } }
          ]
        }
      },
      "score_mode": "avg"
    }
  }
}

CodePudding user response:

This is my example based on your information.

PUT teste
{
  "mappings": {
    "properties": {
      "vehicle": {
        "type": "nested",
        "properties": {
          "CAR_WHEEL_DESCRIPTION": {
            "type": "text"
          }
        }
      }
    }
  }
}

POST teste/_doc
{
  "vehicle": {
    "CAR_WHEEL_DESCRIPTION": "A1=BB=C2C=D35"
  }
}

GET teste/_search
{
  "query": {
    "nested": {
      "path": "vehicle",
      "query": {
        "match": {
          "vehicle.CAR_WHEEL_DESCRIPTION": {
            "query": "A1=BB=C2C=D35"
          }
        }
      }
    }
  }
}
  • Related