Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene boosting / scoring with multiple columns
    primarykey
    data
    text
    <p>How would I take the following data and be able to get the results below?</p> <p>Ive included a code sample but I cant seem to figure out how to properly search through multiple columns and apply the boost I need.</p> <p>Am I going about this the right way?</p> <h3>Boost / Weight for each column</h3> <pre><code>First Name = 100 Last Name = 75 Bio = 50 </code></pre> <h3>Data</h3> First Name, Last Name, Bio <pre><code>Benny, Benson, This is a test - "ben" appears in the first name AND last name - Score = 175 Jim, Smith, Another test with the word ben - "ben" appears in the bio - Score = 50 John, Benson, And another test here - "ben" appears in the last name - Score = 75 </code></pre> <h3>Results</h3> <pre><code>1. Benny 2. John 3. Jim protected override void _addToLuceneIndex(dynamic item, IndexWriter writer) { var user = item as UserTestItem; if (user == null) return; // remove older index entry var searchQuery = new TermQuery(new Term(USER_ID, user.UserID.ToString(CultureInfo.InvariantCulture))); writer.DeleteDocuments(searchQuery); // add new index entry var doc = new Document(); // get fields var userId = new Field(USER_ID, user.UserID.ToString(CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED); var firstName = new Field(FIRST_NAME, user.FirstName ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); var lastName = new Field(LAST_NAME, user.LastName ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); var bio = new Field(BIO, user.Bio ?? string.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES); // add boosts firstName.Boost = 100f; lastName.Boost = 75f; bio.Boost = 50f; // add lucene fields mapped to db fields doc.Add(userId); doc.Add(firstName); doc.Add(lastName); doc.Add(bio); // add entry to index writer.AddDocument(doc); } public string[] FieldsToSearch { get; set; } // i.e. "FirstName", "LastName", "Bio" public UserSearchResults SearchUsers(string searchQuery, bool exact = false) { var results = new UserSearchResults(); if (!string.IsNullOrEmpty(searchQuery)) { //searchQuery = PrepareInput(searchQuery, exact); try { using (var searcher = new IndexSearcher(IndexDirectory, false)) { var analyzer = new StandardAnalyzer(LUCENE_VERSION); // Search by multiple fields (ordered by RELEVANCE) var parser = new MultiFieldQueryParser(LUCENE_VERSION, FieldsToSearch, analyzer); parser.AllowLeadingWildcard = true; parser.DefaultOperator = exact ? QueryParser.AND_OPERATOR : QueryParser.OR_OPERATOR; var multiFieldQuery = ParseQuery(searchQuery, parser); var hits = searcher.Search(multiFieldQuery, null, SearchResultLimit, Sort.RELEVANCE); var docs = hits.ScoreDocs; results.Items = _mapLuceneToDataList(docs, searcher).Cast&lt;UserTestItem&gt;().ToList(); results.Total = results.Items.Count; results.RawQuery = LastUsedQuery.ToString(); analyzer.Close(); searcher.Dispose(); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } } return results; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload