title: Lucene.Net: Relevancy by distance between words
body: <p>I create (and update frequently) the index of users using following code (a bit shortened for demonstration purposes here): </p>
  
  <pre><code>            Lucene.Net.Store.Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo("TestLuceneIndex"));
              StandardAnalyzer standardAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
              IndexWriter indexWriter = new IndexWriter(directory, standardAnalyzer, IndexWriter.MaxFieldLength.UNLIMITED);
              Document doc = new Document();
              doc.Add(new Field("UID", uid, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
              doc.Add(new Field("GENDER", gender, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
              doc.Add(new Field("COUNTRY", countrycode, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
              doc.Add(new Field("CITY", citycode, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
              doc.Add(new Field("USERDATA", userdata, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
              doc.Add(new Field("USERINFO", userinfo, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
              indexWriter.UpdateDocument(new Term("UID", uid), doc);
              indexWriter.Optimize();
              indexWriter.Commit();
              indexWriter.Close();
  </code></pre>
  
  <p>The values, stored in index are as follows:<br>
  UID - user id (string GUID)
  GENDER - id of gender (string "0" (unidentified) "1" (male) or "2" (female)
  COUNTRY - country code (string like "US", "FR", etc)
  CITY - city code (string "A121", "C432", etc)
  USERDATA - long string of user detailes (something like "John Doe j.doe@gmail.com designer high education 5 years of experience")
  USERINFO - long string of text about user (something like "My name is John Doe. I was born ...")</p>
  
  <p>Then I perform search in index. I do search in two fields (USERDATA and USERINFO) and whenever it is necessary I do filter the results by GENDER, COUNTRY and CITY. As the result I retrieve UID (I need this value to identify the id of record of user in DB). </p>
  
  <p>This is a code I use for search:</p>
  
  <pre><code>        Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo("TestLuceneIndex");
          standardAnalyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
          Lucene.Net.Index.IndexReader indexReader = Lucene.Net.Index.IndexReader.Open(directory, true);
          indexSearcher = new Lucene.Net.Search.IndexSearcher(indexReader);
          Lucene.Net.Search.BooleanQuery booleanQuery = new Lucene.Net.Search.BooleanQuery();
          Lucene.Net.QueryParsers.MultiFieldQueryParser queryTextParser = new Lucene.Net.QueryParsers.MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new string[] { "USERDATA", "USERINFO" }, standardAnalyzer);
          Lucene.Net.Search.Query queryText = queryTextParser.Parse(SearchText);
          booleanQuery.Add(queryText, Lucene.Net.Search.BooleanClause.Occur.MUST);
          if (searchGender != "0")
          {
              Lucene.Net.Index.Term termGender = new Lucene.Net.Index.Term("GENDER", searchGender);
              Lucene.Net.Search.Query queryGender = new Lucene.Net.Search.TermQuery(termGender);
              booleanQuery.Add(queryGender, Lucene.Net.Search.BooleanClause.Occur.MUST);
          }
          if (searchCity != "0")
          {
              Lucene.Net.Index.Term termCity = new Lucene.Net.Index.Term("CITY", searchCity);
              Lucene.Net.Search.Query queryCity = new Lucene.Net.Search.TermQuery(termCity);
              booleanQuery.Add(queryCity, Lucene.Net.Search.BooleanClause.Occur.MUST);
          }
          if (searchCountry != "0")
          {
              Lucene.Net.Index.Term termCountry = new Lucene.Net.Index.Term("COUNTRY", searchCountry);
              Lucene.Net.Search.Query queryCountry = new Lucene.Net.Search.TermQuery(termCountry);
              booleanQuery.Add(queryCountry, Lucene.Net.Search.BooleanClause.Occur.MUST);
          }
          Lucene.Net.Search.TopScoreDocCollector collector = Lucene.Net.Search.TopScoreDocCollector.create(indexReader.MaxDoc(), true);
          indexSearcher.Search(booleanQuery, collector);
          Lucene.Net.Search.ScoreDoc[] scoreDocs=collector.TopDocs().scoreDocs;
          Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter("&lt;b&gt;", "&lt;/b&gt;");
          Lucene.Net.Highlight.QueryScorer queryScorer = new Lucene.Net.Highlight.QueryScorer(booleanQuery);
          highlighter = new Lucene.Net.Highlight.Highlighter(formatter, queryScorer);
          Lucene.Net.Highlight.Fragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(150);
          highlighter.SetTextFragmenter(fragmenter);
  </code></pre>
  
  <p>Everything works well enough except the quality of relevance when using several words: 
  When I search for instance for (microsoft .net programmer) the results, containing exact substring are not scored higher, than results, containing those words in different places of text. I understand, that this is caused by simple fact that score calculation is based on factor of percentage of searching string in text rather than exactness of coincidence of strings. But how to force scoring algorithm to asset exactness more valuable ? I.e. how to force the distance between words found to be considered as more important in calculation of relevancy ?</p>