Hi everyone, > I’m a developer integrating Type...
# community-help
a
Hi everyone,
I’m a developer integrating Typesense into our project, and I have a few questions regarding advanced search functionalities and user experience.
1 - Search Query Behavior and Operators:
Is there a configuration or functionality in Typesense that allows users to specify how search queries are processed? For instance, can users explicitly indicate:
All words must be included (e.g., using operators like AND or +).
Any words can match (e.g., similar to using OR).
Exact phrases (e.g., with "quoted text").
I’m particularly interested in whether Typesense supports query customization or operators similar to Google Search, which uses tools like +, -, AND, OR, and quotation marks to refine search behavior.
2 - Query Adjustments and User Feedback:
When a user performs a search, if Typesense modifies the query (e.g., due to typo tolerance or synonyms), is there a built-in mechanism to notify the user about these adjustments? For example, showing a message like “Showing results for [adjusted query]” while also providing an option to search using the exact query they entered.
Additionally, if we want to implement this behavior ourselves, is there an API or configuration setting that allows us to Identify when a query has been adjusted.
Re-run the search with the original input (disabling typo tolerance or synonyms) to display precise results.
These features are crucial for delivering a transparent and user-friendly search experience in our application. If such functionalities exist, could you guide us on how to configure them?
f
To answer your first question: While this isn't possible right out of the box, you could try out curation and try to apply filters based on the user's search input. Not sure to what extent will it match what you're trying to achieve here. For example, what you're describing with the AND solution, you could try to do that by saying that if the query contains the token
+
(and the
+
is not part of the
symbols_to_index
parameter in your collection schema), then change
drop_tokens_threshold=0
. Some documentation on
drop_tokens_threshold
:
If
drop_tokens_threshold
is set to a number
N
and a search query contains multiple words (eg:
wordA wordB
), if at least
N
results with both
wordA
and
wordB
in the same document are not found, then Typesense will drop
wordB
and search for documents with just
wordA
. Typesense will keep dropping keywords like this left to right and/or right to left, until at least
N
documents are found. Words that have the least individual results are dropped first. Set
drop_tokens_threshold
to
0
to disable dropping of words (tokens).
The OR one just sounds like the default behavior of how Typesense handles search queries. For exact queries, you could try to apply a an exact match by filtering by that field with the exact match operator
:=
e.g.
name:=<query_term>
It's not possible to search by documents that DON'T include your search term (or filter by them not including that term for that matter), so the
-
operator wouldn't be possible
For your second question: the
typo_prefix_score
parameter in the
hits
of your search response will be 0 if typo-tolerance didn't kick in for that result. So you can know if your search had to use typo-tolerance for that result if the
typo_prefix_score
is not 0. Essentially, you can use the
/multi_search
endpoint, search by that term with no typo tolerance and with typo tolerance at the same time (two searches in the same multisearch request). Then you'd either recommend that the user searches by a document that matches a result from the typo-tolerated search (let's say that the user searched
the best bur
and there's a document with
the best burger in town
. You'd recommend that the user searched
the best burger
, matching the
first_q
parameter (
the best burger
) that the typo-tolerant search would provide you.
Copy code
❯ curl "<http://localhost:8108/collections/pg-essays/documents/search?q=the%20best%20burg&query_by=title>" \
  -X GET \
  -H "Content-Type: application/json" \
  -H "X-TYPESENSE-API-KEY: xyz" \

{
  "facet_counts": [],
  "found": 1,
  "hits": [
    {
      "document": { "id": "1", "title": "the best burger in town" },
      "highlight": {
        "title": {
          "matched_tokens": ["the", "best", "burg"],
          "snippet": "<mark>the</mark> <mark>best</mark> <mark>burg</mark>er in town"
        }
      },
      "highlights": [
        {
          "field": "title",
          "matched_tokens": ["the", "best", "burg"],
          "snippet": "<mark>the</mark> <mark>best</mark> <mark>burg</mark>er in town"
        }
      ],
      "text_match": 1736172785157275769,
      "text_match_info": {
        "best_field_score": "3315687620608",
        "best_field_weight": 15,
        "fields_matched": 1,
        "num_tokens_dropped": 0,
        "score": "1736172785157275769",
        "tokens_matched": 3,
        "typo_prefix_score": 1
      }
    }
  ],
  "out_of": 2,
  "page": 1,
  "request_params": {
    "collection_name": "pg-essays",
   ** "first_q": "the best burger", // Notice this**
    "per_page": 10,
    "q": "the best burg"
  },
  "search_cutoff": false,
  "search_time_ms": 0
}
a
A huddle started
@Fanis Tharropoulos Thank you for the information. Let me clarify my scenario better so you can understand: I am building a global search page in my project, where I am implementing a search across multiple types of data, such as news, features, projects, jobs, people, etc. When I search, I use the multisearch feature to query different collections simultaneously. Symbol Handling in Search Queries: I want to know if there's a configuration in Typesense that allows the user's query to detect symbols like + or - (hyphen). For example, if a user searches for design+job, I want Typesense to treat this as two search terms (design and job) instead of interpreting it as one single query. If such a configuration exists, how can I implement it? If not, your previous solution for analytics is still better than having nothing. Detecting Typo Adjustments: Since I am searching across multiple fields in multiple collections, do I need to iterate over each record to check if typo_prefix_score > 0 to determine if a typo correction was applied? Alternatively, is there any metadata or flag that Typesense provides to indicate whether the search query was adjusted due to typos? If yes, can I use it to reperform the search with no_typo_tolerance to get an exact match? However your solution can be implemented to thanks for this.
f
What you're describing for your first question wouldn't be possible from Typesense directly. You'd have to setup a regex pattern on your side, and based on that, query typesense twice with different parameters, and concat the results together after
a
Okay, thanks 🙂
🙌 1