Examples 1 - Queries
Vectors in action
Let's take a look at a few more examples of what you can do with Weaviate.
First, we will try vector searches by searching through our demo database. You will learn how to use Weaviate to retrieve objects based on their similarity, using various query types such as an input text, vector, or object.
You will also compare vector search with keyword search to compare and contrast the two techniques, before learning how to combine the two techniques through the use of filters.
Vector search demo
For our first example, let's look through our demo dataset which contains a small sample of questions from the quiz show Jeopardy!.
Imagine that you're running a quiz night, and want to get some questions around the category of "animals in movies". You could look for word matches - perhaps something like:
SELECT question, answer
FROM jeopardy_questions
WHERE (question LIKE '%animal%' OR question LIKE '%creature%' OR question LIKE '%beast%')
AND (question LIKE '%movie%' OR question LIKE '%film%' OR question LIKE '%picture%' OR question LIKE '%cinema%')
But this is very difficult. You likely need to know the names of the specific animals to carry this out.
Not so much with Weaviate, though. See what happens when we run the following query:
animals in movies
See the full query
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["animals in movies"]
}
limit: 3
) {
question
answer
}
}
}
Weaviate retrieved these as the top answers:
- meerkats: Group of mammals seen here like Timon in The Lion King
- dogs: Scooby-Doo, Goofy & Pluto are cartoon versions
- The Call of the Wild Thornberrys: Jack London story about the dog Buck who joins a Nick cartoon about Eliza, who can talk to animals
See the JSON response
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "meerkats",
"question": "Group of mammals seen <a href=\"http://www.j-archive.com/media/1998-06-01_J_28.jpg\" target=\"_blank\">here</a>: [like Timon in <i>The Lion King</i>]"
},
{
"answer": "dogs",
"question": "Scooby-Doo, Goofy & Pluto are cartoon versions"
},
{
"answer": "The Call of the Wild Thornberrys",
"question": "Jack London story about the dog Buck who joins a Nick cartoon about Eliza, who can talk to animals"
}
]
}
}
}
Note just how relevant the results are, despite none of them including the word "animal" or the word "movie", let alone both!
This is exactly why vector searches are so useful. They can identify related objects without the need to match exact texts.
Vector similarities demo
What if we run this query? What will we get back?
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["European geography"]
}
limit: 3
) {
question
answer
_additional {
distance
}
}
}
}
Take a look at this response. Do you notice any additional information?
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"distance": 0.15916324
},
"answer": "Bulgaria",
"question": "A European republic: Sofia"
},
...
]
}
}
}
See the full JSON response from Weaviate
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"distance": 0.15916324
},
"answer": "Bulgaria",
"question": "A European republic: Sofia"
},
{
"_additional": {
"distance": 0.16247147
},
"answer": "Balkan Peninsula",
"question": "The European part of Turkey lies entirely on this peninsula"
},
{
"_additional": {
"distance": 0.16832423
},
"answer": "Mediterranean Sea",
"question": "It's the only body of water with shores on the continents of Asia, Africa & Europe"
}
]
}
}
}
The difference is that the response now contains a distance
value.
A distance
is indicative of the degree of similarity between the returned object and the query.
If you're wondering exactly what that means, and who decides how similar any two objects or concepts are, those are great questions! We will cover those in more detail later.
For now, just keep in mind that smaller distances mean two objects are more similar to each other.
Review
Key takeaways
- Vector searches can be very effective as they can identify related objects without the need for exact text matches.
- When using vector searches, distance values indicate the degree of similarity between the returned object and the query.
- Smaller distances indicate greater similarity.
- Vector searches can be combined with keyword searches and filtering techniques for more refined search results.