NET搜索索引的方法

NET搜索索引的方法

本文介绍了Lucene.NET搜索索引的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个测试用例使用Lucene.NET在我们的网站之一。我想做到以下几点:

I am trying to put together a test case for using Lucene.NET on one of our websites. I'd like to do the following:

指数在一个唯一的ID。
指数跨越一个逗号delimitered条款或标签的字符串。

Index in a single unique id.Index across a comma delimitered string of terms or tags.

例如

第1项:
ID = 1
标签=东西,分居,期限

Item 1:Id = 1Tags = Something,Separated-Term

我将被构建搜索这样我就可以找对标签即文件。

I will then be structuring the search so I can look for documents against tag i.e.

标签:东西或标记:独立的长期

tags:something OR tags:separate-term

我需要为了寻找反对维持精确的长期价值。

I need to maintain the exact term value in order to search against it.

我有事运行,并如期搜索查询被解析,但我没有看到任何结果。下面是一些code。

I have something running, and the search query is being parsed as expected, but I am not seeing any results. Here's some code.

我的解析器(_luceneAnalyzer传递到我的索引服务):

My parser (_luceneAnalyzer is passed into my indexing service):

var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "Tags", _luceneAnalyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
return parser;

我的Lucene.NET文件的创建:

My Lucene.NET document creation:

var doc = new Document();

var id = new Field(
    "Id",
    NumericUtils.IntToPrefixCoded(indexObject.id),
    Field.Store.YES,
    Field.Index.NOT_ANALYZED,
    Field.TermVector.NO);

var tags = new Field(
    "Tags",
    string.Join(",", indexObject.Tags.ToArray()),
    Field.Store.NO,
    Field.Index.ANALYZED,
    Field.TermVector.YES);

doc.Add(id);
doc.Add(tags);

return doc;

我的搜索:

var parser = BuildQueryParser();
var query = parser.Parse(searchQuery);
var searcher = Searcher;

TopDocs hits = searcher.Search(query, null, max);
IList<SearchResult> result = new List<SearchResult>();
float scoreNorm = 1.0f / hits.GetMaxScore();

for (int i = 0; i < hits.scoreDocs.Length; i++)
{
    float score = hits.scoreDocs[i].score * scoreNorm;
    result.Add(CreateSearchResult(searcher.Doc(hits.scoreDocs[i].doc), score));
}

return result;

我在我的索引两个文件,​​一个与标签东西,一个与标签东西和分隔期限。这是很重要的 - 留在条款我要上满值的精确匹配

I have two documents in my index, one with the tag "Something" and one with the tags "Something" and "Separated-Term". It's important for the - to remain in the terms as I want an exact match on the full value.

当我搜索标签的东西。我没有得到任何结果。

When I search with "tags:Something" I do not get any results.

分析仪什么我应该使用来实现搜索索引我之后?
是否有组建一个搜索像这样的指针?
为什么我的当前搜索不返回任何结果?

What Analyzer should I be using to achieve the search index I am after?Are there any pointers for putting together a search such as this?Why is my current search not returning any results?

非常感谢

推荐答案

看样子你可以用相同的名字,所以我改变了我的code添加多个领域的一个文件:

It appears you can add multiple fields with the same name to a document so I changed my code to:

foreach (string tag in vehicle.Tags)
{
    var tags = new Field(
        TAGS,
        tag,
        Field.Store.YES,
        Field.Index.ANALYZED,
        Field.TermVector.YES);

    doc.Add(tags);
}

我可以通过单个或多个标签,在标签字段现在搜索。

I can now search by single or multiple tags in the "Tags" field.

这篇关于Lucene.NET搜索索引的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:06