How to use query combined to KNN with ElasticSearch?

15 views Asked by At

I'm facing an issue, I need to made a KNN calculation on ElasticSearch on a nested. My main problem is about the query, I have a complete system to generate some queries related to the profile of the user, it's already implemented and I need to get the KNN from the documents that matches this given query.

Here is my actual code, with a really simple query, but take in account that in 99% of the time it's really more complicated with a combinaison of bool/must/must_not.

POST feedbacks/_search
{
  "fields": [
    "id",
    "created_at",
    "satisfaction_ratio"
  ],
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "id": 1190473 // Example query that doesn't work for KNN calculation
                }
              }
            ]
          }
        }
      ]
    }
  },
  "knn": {
    "inner_hits": {
      "_source": false,
      "fields": [
        "question_answers.value"
      ],
      "size": 1
    },
    "field": "question_answers.embedding",
    "k": 2,
    "num_candidates": 2,
    "query_vector": [
      -0.031112670898438,
      0.04901123046875,
      0.05078125,
      // 1024 dimensional
    ]
  }
}
0

There are 0 answers