title: Searching Lucene.Net index for an url field
body: <p>I want to search a Lucene.net index for a stored url field. My code is given below:</p>
  
  <pre><code>Field urlField = new Field("Url", url.ToLower(), Field.Store.YES,Field.Index.TOKENIZED);
  document.Add(urlField);`
  indexWriter.AddDocument(document);
  </code></pre>
  
  <p>I am using the above code for writing into the index.</p>
  
  <p>And the below code to search the Url in the index.</p>
  
  <pre><code>Lucene.Net.Store.Directory _directory = FSDirectory.GetDirectory(Host, false);
  IndexReader reader = IndexReader.Open(_directory);
  KeywordAnalyzer _analyzer = new KeywordAnalyzer();
  IndexSearcher indexSearcher = new IndexSearcher(reader);
  QueryParser parser = new QueryParser("Url", _analyzer);
  Query query = parser.Parse("\"" + downloadDoc.Uri.ToString() + "\"");
  TopDocs hits = indexSearcher.Search(query, null, 10);
  if (hits.totalHits &gt; 0)
  {
      //statements....
  }
  </code></pre>
  
  <p>But whenever I search for a url for example: <code>http://www.xyz.com/</code>, I am not getting any hits.</p>
  
  <p>Somehow, figured out the alternative. But this works in case of only one document in the index. If there are more documents, the below code will not yield correct result. Any ideas? Pls help</p>
  
  <p>While writing the index, use KeywordAnalyzer()</p>
  
  <pre><code>KeywordAnalyzer _analyzer = new KeywordAnalyzer();    
  indexWriter = new IndexWriter(_directory, _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
  </code></pre>
  
  <p>Then while searching also, use KeywordAnalyzer()</p>
  
  <pre><code>IndexReader reader = IndexReader.Open(_directory);
  KeywordAnalyzer _analyzer = new KeywordAnalyzer();
  IndexSearcher indexSearcher = new IndexSearcher(reader);
  QueryParser parser = new QueryParser("Url", _analyzer);
  Query query = parser.Parse("\"" + url.ToString() + "\"");                    
  TopDocs hits = indexSearcher.Search(query, null, 1);
  </code></pre>
  
  <blockquote>
    <p>This is because the KeywordAnalyzer "Tokenizes" the entire stream as a
    single token.</p>
  </blockquote>
  
  <p>Please help. Its urgent.</p>
  
  <p>Cheers
  Sunil...</p>