Translate searchSourceJSON to Query DQL

I am trying to convert searchSourceJSON attribute of savedObject in DQL. But I haven't found a way to do it. The idea is to be able to execute the query that is stored in the object and execute it directly in ElasticSearch.

Perhaps the buildEsQuery could be the key. But I am not an expert in Kibana and for me it is complicated. I've been at it for several days.

someone could help me? Thanks a you

KQL

 {
  "highlightAll": true,
  "version": true,
  "query": {
    "query": "",
    "language": "kuery"
  },
  "filter": [
    {
      "meta": {
        "alias": null,
        "negate": false,
        "disabled": false,
        "type": "phrase",
        "key": "category.keyword",
        "params": {
          "query": "Women's Accessories"
        },
        "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index"
      },
      "query": {
        "match_phrase": {
          "category.keyword": "Women's Accessories"
        }
      },
      "$state": {
        "store": "appState"
      }
    }
  ],
  "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.index"
}

DSL

{
  "query": {
"bool": {
  "must": [],
  "filter": [
    {
      "match_all": {}
    },
    {
      "match_phrase": {
        "category.keyword": "Women's Accessories"
      }
    }
  ],
  "should": [],
  "must_not": []
}
  }
}

Hello,

first a few warnings :slight_smile: relying on saved object format (outside of app owning that saved object) is not safe as that might change in any version. To do the conversion you first need to parse json, extract the searchsourceFields from it and pass that to data.search.searchSource.create(). Now you have a searchsource instance and you can call flatten() on it to convert it to elasticsearch DSL.