SemanticSearch[index,query]
finds the items similar to query inside index.
SemanticSearch[index,query->f]
filters the results using the function f.
SemanticSearch[index,query,prop]
returns the specified property prop.
SemanticSearch
SemanticSearch[index,query]
finds the items similar to query inside index.
SemanticSearch[index,query->f]
filters the results using the function f.
SemanticSearch[index,query,prop]
returns the specified property prop.
Details and Options
- SemanticSearch performs a search on the semantic index using a query to find and retrieve similar text items.
- Valid index specifications are:
-
"name" a string matching a named semantic search index SemanticSearchIndex[…] a valid SemanticSearchIndex object - The input to the function f is the same annotation specified when the index was created.
- Possible values for prop include:
-
"Distance" distance between the encoded query and item "Item" item that matches "query" (default) "ItemEmbedding" encoded item "Label" item label "Query" specified query "QueryEmbedding" encoded query "Tags" item tags "Tags""tag" a single item tag "Tags"{"tag1",…} multiple item tags {prop1,…} a list of properties All an Association with all the properties - The following options can be given:
-
MaxItems 10 how many items are returned ProgressReporting $ProgressReporting whether to display progress information RerankingMethod Automatic how to reorder items once retrieved - Values for the RerankingMethod include:
-
Automatic default reranking None no reranking Tiny, Small, Medium, Large predefined model sizes "string" a generic SemanticRanking model {"string",size} a specific-sized SemanticRanking model f a custom reranking function <|opt1 val1,…|> detailed options specifying reranking parameters - A custom ranker f must operate on the reference string and a list of strings to produce a vector of the same length.
- Detailed options for the RerankingMethod include:
-
"Function" SemanticRanking function ranking the candidate items "MaxItemMultiplier" 2 multiple of MaxItems provided as candidates "MinItems" 10 minimum number of candidate items "Query" Automatic query string used for ranking "Target" "Item" which property prop the function operates on - With "Query"Automatic, the input query is used for the reranking as well.
- When reranking with "MinItems"min, "MaxItemMultiplier"k and MaxItemsmax, the initial number of candidate items is Max[min,k*max].
Examples
open all close allBasic Examples (1)
Create a SemanticSearchIndex:
index = CreateSemanticSearchIndex[ResourceData["Alice in Wonderland"]]Extract the most relevant quote from Alice in Wonderland:
SemanticSearch[index, "Off with their heads", MaxItems -> 1]Scope (3)
Create a SemanticSearchIndex with a single source:
index = CreateSemanticSearchIndex[ResourceData["Alice in Wonderland"]]Find index items closest to a given query:
Snippet /@ SemanticSearch[index, "Talking insects", MaxItems -> 3]Get a list of available result properties:
SemanticSearch[index, "Talking insects", "Properties"]Extract a specific item property:
SemanticSearch[index, "Talking insects", "Distance"]SemanticSearch[index, "Talking insects", {"Item", "Distance"}, MaxItems -> 2]//ShortRetrieve all available properties:
Short@SemanticSearch[index, "Talking insects", All, MaxItems -> 2]Define an index with labeled sources:
index = CreateSemanticSearchIndex[{WikipediaData["cat"] -> "cat", WikipediaData["dog"] -> "dog"}]The label is automatically returned when searching:
SemanticSearch[index, "sense of smell"]Snippet /@ SemanticSearch[index, "sense of smell", "Item"]Return the item and the label:
Short /@ SemanticSearch[index, "sense of smell", {"Label", "Item"}, MaxItems -> 2]Filter to only search items with a specific label:
SemanticSearch[index, "sense of smell" -> MatchQ["dog"]]Define an index with tagged sources:
index = CreateSemanticSearchIndex[{WikipediaData["wolfram"] -> <|"Website" -> "Wikipedia", "Date" -> DateObject[{2024, 7, 25}, "Day"]|>, URL["https://www.wolfram.com"] -> <|"Website" -> "Wolfram", "Date" -> DateObject[{2024, 6, 12}, "Day"]|>}]The tags are automatically returned when searching:
SemanticSearch[index, "wolfram language"]SemanticSearch[index, "wolfram language", "Tags" -> "Website"]SemanticSearch[index, "wolfram language", "Tags" -> {"Website", "Date"}]Return tags and other properties:
SemanticSearch[index, "wolfram language", {"Distance", "Tags" -> "Website"}]Filter for a specific tag value:
SemanticSearch[index, "wolfram language" -> Function[#Website == "Wolfram"]]Options (5)
MaxItems (1)
RerankingMethod (4)
Search with automatic reranking:
index = CreateSemanticSearchIndex[ResourceData["Alice in Wonderland"]]AbsoluteTiming@First@SemanticSearch[index, "Why was Alice falling?"]Disabling reranking can make the results faster but may degrade results:
AbsoluteTiming@First@SemanticSearch[index, "Why was Alice falling?", RerankingMethod -> None]Specify a custom reranking method as a function of the items:
First@SemanticSearch[index, "Talking animals", RerankingMethod -> (-StringLength[#1]&)]Specify the reranker as a function of the items and the query:
First@SemanticSearch[index, "Caterpillar", RerankingMethod -> (StringCount[#1, #2, IgnoreCase -> True]&)]Reranking defaults to a minimum of 10 candidate items retrieved from the database, then returns the requested number of MaxItems:
index = CreateSemanticSearchIndex[{
"Jellyfish drift with ocean currents.",
"Starfish regenerate lost body parts.",
"Crayfish dig burrows in mud.",
"Cuttlefish change color for camouflage.",
"Silverfish infest homes, eating paper.",
"Salmon migrate upstream to spawn.",
"Starfish live on ocean floors.",
"Shellfish create shells from calcium."
}]SemanticSearch[index, "Fish", MaxItems -> 1]Disabling reranking will give the first result from the initial search:
SemanticSearch[index, "Fish", RerankingMethod -> None, MaxItems -> 1]Reduce the minimum number of candidate items provided to the reranker:
SemanticSearch[index, "Fish", RerankingMethod -> <|"MinItems" -> 2|>, MaxItems -> 1]The number of candidates is calculated by taking the larger of "MinItems" and the product of the "MaxItemMultiplier" method option and the specified number of MaxItems:
SemanticSearch[index, "Fish", RerankingMethod -> <|"MinItems" -> 2, "MaxItemMultiplier" -> 4|>, MaxItems -> 1]Create an index with tags corresponding to the frequency of words in the text:
words = KeyValueMap[#1 -> <|"Count" -> #2 /. _Missing -> 0|>&, WordCounts[ResourceData["Alice in Wonderland"]]];
Short[words]index = CreateSemanticSearchIndex[words]Default sorting gives close matches, but the first match may be obscure or only mentioned a few times in the text:
SemanticSearch[index, "Rabbits", "Items", RerankingMethod -> Automatic]Rerank based on the most prevalent words:
SemanticSearch[index, "Rabbits", "Items", RerankingMethod -> <|"Function" -> (#[[All, "Count"]]&), "Target" -> "Tags"|>]index = CreateSemanticSearchIndex[ResourceData["Alice in Wonderland"]]Use a custom question when reranking to further refine the results:
First@SemanticSearch[index, "Talking animals", "RerankingMethod" -> <|"Query" -> "A cat that grins"|>]Applications (4)
Search Shakespeare's sonnets using partial quotes. Use a manual splitting of the text to group each sonnet together with its number:
sonnets = StringSplit[ResourceData["Shakespeare's Sonnets"], WordBoundary ~~ ("I" | "V" | "X" | "L" | "C").. ~~ "
"];
Short[sonnets]Then label each sonnet with its number:
sonnets = MapIndexed[#1 -> First[#2] &, sonnets];Create a index using the labeled sonnets:
index = CreateSemanticSearchIndex[sonnets, "Sonnets", "Method" -> {"SplitPattern" -> None, "MaximumItemLength" -> Infinity}]Retrieve sonnets and their number using a partial quote:
SemanticSearch[index, "summer's day", {"Item", "Label"}, MaxItems -> 1]Look for a vaguely remembered sonnet:
SemanticSearch[index, "About marriage and inspiration", {"Item", "Label"}, MaxItems -> 1]Retrieve the index of the Wolfram Function Repository:
index = ResourceData["Semantic Index of Wolfram Function Repository Documentation"]Search for custom functions that can generate a graphic of the solar system:
DeleteDuplicates[SemanticSearch[index, "Generate a graphic of the solar system"]]Search for a specific example in the given function:
SemanticSearch[index, "The position of a comet" -> MatchQ["SolarSystemPlot3D"], "Items", MaxItems -> 1]Run the example function from the given item:
ResourceFunction["SolarSystemPlot3D"][{Red, Sphere[Entity["Comet", "Comet1PHalley"], 1]}]Create an index from a set of books:
index = CreateSemanticSearchIndex[{ResourceData["Alice in Wonderland"] -> "Alice in Wonderland", ResourceData["The Wind in the Willows"] -> "The Wind in the Willows", ResourceData["The Jungle Book"] -> "The Jungle Book"}]Find some examples on a topic of interest:
passages = SemanticSearch[index, "Talking animals", {"Label", "Item"}];
Short[passages]Use LLMSynthesize to give a summary contrasting the differences:
LLMSynthesize[StringJoin["Write a short paragraph contrasting the differences between talking animals in these passages. Use direct quotes when possible:
", ToString[passages]]]Create an index containing German and English text:
index = CreateSemanticSearchIndex[{ResourceData["Death in Venice"], ResourceData["Alice in Wonderland"]}]The embeddings of the same meanings are similar across languages. Search in English for information in the German source:
result = SemanticSearch[index, "What does Gustav von Aschenbach look like?", MaxItems -> 1]Translate the result into English:
LLMSynthesize[LLMPrompt["Translate"]["English", result]]Properties & Relations (3)
When performing a search, the distance function specified in CreateSemanticSearchIndex is used:
index = CreateSemanticSearchIndex[{ResourceData["Alice in Wonderland"]}, "DistanceFunction" -> CosineDistance]SemanticSearch[index, "What did the rabbit wear?", "MaxItems" -> 1]The distance function setting can be retrieved from the VectorDatabaseObject backing the index:
index["Database"]["DistanceFunction"]index = CreateSemanticSearchIndex[{WikipediaData["cat"] -> "cat", WikipediaData["dog"] -> "dog"}]Items will always share the tag of their source:
SemanticSearch[index, "wolfram language", "Label"]To get list of source tags, use unique tags and DeleteDuplicates:
DeleteDuplicates[SemanticSearch[index, "wolfram language", "Label"]]SemanticSearch automatically reranks the results using SemanticRanking:
index = CreateSemanticSearchIndex[{
"Jellyfish drift with ocean currents.",
"Starfish regenerate lost body parts.",
"Crayfish dig burrows in mud.",
"Cuttlefish change color for camouflage.",
"Silverfish infest homes, eating paper.",
"Salmon migrate upstream to spawn.",
"Starfish live on ocean floors.",
"Shellfish create shells from calcium."
}]query = "Fish"
semanticSearchReranked = SemanticSearch[index, query, MaxItems -> 5]This is the equivalent of retrieving 10 results without reranking (the default "MinItems" for reranking):
extendedResults = SemanticSearch[index, query, MaxItems -> 10, RerankingMethod -> None]Then taking the first five results of the results after using SemanticRanking:
rankingResults = SemanticRanking[extendedResults, query][[1 ;; 5]]semanticSearchReranked === rankingResultsRelated Guides
Text
Wolfram Research (2024), SemanticSearch, Wolfram Language function, https://reference.wolfram.com/language/ref/SemanticSearch.html (updated 2025).
CMS
Wolfram Language. 2024. "SemanticSearch." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/SemanticSearch.html.
APA
Wolfram Language. (2024). SemanticSearch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SemanticSearch.html
BibTeX
@misc{reference.wolfram_2026_semanticsearch, author="Wolfram Research", title="{SemanticSearch}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/SemanticSearch.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_semanticsearch, organization={Wolfram Research}, title={SemanticSearch}, year={2025}, url={https://reference.wolfram.com/language/ref/SemanticSearch.html}, note=[Accessed: 13-June-2026]}