Search code examples
vespa

Query tensor in vespa.ai


Imitating: https://blog.vespa.ai/billion-scale-knn/

Command line: curl -s -d '{"yql":"select * from user where {\"targetHits\":10}nearestNeighbor(approximate, q_binary_code);","ranking.features.query(q_binary_code)":[1,2,3,4,5,6,7,8,9,10],"hits":10}' -H "Content-Type: application/json" -X POST http://localhost:8080/search/ | jq .

Error message:

{
  "root": {
    "id": "toplevel",
    "relevance": 1,
    "fields": {
      "totalCount": 0
    },
    "errors": [
      {
        "code": 4,
        "summary": "Invalid query parameter",
        "source": "content",
        "message": "Expected a tensor value of 'query(q_binary_code)' but has [1,2,3,4,5,6,7,8,9,10]"
      }
    ]
  }
}

Question: How pass q_binary_code?


Solution

  • With recent Vespa versions, you can define the query tensor in the schema. It must be defined

    schema code {
      document code {
        field id type int {..} 
        field binary_code type tensor<int8>(b[16]) {..}
     }
     rank-profile coarse-ranking {
        inputs {
          query(q_binary_code) tensor<int8>(b[16])
        }
        num-threads-per-search:12
        first-phase { expression { closeness(field,binary_code) } } 
     }
    
    

    You also must define the rank profile in the query request:

    curl -s -d '{"yql":"select * from user where {\"targetHits\":10}nearestNeighbor(binary_code, q_binary_code);","ranking.features.query(q_binary_code)":[1,2,3,4,5,6,7,8,9,10],"hits":10, "ranking": "coarse-ranking"}' -H "Content-Type: application/json" -X POST http://localhost:8080/search/ | jq .