({{, (RefLink[TextSearch, paclet:ref/TextSearch])[source,form]
searches for files referenced by source that contain text matching form.}, {, (RefLink[TextSearch, paclet:ref/TextSearch])[source,form,"prop"]
returns the property prop for each result.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[TextSearch, paclet:ref/TextSearch]) supports the following basic search forms, which can be combined arbitrarily:

(RefLink[TextSearch, paclet:ref/TextSearch]) also supports the alternative forms (RefLink[ContainsAll, paclet:ref/ContainsAll])[…], (RefLink[ContainsAny, paclet:ref/ContainsAny])[…] and (RefLink[ContainsNone, paclet:ref/ContainsNone])[…], as well as (RefLink[And, paclet:ref/And])[…], (RefLink[Or, paclet:ref/Or])[…] and (RefLink[Not, paclet:ref/Not])[…].

(RefLink[TextSearch, paclet:ref/TextSearch]) allows the following to be used as sources:

(RefLink[TextSearch, paclet:ref/TextSearch])[source,form] returns a (RefLink[SearchResultObject, paclet:ref/SearchResultObject])[…] expression.

The properties prop can be any of the properties for (RefLink[ContentObject, paclet:ref/ContentObject]), in which case (RefLink[TextSearch, paclet:ref/TextSearch])[source,form,prop] is equivalent to (RefLink[SearchResultObject, paclet:ref/SearchResultObject])[…][(RefLink[All, paclet:ref/All]),prop].

The following additional properties are supported in (RefLink[TextSearch, paclet:ref/TextSearch])[source,form,prop]:

The property "Score" defines the default order in which search results are sorted.

Possible options include:

Files with an extension typical of binary files as well as files that contain non-textual byte values will not be indexed or searched.

(RefLink[TextSearch, paclet:ref/TextSearch]) supports many file formats that can be imported as plain text. Some of these include: "TXT", "CSV", "JSON", "XML", "PDF", "NB", "EPS".

(RefLink[TextSearch, paclet:ref/TextSearch]) does not support most image, audio and other kinds of file formats that do not have a text component.

(RefLink[TextSearch, paclet:ref/TextSearch]) reads the "Plaintext" element of a file, which is given by (RefLink[Import, paclet:ref/Import])[file, "Plaintext"].

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Create an index over a specific directory:

index=CreateSearchIndex["ExampleData/Text"]

Search for files in the specified directory that contain the word "dog" using the index:

TextSearch[index, "dog"][All]

Search for files containing "man" but not "animal" using advanced query syntax and show a contextual snippet:

TextSearch[index,SearchQueryString["man -animal"]][All,"Snippet"]

(RefLink[TextSearch, paclet:ref/TextSearch]) can also query a directory without indexing it:

TextSearch["ExampleData/Text", "dog"]

Search for files containing both words:

TextSearch["ExampleData/Text", {"dog", "lorem"}]

Search for files containing either word:

TextSearch["ExampleData/Text","dog"| "lorem"]

Queries can be combined:

TextSearch["ExampleData/Text", {"dog", "cat"}| "lorem"]

OpenersExampleSection1(Spacer1)Scope  (1)

Create an index using example text:

index = CreateSearchIndex[{
"ExampleData/Text/AliceInWonderland.txt", "ExampleData/Text/OriginOfSpecies.txt",
"ExampleData/Text/LoremIpsum.txt"}]

TextSearch[index, "alice"][All]

TextSearch[index, {"dog", "cat"}| "lorem"][All]

Add a second index:

index2 = CreateSearchIndex["ExampleData/Text/AeneidEnglish.txt"]

Search in both indices:

TextSearch[{index,index2}, "dog" | "lorem" | "Greece"][All]

OpenersExampleSection2(Spacer1)Options  (3)

OpenersExampleSubsection0(Spacer1)ContentFieldOptions  (1)

Specify weights for the fields in the index at query time:

index =CreateSearchIndex[{ContentObject[<|"Keywords" -> {"apple", "fruit"}, "Title" -> "doc1","Text"->"orange"|>], ContentObject[<|"Keywords" -> {"orange", "fruit"}, "Title" -> "doc2","Text"->"apple"|>]}]

Set a weight of 2 for the "Keywords" field:

TextSearch[index,"orange",ContentFieldOptions -> <|"Keywords" -> <|"Weight" -> 2|>|>][All]

When no weight is set, "doc1" gets a higher score:

TextSearch[index,"orange"][All, {"Title", "Score"}]

OpenersExampleSubsection1(Spacer1)DocumentWeightingRules  (1)

Define a "ConfidenceLevel" field and use it for document weighting:

index=CreateSearchIndex[{
ContentObject[<| "Text"->"orange","Title" -> "doc1","ConfidenceLevel"->"high"|>],
ContentObject[<| "Text"->"orange","Title" -> "doc2","ConfidenceLevel"->"low"|>]
},
ContentFieldOptions-><|"ConfidenceLevel"-><|"BulkRetrievalOptimized"->True|>|>,
DocumentWeightingRules-><|"ConfidenceLevel"-><|"high"->5,"low"->0.5|>|>]

TextSearch[index,"orange"][All,{"Title","Score"}]

OpenersExampleSubsection2(Spacer1)MaxItems  (1)

Get only the first result:

index = CreateSearchIndex["ExampleData/Text"];TextSearch[index,"dog",MaxItems->1]

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

Queries are case insensitive:

TextSearch["ExampleData/Text","ALICE"][All]

Queries only match entire words:

TextSearch["ExampleData/Text","Alice"][All]

TextSearch["ExampleData/Text","Alic"]

OpenersExampleSection4(Spacer1)Possible Issues  (2)

Only field weights can be specified at search time, while other content field options need to be specified at index creation time:

index =CreateSearchIndex[{ContentObject[<| "Title" -> "doc1","Text"->"orange"|>], ContentObject[<| "Title" -> "doc2","Text"->"apple"|>]}]

The following search returns a result because "IgnoreCase" is (RefLink[True, paclet:ref/True]) by default, and "IgnoreCase" cannot be specified at search time:

TextSearch[index,"Apple",ContentFieldOptions-><|"Text"-><|"IgnoreCase"->False|>|>][All]

When the same option is specified at index time, no result is returned:

index2 =CreateSearchIndex[{ContentObject[<| "Title" -> "doc1","Text"->"orange"|>], ContentObject[<| "Title" -> "doc2","Text"->"apple"|>]},ContentFieldOptions-><|"Text"-><|"IgnoreCase"->False|>|>];
TextSearch[index2,"Apple"][All]

The "Score" fields of objects matched in different indices are not comparable in general:

index= CreateSearchIndex[{
ContentObject[<| "Field1"->"orange"|>],ContentObject[<| "Field1"->"orange orange orange apple"|>]}];
index2 = CreateSearchIndex[{
ContentObject[<| "Field1"->"orange"|>]}];
TextSearch[{index,index2},"orange"][All,"Score"]

And indeed, in general a search on multiple indices returns results not sorted by "Score":

index = CreateSearchIndex[{
"ExampleData/Text/AliceInWonderland.txt", "ExampleData/Text/OriginOfSpecies.txt",
"ExampleData/Text/LoremIpsum.txt"}];
index2 = CreateSearchIndex["ExampleData/Text/AeneidEnglish.txt"];
TextSearch[{index,index2}, "dog" | "lorem" | "Greece"][All,"Score"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[TextSearchReport, paclet:ref/TextSearchReport])  ▪  (RefLink[SearchIndexObject, paclet:ref/SearchIndexObject])  ▪  (RefLink[ContentObject, paclet:ref/ContentObject])  ▪  (RefLink[CreateSearchIndex, paclet:ref/CreateSearchIndex])  ▪  (RefLink[SearchResultObject, paclet:ref/SearchResultObject])  ▪  (RefLink[SearchIndices, paclet:ref/SearchIndices])  ▪  (RefLink[FindList, paclet:ref/FindList])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[FindTextualAnswer, paclet:ref/FindTextualAnswer])  ▪  (RefLink[MailSearch, paclet:ref/MailSearch])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Text Search, paclet:guide/TextSearch])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])(WebLink[: String Patterns and Templates , https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])}})}})