PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to filter by score in Elastic KNN search?

 October 25, 2022     elasticsearch, knn     No comments   

Issue

I have index with following mapping:

{
  "test-2": {
    "mappings": {
      "properties": {
        "advert_id": {
          "type": "integer"
        },
        "fraud": {
          "type": "boolean"
        },
        "photos": {
          "properties": {
            "id": {
              "type": "integer"
            },
            "vector": {
              "type": "dense_vector",
              "dims": 3,
              "index": true,
              "similarity": "l2_norm"
            }
          }
        },
        "rating": {
          "type": "long"
        }
      }
    }
  }
}

Here is how my data is saved in Elastic:

 {
    "advert_id": 123,
    "fraud": true,
    "photos": [
      {
        "id": 456,
        "vector": [
          213.32,
          3.23,
          4.21
        ]
      }
    ]
  }

I want to search data with similar vectors according to KNN algorithm. Here is my query for that:

GET /test-2/_knn_search
{
  "knn": {
    "field": "photos.vector",
    "k": 1,
    "num_candidates": 5,
    "query_vector": [213.32, 3.23, 4.22]
  }
}

Elastic returns me a score per each hit. Question is how can I get data with score more than N? It know about min_score, but couldn't apply it in this query.


Solution

Now that the kNN search API (/_knn_search) has been integrated into the search API (/_search) since Elasticsearch 8.4.0, we can use the min_score option as per the documentation as follows:

- GET /test-2/_knn_search
+ GET /test-2/_search
{
  "knn": {
    "field": "photos.vector",
    "k": 1,
    "num_candidates": 5,
    "query_vector": [213.32, 3.23, 4.22]
  },
+ "min_score": N
}


Answered By - mmizutani
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing