0
votes

I'm using django-elasticsearch-dsl-drf package and I have Postgres jsonField which I want to index. I tried to use Nestedfield in the document but without any properties since the json field is arbitrary, But I'm not able to search on that field, and I don't see anything related to that on their documentation.

Any idea how can I achieve this?

Mapping:

    {
  "mappings": {
    "_doc": {
      "properties": {
        "jsondata": {
          "type": "nested",
          "properties": {
            "timestamp": {
              "type": "date"
            },
          "gender": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "group_id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }, ...

I want to search on that field like jsondata.gender = x

1
Nested fields should be used for array of object. Also querying nested objects in Elasticsearch is a bit different. Please have look at : elastic.co/guide/en/elasticsearch/reference/current/nested.html. As the json field is arbitrary it would be nice if could try with object type. - skap
I tried both, and it mapping correctly. but how can I search on that field inside viewset? "as in their doc" - SDev
It would be nice if you could share the maping and some sample data. - skap
the question is updated. As on their doc, they have "nested field filter" but they know the keys that they want to search on it. for my case I don't know it. your help is appreciated! - SDev
was the query helpful? - skap

1 Answers

0
votes

Query for jsonfield.gender = x

GET <index>/_search
{
  "query": {
    "nested": {
      "path": "jsonfield",
      "query": {
        "term": {
          "jsonfield.gender.keyword": {
            "value": "x"
          }
        }
      }
    }
  }
}

NOTE: The query has not been verified using Kibana Dev Tools.