Lucene字段不包括在其他工作搜索中

时间:2016-05-19 13:44:59

标签: c# lucene

在C#布尔查询中,Lucene搜索多个字段三个字段不包含在搜索中(Sku,VariantSkus和Mpc),而其他字段的工作正常。

使用Luke,我可以看到值存储在索引中。在Luke中搜索时,我使用搜索器中包含的查询得到了正确的结果 (取自Visual Studio中的调试器)。 例: 使用以下查询:(在Visual Studio中调试时直接从查询值中获取)

create-drop

在运行代码时不起作用(搜索者的totalHits为0),但是给出了将Mpc与Luke中的正确产品匹配的预期结果。

我真的很困惑为什么同一个查询在C#代码中不起作用。 任何帮助或建议将不胜感激。

创建索引:

(+Mpc:B118^5) (+Sku:B118^5) (+Brand:B118) (+VariantSkus:B118^4) (+DisplayName:B118^3) (+DisplayName:B118*) (+DisplayName:B118~0.5) (+MisspelledNames:B118) (+Description:B118^0.4)

搜索:

        public static String CreateLuceneIndex(string basePath, HttpContext context)
    {
        var stopwatch = new Stopwatch();

        /* get the absolute path to the directory where the indexes will be created (and if it doesn't exist, create it) */
        var dirPath = context.Server.MapPath(basePath);
        if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
        var di = new DirectoryInfo(dirPath);
        var directory = FSDirectory.Open(di);

        stopwatch.Start();

        /* Select the standard Lucene analyser */
        var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
        var count = 0;
        var catalog = ProductCatalog.All().First();

        /* Open the index writer using the selected analyser */
        using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
        using(var mediaRepository = new ProductMediaRepository())
        {
            var urlService = ObjectFactory.Instance.Resolve<IUrlService>();

            // Get all the visible products from uCommerce we wish to index
            foreach (var product in Product.Find(p => p.DisplayOnSite && p.ParentProduct == null))
            {
                var url = urlService.GetUrl(catalog, product);

                var doc = new Document();
                doc.Add(new Field("id", product.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
                doc.Add(new Field("Url", url ?? String.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
                doc.Add(new Field("Src", ImageService.GetProductMainImage(mediaRepository, product).Src ?? String.Empty
                    , Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES));
                doc.Add(new Field("Sku", product.Sku ?? String.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));

                var varianSkus = String.Join(" ", product.Variants.Select(variant => variant.VariantSku));
                doc.Add(new Field("VariantSkus", varianSkus, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));

                doc.Add(new Field("DisplayName", product.DisplayName() ?? product.Name ?? String.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
                var brands = String.Join(" ", product.Variants.Select(variant => variant.GetPropertyValue<String>("Brand")).Where(w => !String.IsNullOrWhiteSpace(w)));
                doc.Add(new Field("Brand", brands ?? String.Empty, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES));

                doc.Add(new Field("MisspelledNames", product.GetPropertyValue<String>("MisspelledNames") ?? String.Empty,
                    Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES));

                doc.Add(new Field("Description", product.ShortDescription()?.StripHtml() ?? String.Empty, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));

                doc.Add(new Field("Mpc", product.GetPropertyValue<String>("MPC") ?? String.Empty, Field.Store.NO, Field.Index.NOT_ANALYZED, Field.TermVector.YES));

                writer.AddDocument(doc);
                count++;
            }

            writer.Optimize();
            writer.Close();
        }

        stopwatch.Stop();
        return $"Indexed {count} products in {stopwatch.Elapsed}.\n\n";

1 个答案:

答案 0 :(得分:0)

问题在于你应用提升的方式:

return AddClauseGroup(searchTerms, word => new TermQuery(new Term(field, word  + boostStr)));

您不能以这种方式将提升纳入术语本身。这里没有QueryParser,所以像“term ^ 4”这样的QueryParser语法不起作用。它只会搜索字符串“term ^ 4”,默认值为1.0。带有提升的TermQuery看起来像:

Query query = new TermQuery(new Term(field, word));
query.Boost = boost;