With the growing importance of vector-based search in AI applications such as semantic search, recommendation engines, and natural language processing, 4D introduces native support for vector queries in the query() function. This enhancement brings vector similarity comparisons directly into the language of DataClass.query() and EntitySelection.query().
Querying by Vector Similarity
You can now search for entities whose vector fields are similar to a reference vector using familiar comparison operators: >, >=, <, and <=.
The basic syntax is:
// Call the OpenAI embeddings API to generate the vector for the input text
var $result:=$clientAI.embeddings.create("A general manager living in France"; $model)
Var $inputEmbedding:=$result.vector
var $results := ds.MyClass.query("myVectorField > :1"; {vector: $inputEmbedding})
This returns entities where the similarity with $myVector is greater than the default threshold (0.5), using the default similarity metric: cosine similarity.
You can fine-tune your search by explicitly defining both:
-
Which similarity metric is most relevant for your use case with the metric attribute
-
How similar a result must be to your reference vector to be considered relevant with the threshold attribute
var $comparisonVector := {vector: $inputEmbedding; metric: mk cosine; threshold: 0.4}
var $results := ds.MyTable.query("myVectorField <= :1"; $comparisonVector)
COMBINING SEMANTIC SEARCH WITH CLASSICAL QUERY
A key advantage of 4D’s approach is that semantic queries can be combined with traditional ORDA filters in the same query.
This enables you to create powerful and precise searches that combine meaning-based discovery with structured criteria.
var $comparisonVector := {vector: $myVector; metric: mk cosine; threshold: 0.4}
var $results := ds.MyTable.query("myVectorField <= :1 AND salary>100000"; $comparisonVector)
In this example, the query retrieves employees whose job descriptions are semantically close to the input text and whose salary exceeds 100,000.
WHEN TO USE (AND WHEN NOT TO)
Semantic Search (query) is ideal when you want your application to understand meaning rather than exact matches.
It’s perfect for:
-
Fuzzy or contextual searches (e.g., “find documents similar to this one”)
-
Natural language queries (e.g., “employees working in marketing and design”)
-
Recommendation engines and knowledge discovery features
However, it’s not designed for structured analytical tasks.
If your query is something like “the top 10 most paid employees”, a classical query is faster and more appropriate.
By combining both approaches, you can build search experiences that are both intelligent and precise. Semantic search adds remarkable power and blends perfectly with traditional ORDA attribute-based queries.
Conclusion
With Semantic Search (query) now built into 4D, you can easily create AI-driven search and discovery features in your apps.
Select the similarity metric that best suits your use case, adjust the relevance threshold, and combine semantic and structured filters for optimal results.
This new capability brings the power of AI-based understanding directly into your 4D data model, opening the door to smarter, context-aware queries.
Currently, vector fields are not indexed, but indexing support will be introduced in an upcoming version of 4D. This future enhancement will significantly improve performance and make Semantic Search even faster and more efficient for large datasets.
