如果用户在搜索框中输入短语(带或不带引号),我希望首先显示的结果是文档标题中具有完全短语的文档,以及显示在其后的其他文档。这是我尝试过的,但它没有按顺序给我搜索结果:
在编制索引时我说:
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的一部分,我有:
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)
时我明白了:
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相关联的数字,但每个keyWord都有单独的数字 但是在搜索时我运行query.ToString()我得到:
+(KeyWord:chicken KeyWord:parmesan) title:"Chicken Parmesan"~12^5.0
表示查询是正确的。对?我错过了什么?
答案 0 :(得分:2)
你建立标题查询的方式我怀疑你从来没有得到来自标题条款的命中。
您构建PhraseQuery以查找单个术语:“Chicken Parmesan”,但当您将其编入索引时,StandardAnalyzer会生成两个词:“chicken”和“parmesan”。您需要使用这两个术语构建PhraseQuery。
您可以将QueryParser用于此目的:
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来破坏令牌中的文本:
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);