Search code examples
vespa

How to change ranking order for nearest neighbor search in Vespa?


Let's say I have such schema:

schema embeddings {
  document embeddings {
    field id type int {}
    field text_embedding type tensor<double>(d0[960]) {
      indexing: attribute
      attribute {
        distance-metric: euclidean
      }
    }
  }

  rank-profile distance {
    num-threads-per-search:1
    inputs {
      query(query_embedding) tensor<double>(d0[960])
    }
    first-phase {
      expression: distance(field, text_embedding)
    }
  }
}

After performing a query with such a body

body = {
    'yql': 'select * from embeddings where ([{\"targetHits\":10}] nearestNeighbor(text_embedding, query_embedding));',
    "hits":10,
    'ranking': {
        'profile': 'distance',
        'features': {
            'query(query_embedding)': [...]
        }
    },
}

I received a number of results sorted by the distance in descending order.

How can I change the query or schema to receive results sorted by the distance in ascending order?

I am aware of closeness operator but I need exactly what I described


Solution

  • Use for example -distance(field, embedding) then - you can write any math expression here.

    You can also have distance returned while ranking by something else by declaring it a summary-feature, for example:

      rank-profile distance {
        num-threads-per-search:1
        inputs {
          query(query_embedding) tensor<double>(d0[960])
        }
        first-phase {
          expression: closeness(field, text_embedding)
        }
        summary-features {
          distance(field, text_embedding)
        }
      }