针对标题字段的PhraseQuery和针对全部捕获字段的QueryParser不会导致我期望的结果 [英] PhraseQuery against title field and QueryParser against a catch all field do not result in the results I expect
问题描述
如果用户在搜索框中输入短语(带引号或不带引号),我希望首先显示的结果是文档标题中具有确切短语的文档,然后显示其他文档.这是我尝试过的方法,但无法按该顺序提供搜索结果:
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:
在索引过程中,我说:
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;
}
在搜索时,作为BooleanQuery的一部分,我有:
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);
我得到的结果好坏参半.此外,当我运行IndexSearcher.Explain(query,docId)
The results I get are mixed. Further, when I run IndexSearcher.Explain(query, docId)
我得到:
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)
没有与PhraseQuery关联的数字,但每个关键字都有单独的数字.
但是在搜索时,当我运行query.ToString()时,我得到:
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?
推荐答案
构建标题查询的方式我怀疑您永远不会获得来自title子句的匹配.
The way you build up your query for the title I suspect you never get hits originating from the title clause.
您构建了PhraseQuery来查找单个术语:"Chicken Parmesan",但是当您对其进行索引时,StandardAnalyzer产生了两个术语:"chicken"和"parmesan".您需要使用这两个术语来构建PhraseQuery.
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.
您可以为此使用QueryParser:
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);
如果您不想使用QueryParser,请使用TokenStream api将您的文本以令牌分隔:
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);
这篇关于针对标题字段的PhraseQuery和针对全部捕获字段的QueryParser不会导致我期望的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!