Getting Started with Vector-Based Search

ODSC - Open Data Science
4 min readMar 21, 2022

--

Editor’s note: Laura is a speaker for ODSC East 2022. Be sure to check out her talk, “Vector Database Workshop Using Weaviate,” to learn more about vector-based search!

Traditional search engines perform a keyword-based search. Such search engines return results that contain an exact match or a close variation of a search query, like searching for papers about “Covid-19“. Those keyword-based search engines work well in some cases, but fall short in use cases where semantics play a role. This means that traditional search engines do not take the actual meaning of the query and documents or data into account. Documents in a database that might be of interest might not be found, because different wording is used. This means that, for example, a paper with the word “coronavirus” in it will not be found with the search term “Covid-19“.

https://odsc.com/boston/

Vector databases

Vector-based search (or semantic search, neural search) tries to solve this problem. Vector search engines like Weaviate retrieve documents by meaning, not by exact matching keywords. With machine learning models like BERT or ResNet50, a vector database converts data objects (text, images, etc) to embeddings. Embeddings are vector representations, which are stored in a high-dimensional vector space. The following image shows a simplified example of embedded concepts and images. Note that some of the name “cat” lies close to an image of a cat. And that “dog” lies closer to “cat” than “banana“.

Figure 1: Simplified vector representation of various concepts

Similarity search

If we would search for “kitten” in our simple database, the machine learning model would calculate a vector for this word, and return the closest data points. Although there is no datapoint matching exactly the word “kitten“, the vector search engine would return objects like the image of the cat, because this is semantically close to the query. This is visualized in Figure 2.

Figure 2: Simplified query vector representation of the query “Kitten”

Using Weaviate to do a vector-based search

Weaviate is an open-source vector-based search engine. With Weaviate you can perform a semantic search to various data types (text, images, etc) as described above. To embed data and queries, you can use out-of-the-box models, use open models (e.g. from HuggingFace), or connect your own machine learning model. Weaviate is horizontally scalable, which enables you to scale ML models and search through big data very fast.

Try out a semantic search demo

To get a glimpse of how semantic search in practice works, you can try out the following demo. This demo contains a small dataset of around 3500 news articles, which are embedded with a BERT model. You can access this dataset on https://demo.dataset.playground.semi.technology. Using the Weaviate Console, you can query this dataset. You can use this link to access the GraphQL query module on this dataset directly.

With the following GraphQL query, you search the dataset for Articles that are around the topic of “kitten”. The query “kitten” will be vectorized using the BERT model, and Articles that are close to this vector will be retrieved. The title of the top articles along with the calculated “certainty” (ranging from 0.0–1.0) are shown.

{
Get{
Article(
nearText: {
concepts: ["kitten"]
}
){
title
_additional {
certainty
}
}
}
}

If you run this query, you’ll see that the results will all be around the topic of “kitten” or related (cats or pets).

The previous query uses the BERT model as a retriever model to vectorize textual articles and queries. With Weaviate you can also attach reader or generator models to enhance the search experience. An example is a Question Answering model, which works on top of the retriever model, and thus works with the results that the BERT model returns. A Q&A BERT model is attached to the demo setup, which extracts an answer for a given question from the list of retrieved articles (context documents). You can try this out with the following query:

{
Get {
Article(
ask: {
question: "What is the name of the lawyer cat?",
properties: ["summary"]
},
limit:1
) {
title
summary
_additional {
answer {
hasAnswer
certainty
property
result
startPosition
endPosition
}
}
}
}
}

Feel free to use this demo dataset to make your own queries!

Vector search for your own dataset

If you get excited by the technology described above, you can learn to apply Weaviate to your own dataset. Visit Weaviate’s documentation or join the (virtual) workshop during the ODSC East conference this April!

Original post here.

Read more data science articles on OpenDataScience.com, including tutorials and guides from beginner to advanced levels! Subscribe to our weekly newsletter here and receive the latest news every Thursday. You can also get data science training on-demand wherever you are with our Ai+ Training platform. Subscribe to our fast-growing Medium Publication too, the ODSC Journal, and inquire about becoming a writer.

--

--

ODSC - Open Data Science

Our passion is bringing thousands of the best and brightest data scientists together under one roof for an incredible learning and networking experience.