Elasticsearch使用过滤器

时间:2018-05-30 16:04:07

标签: .net elasticsearch nest

我已经创建了一个搜索描述符,以便能够从elasticsearch中过滤一些数据,似乎我的所有测试都会过滤掉过滤器功能但是它们无法进行全文搜索。

我已经尝试了所有的变化,并且为什么会发生这种情况而迷失了。

poco:

[ElasticsearchType(Name = "restaurant")]
public class RestaurantSearchItem
{
    public const string IndexName = "restaurants";

    [Keyword]
    public string Id { get; set; }

    [Text]
    public string Name { get; set; }

    public decimal Rating { get; set; }

    public int ReviewCount { get; set; }

    public string UrlName { get; set; }

    [Keyword]
    public string CountryCode { get; set; }

    [GeoPoint]
    public Geolocation Location { get; set; }

    [Keyword]
    public string[] CuisineIds { get; set; }

    [Keyword]
    public string[] StyleIds { get; set; }

    public Image[] Images { get; set; }
}

映射:

await _elasticClient.CreateIndexAsync(
                RestaurantSearchItem.IndexName,
                i => i.Mappings(ms => ms.Map<RestaurantSearchItem>(m => m.AutoMap())), cancellation);

描述符:

var filters = new List<Func<QueryContainerDescriptor<RestaurantSearchItem>, QueryContainer>>();

        if (request.Query.CuisineIds?.Any() == true)
        {
            filters.Add(fq => fq.Terms(t => t.Field(f => f.CuisineIds).Terms(request.Query.CuisineIds)));
        }

        if (request.Query.StyleIds?.Any() == true)
        {
            filters.Add(fq => fq.Terms(t => t.Field(f => f.StyleIds).Terms(request.Query.StyleIds)));
        }

        return descriptor => descriptor
            .Query(q => q
                .Bool(b => b
                    .Must(m => m            
                        .MultiMatch(mp => mp    
                            .Query(request.Query.FreeText)
                            .Fields(f => f
                                .Fields(r => r.Name))))
                    .Filter(f => f          
                        .Bool(b1 => b1
                            .Must(filters)))))
            .Sort(o => o.Ascending(r => r.Id))
            .From(request.Offset)
            .Size(request.PageSize);
    }

0 个答案:

没有答案