In many 4D business applications, documents are everything — technical notes, reports, manuals, internal guides. But when users can’t remember the exact wording, finding the right one becomes slow, frustrating, or worse — impossible.
With 4D 20 R10, semantic search powered by AI vectors changes that. Instead of matching keywords, you match meaning. Users get the right document, even if they search in different words or a different language. It’s a smarter way to surface the knowledge hidden in your documents — fast, accurate, and built for how people actually search.
Let’s consider a concrete example: a user wants to locate a technical note that explains how to insert an image into a 4D Write Pro document. However, they may not recall the precise phrase used in the document.
How to search in 4D Write Pro documents
Searching through documents stored in a database is not a new concept. Traditionally, 4D developers have relied on keyword-based search to implement this functionality. Today, however, artificial intelligence opens the door to a far more flexible and powerful approach: semantic search using vectors.
Let’s take a closer look at both techniques and see how vectors revolutionize the way we search.
Traditional search methods with Keywords
This method involves extracting a document’s keywords and storing them in a text field. A search query is then performed by matching specific keywords entered by the user.
This approach is fast and effective when the query matches the document’s wording exactly. If the wording changes even slightly, the search fails. For example, a search for “insert image” would not find documents containing “add picture”.
Semantic Search with AI Vectors
Starting with 4D 20 R10, a powerful new feature is available: AI vector support. This enables semantic search, powered by artificial intelligence.
Unlike keyword search, semantic search focuses on meaning, not exact words. It allows users to find documents that express the same idea in different ways. For example, phrases like “insert image” or “add picture” convey the same idea.
Users can even enter queries in a language different from the document content. Meaning is preserved and matched — something keyword indexing cannot do.
How It Works
- Each document is analyzed and converted into a vector using the 4D AIKit component. This vector represents the semantic meaning of the text.
- A user query is also converted into a vector.
- The similarity between the query vector and the document vectors is calculated.
- The documents are returned, sorted from most to least relevant.
Implementation Example
A demo database was created to showcase this feature. It contains several 4D Write Pro documents representing technical notes.
Step 1 – Generate vectors for each document
Vector generation can be triggered when a document is saved to the database. In our example, a method was created to generate all vectors at once:
var $document : cs.NoteEntity
var $documents:=ds.Note.all()
For each ($document; $documents)
$txt:=WP Get text($document.Document)
$document.Vector:=cs.AIManagement.new($apiKey).generateVector($txt)
$document.save()
End for each
Step 2 – Generate the vector from the user’s query
When the user clicks on a search button, we create a vector from the user’s prompt
var $vector:=cs.AIManagement.new($apiKey).generateVector($prompt)
Step 3 – Compare vectors and sort by similarity
Finally, we return a list of documents, sorted by semantic relevance to the user’s query.
var $formula:=Formula(This.Vector.cosineSimilarity($vector)
$documents:=ds.Note.all().orderByFormula($formula); dk descending)
In this example, we return all documents ranked from most to least relevant. You can also limit the results presented to the user — for example, to the top five.
Live action
Conclusion
Semantic search with AI vectors marks a major evolution in how users interact with document-based data in 4D. It provides more flexibility, better accuracy, and a more intuitive user experience — without relying on strict keyword matching.
Whether for internal notes, documentation, or knowledge bases, vector search unlocks a new level of intelligence in your 4D applications.
An upcoming 4D version (not too far away) will introduce a special vector index type for fast analysis of large datasets.
And you? Has this blog sparked ideas for your own projects? What types of documents could benefit from semantic search in your applications?
