4D Blog

Home Product Semantic search: querying by vector similarity

Semantic search: querying by vector similarity

October 15, 2025

Product

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().

HDI Query Vectors

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.

Discuss

Tags 21, AI, Vector Search, Vectors

Latest related posts

  • December 3, 2025

    Give AI to a 30 years old 4D application

  • November 28, 2025

    ORDA – Handle an event-driven logic during database operations

  • November 27, 2025

    ORDA – Permissions – Restrict / allow web access to the resources in one click

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy