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 search our demo dataset. It contains a small sample of questions from the quiz show Jeopardy!.
Imagine that you're running a quiz night, and you want to get some questions about "animals in movies". In a traditional database 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%'
)
This is a difficult query to write. Even worse, you would probably have to add the names of specific animals to the query as well.
The Weaviate query is much more intuitive. 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 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 text.
Vector similarities demo
If we run this query, you might expect to see responses like the ones we saw earlier.
{
Get {
JeopardyQuestion (
nearText: {
concepts: ["European geography"]
}
limit: 3
) {
question
answer
_additional {
distance
}
}
}
}
But, 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 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 identify related objects without the need for exact text matches.
- In 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.
Questions and feedback
If you have any questions or feedback, let us know in the user forum.