【问题标题】:Orchard Lucene Custom Search Service with Query Grouping and Numeric Range Query具有查询分组和数值范围查询的 Orchard Lucene 自定义搜索服务
【发布时间】:2016-02-24 07:47:22
【问题描述】:

我正在尝试实现我自己的自定义 SearchService。据我了解

// If I wanted to query "+name:camera +price:[100 TO 150] +location:thailand"
searchBuilder.WithField("name", "camera").ExactMatch().Mandatory();
searchBuilder.WithinRange("price", 100, 150, true, true).Mandatory();
searchBuilder.WithField("location", "thailand").ExactMatch().Mandatory();

这里没问题。但是如何处理带有分组的查询(带括号的子句)?

// How do I query "+name:camera +(price:[100 TO 150] cost:[100 TO 150]) +(bundle-price:[100 TO 150] total-price:[100 TO 150])"?
// Since the ISearchBuilder interface doesn't support grouping, the only other way
// I could think of is to use the freestyle ISearchBuilder.Parse() to construct the query.
// However this doesn't return any hits:
searchBuilder.WithField("name", "camera").ExactMatch().Mandatory();
searchBuilder.Parse("price", "[100 TO 150] cost:[100 TO 150]", false).Mandatory();
searchBuilder.Parse("bundle-price", "[100 TO 150] total-price:[100 TO 150]", false).Mandatory();

没有足够的信息让ISearchBuilder.Parse() 确定范围的数据类型(无论是intfloat 还是double)。看起来 Luke 也遇到了这个问题,可以使用我认为 Lucene.Net 没有的 XML Query Parser 来解决这个问题:

Lucene numeric range search with LUKE


我在想如果ISearchBuilder 有一种方法可以让我们插入自己的查询

// /src/Orchard/Indexing/ISearchBuider.cs
public interface ISearchBuilder {
    ISearchBuilder WithQuery(Query query);
}

// /src/Orchard.Web/Modules/Lucene/Services/LuceneSearchBuilder.cs
public class LuceneSearchBuilder : ISearchBuilder {
    public ISearchBuilder WithQuery(Query query) {
        CreatePendingClause();
        _query = query;
        return this;
    }
}

public class CustomSearchService {
    public void Search() {
        // construct our own custom queries
        var booleanQuery1 = new BooleanQuery();
        booleanQuery1.Add(NumericRangeQuery.NewDoubleRange("price", 100, 150, true, true), Occur.SHOULD);
        booleanQuery1.Add(NumericRangeQuery.NewDoubleRange("cost", 100, 150, true, true), Occur.SHOULD);

        var booleanQuery2 = new BooleanQuery();
        booleanQuery2.Add(NumericRangeQuery.NewDoubleRange("bundle-price", 100, 150, true, true), Occur.SHOULD);
        booleanQuery2.Add(NumericRangeQuery.NewDoubleRange("total-price", 100, 150, true, true), Occur.SHOULD);

        searchBuilder.WithField("name", "camera").ExactMatch().Mandatory();
        // insert our custom queries into the SearchBuilder using the newly added interface method (ISearchBuilder.WithQuery()) above
        searchBuilder.WithQuery(booleanQuery1).Mandatory();
        searchBuilder.WithQuery(booleanQuery2).Mandatory();
    }
}

然后我们可以构造更复杂和灵活的查询。

更新

之前有人在 https://github.com/OrchardCMS/Orchard/issues/5764

【问题讨论】:

    标签: orchardcms lucene.net orchardcms-1.9


    【解决方案1】:

    您可能应该将 Search API 与构面一起使用。这是一个精确的演示:https://www.youtube.com/watch?v=7v5qSR4g7E0 和一些注释:http://weblogs.asp.net/bleroy/orchard-harvest-2015-search-api

    【讨论】:

    • 我之前已经看过这个,但我看不出这与我的问题有什么关系。请您进一步详细说明?从外观上看,facet 就像将一个简单的子句作为过滤器应用于现有查询,而不是一组复杂的子句。此外,分面搜索强调点击次数的汇总。我对实际命中(从命中返回的文档)更感兴趣,而不仅仅是命中计数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多