<p>I want to use Lucene (in particular, Lucene.NET) to search for email address domains.</p>

<p>E.g. I want to search for "@gmail.com" to find all emails sent to a gmail address.</p>

<p>Running a Lucene query for "*@gmail.com" results in an error, asterisks cannot be at the start of queries. Running a query for "@gmail.com" doesn't return any matches, because "foo@gmail.com" is seen as a whole word, and you cannot search for just parts of a word.</p>

<p>How can I do this?</p>

[{'answer_id': 20468, 'author': 'Judah Gabriel Himango', 'author_id': 536, 'author_profile': 'https://Stackoverflow.com/users/536', 'pm_score': 5, 'selected': True, 'text': '<p>No one gave a satisfactory answer, so we started poking around Lucene documentation and discovered we can accomplish this using custom Analyzers and Tokenizers.</p>\n\n<p>The answer is this: create a WhitespaceAndAtSymbolTokenizer and a WhitespaceAndAtSymbolAnalyzer, then recreate your index using this analyzer. Once you do this, a search for "@gmail.com" will return all gmail addresses, because it\'s seen as a separate word thanks to the Tokenizer we just created.</p>\n\n<p>Here\'s the source code, it\'s actually very simple:</p>\n\n<pre><code>class WhitespaceAndAtSymbolTokenizer : CharTokenizer\n{\n    public WhitespaceAndAtSymbolTokenizer(TextReader input)\n        : base(input)\n    {\n    }\n\n    protected override bool IsTokenChar(char c)\n    {\n        // Make whitespace characters and the @ symbol be indicators of new words.\n        return !(char.IsWhiteSpace(c) || c == \'@\');\n    }\n}\n\n\ninternal class WhitespaceAndAtSymbolAnalyzer : Analyzer\n{\n    public override TokenStream TokenStream(string fieldName, TextReader reader)\n    {\n        return new WhitespaceAndAtSymbolTokenizer(reader);\n    }\n}\n</code></pre>\n\n<p>That\'s it! Now you just need to rebuild your index and do all searches using this new Analyzer. For example, to write documents to your index:</p>\n\n<pre><code>IndexWriter index = new IndexWriter(indexDirectory, new WhitespaceAndAtSymbolAnalyzer());\nindex.AddDocument(myDocument);\n</code></pre>\n\n<p>Performing searches should use the analyzer as well:</p>\n\n<pre><code>IndexSearcher searcher = new IndexSearcher(indexDirectory);\nQuery query = new QueryParser("TheFieldNameToSearch", new WhitespaceAndAtSymbolAnalyzer()).Parse("@gmail.com");\nHits hits = query.Search(query);\n</code></pre>\n'}, {'answer_id': 23488, 'author': 'dlamblin', 'author_id': 459, 'author_profile': 'https://Stackoverflow.com/users/459', 'pm_score': 3, 'selected': False, 'text': "<p>I see you have your solution, but mine would have avoided this and added a field to the documents you're indexing called email_domain, into which I would have added the parsed out domain of the email address. It might sound silly, but the amount of storage associated with this is pretty minimal.  If you feel like getting fancier, say some domain had many subdomains, you could instead make a field into which the reversed domain went, so you'd store com.gmail, com.company.department, or ae.eim so you could find all the United Arab Emirates related addresses with a prefix query of 'ae.'</p>\n"}, {'answer_id': 83669, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>You could a separate field that indexes the email address reversed:\nIndex \'foo@gmail.com\' as \'moc.liamg@oof\'\nWhich enables you to do a query for "moc.liamg@*" </p>\n'}, {'answer_id': 100285, 'author': 'Markus', 'author_id': 18597, 'author_profile': 'https://Stackoverflow.com/users/18597', 'pm_score': 2, 'selected': False, 'text': '<p>There also is <a href="http://lucene.apache.org/java/2_3_2/api/org/apache/lucene/queryParser/QueryParser.html#setAllowLeadingWildcard(boolean)" rel="nofollow noreferrer"><strong>setAllowLeadingWildcard</strong></a></p>\n\n<p>But be <strong>careful</strong>. This could get very performance expensive (thats why it is disabled by default). Maybe in some cases this would be an easy solution, but I would prefer a custom Tokenizer as stated by <a href="https://stackoverflow.com/questions/19014/using-lucene-to-search-for-email-addresses#20468">Judah Himango</a>, too.</p>\n'}]
['https://Stackoverflow.com/questions/19014', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/536/']