Case sensitive search

Hi,

we understood that case sensitive search is not possible if pattern search or regular expressions are being used. Is there a way to retrieve the original match from a document or to find out whether the match was case sensitive? It seems like the FoundDocument Terms member only contains the lower case variant of matches.

Best regards
Jam

Hi @jamsharp!

Good question; honestly, no, at this moment: the original casing isn’t in the index at all, so there’s nothing to retrieve from FoundDocument. You can get it back from the document text, though.

What can be implemented in this direction

Some obvious way: let the index narrow the candidates, then confirm case yourself against the real text.

The index can give you the document text back — either from text storage if you enabled it at index time, or by re-extracting from the original file if you didn’t:

// 1. Pattern search, case-insensitive - narrows to candidate documents
var result = index.Search("^Group.*Docs$");   // your pattern

foreach (var found in result.GetFoundDocuments())
{
    // 2. Pull the actual text, original casing intact
    var adapter = new StringOutputAdapter(OutputFormat.PlainText);
    index.GetDocumentText(found.DocumentInfo, adapter);
    var text = adapter.GetResult();

    // 3. Apply your own case-sensitive check
    foreach (Match m in Regex.Matches(text, @"Group\w*Docs"))   // case-sensitive by default
    {
        Console.WriteLine($"{found.DocumentInfo.FilePath}: {m.Value}");
    }
}

If you’d rather see the matches in context, use the highlighter instead — it marks the hit positions for you and keeps the original casing:

var adapter = new StringOutputAdapter(OutputFormat.Html);
var highlighter = new DocumentHighlighter(adapter);
index.Highlight(found, highlighter);
string html = adapter.GetResult();   // matches wrapped in your highlight tags

FragmentHighlighter gives you just the surrounding fragments rather than the whole document, which is usually what you want if the documents are large.

One thing to check: if you didn’t set TextStorageSettings when creating the index, both of these re-extract from the original file, so the file still has to be there and it costs an extraction per document. If you’re doing this routinely, enabling text storage makes it a cheap read instead. It does grow the index, so weigh it up.

Following this topic, we created the Investigation ticket for the backlog SEARCHNET-3631
We will analyze implementation options and share our plans with you.
Thank you!