PhraseQuery针对标题字段和QueryParser针对所有字段的捕获不会导致结果我期望

如果用户在搜索框中输入短语(带或不带引号),我希望显示的结果首先是文档文档标题中包含精确的词组以及其后的其他文档。这是我试过,但它不能给我在搜索结果的顺序:PhraseQuery针对标题字段和QueryParser针对所有字段的捕获不会导致结果我期望

在索引我说:

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(查询的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()我得到:

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

这意味着查询被写入权。对?我错过了什么?

回答:

你建立你的查询标题的方式我怀疑你永远不会得到来自标题子句的命中。

您构建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);

以上是 PhraseQuery针对标题字段和QueryParser针对所有字段的捕获不会导致结果我期望 的全部内容, 来源链接: utcz.com/qa/260449.html

回到顶部