Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:12+00:00 2026-05-26T06:39:12+00:00

If a user enters a phrase in the search box (with or without quotes),

  • 0

If a user enters a phrase in the search box (with or without quotes), I want the results that show first be the documents that have the exact phrase in the document title, and other documents showing after it. This is what I have tried but it fails to give me the search results in that order:

During indexing I say:

AddStringFieldToDocument(document, "keyWord", this.BuildKeywordsString(), Field.Store.NO, Field.Index.ANALYZED, false);

AddStringFieldToDocument(document, "title", this.Title, Field.Store.NO, Field.Index.ANALYZED, false, 4f);

    private void AddStringFieldToDocument(Document document, string fieldName, string fieldValue, 
        Field.Store store, Field.Index index, bool setOmitTermFreqAndPositions)
    {
        if (fieldValue == null)
        {
            return;
        }
        var field = GetFieldToAddToDocument(document, fieldName, fieldValue, store, index, setOmitTermFreqAndPositions);
        document.Add(field);
    }

    private void AddStringFieldToDocument(Document document, string fieldName, string fieldValue,
        Field.Store store, Field.Index index, bool setOmitTermFreqAndPositions, Single boost)
    {
        if (fieldValue == null)
        {
            return;
        }

        var field = GetFieldToAddToDocument(document, fieldName, fieldValue, store, index, setOmitTermFreqAndPositions);
        field.SetBoost(boost); //boosting title
        document.Add(field);
    }

    private Field GetFieldToAddToDocument(Document document, string fieldName, string fieldValue, Field.Store store,
        Field.Index index, bool setOmitTermFreqAndPositions)
    {
        Field field = new Field(fieldName, fieldValue, store, index);
        field.SetOmitTermFreqAndPositions(setOmitTermFreqAndPositions);
        return field;
    }

At search time as part of my BooleanQuery I have:

if (!string.IsNullOrWhiteSpace(queryString))
        {
            QueryParser qpKeyWord = new QueryParser(myVersionUsed, "keyWord", StandardAnalyzer);

            Query qKeyWord = qpKeyWord.Parse(queryString);
            booleanQuery.Add(qKeyWord, BooleanClause.Occur.MUST);

            Term titleTerm = new Term("title", queryString);
            PhraseQuery qTitleWord = new PhraseQuery();
            qTitleWord.SetSlop(12);
            qTitleWord.Add(titleTerm);
            qTitleWord.SetBoost(5);
            booleanQuery.Add(qTitleWord, BooleanClause.Occur.SHOULD);

The results I get are mixed. Further, when I run IndexSearcher.Explain(query, docId)

I get:

Document Id: 92871
0.5439626 = (MATCH) product of:
  0.8159439 = (MATCH) sum of:
    0.5884751 = (MATCH) sum of:
      0.2580064 = (MATCH) weight(KeyWord:chicken in 92871), product of:
        0.2226703 = queryWeight(KeyWord:chicken), product of:
          3.236447 = idf(docFreq=25345, maxDocs=237239)
          0.06880084 = queryNorm
        1.158692 = (MATCH) fieldWeight(KeyWord:chicken in 92871), product of:
          4.582576 = tf(termFreq(KeyWord:chicken)=21)
          3.236447 = idf(docFreq=25345, maxDocs=237239)
          0.078125 = fieldNorm(field=KeyWord, doc=92871)
      0.3304687 = (MATCH) weight(KeyWord:parmesan in 92871), product of:
        0.2962231 = queryWeight(KeyWord:parmesan), product of:
          4.305515 = idf(docFreq=8701, maxDocs=237239)
          0.06880084 = queryNorm
        1.115608 = (MATCH) fieldWeight(KeyWord:parmesan in 92871), product of:
          3.316625 = tf(termFreq(KeyWord:parmesan)=11)
          4.305515 = idf(docFreq=8701, maxDocs=237239)
          0.078125 = fieldNorm(field=KeyWord, doc=92871)
    0.2274688 = (MATCH) weight(has_photo:y in 92871), product of:
      0.1251001 = queryWeight(has_photo:y), product of:
        1.818294 = idf(docFreq=104665, maxDocs=237239)
        0.06880084 = queryNorm
      1.818294 = (MATCH) fieldWeight(has_photo:y in 92871), product of:
        1 = tf(termFreq(has_photo:y)=1)
        1.818294 = idf(docFreq=104665, maxDocs=237239)
        1 = fieldNorm(field=has_photo, doc=92871)
  0.6666667 = coord(2/3)

Which has no number associated with the PhraseQuery but has separate numbers for each keyWord.
However at search time when I run query.ToString() I get:

+(KeyWord:chicken KeyWord:parmesan) title:"Chicken Parmesan"~12^5.0 

which means that The query was written right. Right? What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:39:12+00:00Added an answer on May 26, 2026 at 6:39 am

    The way you build up your query for the title I suspect you never get hits originating from the title clause.

    You build the PhraseQuery to look for a single term: “Chicken Parmesan”, but when you indexed it, the StandardAnalyzer produced two Terms: “chicken” and “parmesan”. You need to build the PhraseQuery with those two terms.

    You can use the QueryParser for this purpose:

        QueryParser qp = new QueryParser("keyWord", new StandardAnalyzer());
        Query q = qp.Parse("+(keyWord:chicken KeyWord:parmesan) title:\"Chicken Parmesan\"~12^5.0");
        var hits = searcher.Search(q);
    

    If you dont want to use the QueryParser, use the TokenStream api to break your text in tokens:

        PhraseQuery titleQuery = new PhraseQuery();
        titleQuery.SetSlop(12);
        titleQuery.SetBoost(5);
        BooleanQuery keywordQuery = new BooleanQuery();
    
        var standard = new StandardAnalyzer();
        TokenStream tokens = standard.TokenStream("title", new StringReader("Chicken Parmesan"));
        List<Term> terms = new List<Term>();
        while (tokens.IncrementToken())
        {
            TermAttribute termAttribute = (TermAttribute)tokens.GetAttribute(typeof(TermAttribute));
    
            titleQuery.Add(new Term("title", termAttribute.Term()));
    
            keywordQuery.Add(
                new TermQuery(
                    new Term("keyWord", termAttribute.Term())),
                BooleanClause.Occur.SHOULD);  
        }
    
        BooleanQuery query = new BooleanQuery();
        query.Add(keywordQuery, BooleanClause.Occur.MUST);
        query.Add(titleQuery, BooleanClause.Occur.SHOULD);
    
        var hits = searcher.Search(query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate the value a user enters in a text box, so
When a user enters a search query, I'd like to track: 1) Their search
When the user enters info I want all the controls to look and act
When a user enters a date in text box,i've to check ,whether it is
I've got a requirement where a user enters a few terms into a search
I have a textarea in which the user enters the following data: Paul:Nine, Rome
User enters tag in the textbox. The textbox is a live search as the
sometimes user enters text that cannot be coerced properly. e.g. string -> money. '123
when the user enters text in a uitextview I would that the title of
I have a data entry form where user enters lots of data. When user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.