Back to Vespa

Vespa Query Examples

container-search/src/main/resources/queryExamples.md

8.683.246.4 KB
Original Source

Vespa Query Examples

This is a resource file containing example queries for the MCP client to help the client understand how to query a Vespa application. The example queries demonstrate how to transform Command-line queries into the format required by the MCP client.:

  • A String: yql
  • A Map: params

Note: The examples are based on the Vespa application and schemas used in the news tutorial. Most likely the Vespa application will be different so the example queries is meant for demonstration purposes only for the MCP client. The MCP client would need to adapt the queries to the specific application and schemas it is working with. For other queries, consult Vespa's query language documentation.

The examples are based on the news tutorial application with the following schemas:

news.sd

schema news {
    document news {

        field news_id type string {
            indexing: summary | attribute
            attribute: fast-search
        }
        field category type string {
            indexing: summary | attribute
        }
        field subcategory type string {
            indexing: summary | attribute
        }
        field title type string {
            indexing: index | summary
            index: enable-bm25
        }
        field abstract type string {
            indexing: index | summary
            index: enable-bm25
        }
        field body type string {
            indexing: index | summary
            index: enable-bm25
        }
        field url type string {
            indexing: index | summary
        }
        field date type int {
            indexing: summary | attribute
            attribute: fast-search
        }
        field clicks type int {
            indexing: summary | attribute
        }
        field impressions type int {
            indexing: summary | attribute
        }
        field embedding type tensor<float>(d0[50]) {
            indexing: attribute | index
            attribute {
                distance-metric: dotproduct
            }
        }
    }

    fieldset default {
        fields: title, abstract, body
    }

    rank-profile popularity inherits default {
        function popularity() {
            expression: if (attribute(impressions) > 0, attribute(clicks) / attribute(impressions), 0)
        }
        first-phase {
            expression: nativeRank + 100 * popularity
        }
    }
    rank-profile recommendation inherits default {
        first-phase {
            expression: closeness(field, embedding)
        }
    
    }
}

user.sd

schema user {
    document user {
        
        field user_id type string {
            indexing: summary | attribute
            attribute: fast-search
        }
        field embedding type tensor<float>(d0[50]) {
            indexing: summary | attribute
        }
    }

}

Example queries

  1. Search all documents of type news
  • Command-line query: vespa query 'yql=select * from news where true'
  • MCP client query:
json
    {
        "yql": "select * from sources where true",
        "parameters": {
            "sources": "news"
        }
    }
  1. Search all documents of the news schema with field abstract containing the word "election"
  • Command-line query: vespa query 'yql=select * from news where abstract contains "election"'
  • MCP client query:
json
    {
        "yql": "select * from sources where abstract contains 'election'",
        "parameters": {
            "sources": "news"
        }
    }
  1. Search all documents of the news schema with fieldset default containing the words "music" and "festival", and retrieve only two hits.
  • Command-line query: vespa query 'yql=select * from news where default contains "music" and default contains "festival"' 'hits=2'
  • MCP client query:
json
    {
        "yql": "select * from sources where default contains 'music' and default contains 'festival'",
        "parameters": {
            "sources": "news",
            "hits": 2
        }
    }
  1. Search all documents of the news schema with range search on field date.
  • Command-line query: vespa query 'yql=select * from news where date <= 20191110 AND date >= 20191108'
  • MCP client query:
json
    {
        "yql": "select * from sources where date <= 20191110 AND date >= 20191108",
        "parameters": {
            "sources": "news"
        }
    }
  1. Search all documents of the news schema with fieldset default containing the phrase "music festival" and order by date descending.
  • Command-line query: vespa query 'yql=select date from news where default contains phrase("music","festival") order by date desc'
  • MCP client query:
json
    {
        "yql": "select * from sources where default contains phrase('music','festival') order by date desc",
        "parameters": {
            "sources": "news"
        }
    }
  1. Search all documents of the news schema with fieldset default containing the word "music" and ranking by popularity.
  • Command-line query: vespa query 'yql=select * from news where default contains "music"' 'ranking=popularity'
  • MCP client query:
json
    {
        "yql": "select * from sources where default contains 'music'",
        "parameters": {
            "sources": "news",
            "ranking": "popularity"
        }
    }
  1. Search all documents of the user schema with field user_id equal to "U33527" and retrieve only the field embedding.
  • Command-line query: vespa query 'yql=select embedding from user where user_id contains "U33527"'
  • MCP client query:
json
    {
        "yql": "select embedding from sources where user_id contains 'U33527'",
        "parameters": {
            "sources": "user"
        }
    }
  1. Search all documents of the news schema and retrieve three documents for user "U33527" based on nearest neighbor search on the embedding field.
  • Command-line query: vespa query 'yql=select * from sources news where {targetHits:3}nearestNeighbor(embedding, user_embedding)' 'hits=3' 'ranking.features.query(user_embedding)={<U33527 embedding>}' 'ranking.profile=recommendation'
  • MCP client query:
json
    {
        "yql": "select * from sources where {targetHits:3}nearestNeighbor(embedding, user_embedding)",
        "parameters": {
            "sources": "news",
            "hits": 3,
            "ranking.features.query(user_embedding)": "<U33527 embeddings as a list>",
            "ranking": "recommendation"
        }
    }