asp.net - 带有 NEST 查询问题的 ElasticSearch

标签 asp.net elasticsearch entity-framework-6 nest elasticsearch-plugin

原谅我的新手,因为我是 ElasticSearch 和 NEST 的新手。我正在开发一个原型(prototype),以在正在实现的 .NET 解决方案中评估 ElasticSearch。原型(prototype)编译并且似乎搜索,但没有正确返回结果。它仅返回几个关键字的结果,仅小写,而忽略其他关键字并且不返回任何内容。我认为我的查询有问题。这是查询部分(假设指定并构建了连接信息和默认索引)。

// string searchString to be searched against ProductName and Description fields.            
var searchResults = client.Search<Product>(s=>s
            .From(0)
            .Size(100)
            .Query(q=>q.Term(p=>p.ProductName, searchString) || 
                q.Term(p=>p.Description, searchString)
            ));

如果需要,这是模型:
[ElasticType(IdProperty = "ProductID")]
public class Product
{
    [ScaffoldColumn(false)]
    [JsonIgnore]
    public int ProductID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string ProductName { get; set; }

    [Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
    public string Description { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "Price")]
    public double? UnitPrice { get; set; }

    public int? CategoryID { get; set; }
    [JsonIgnore]
    public virtual Category Category { get; set; }
}

感谢帮助!

最佳答案

您的问题是您正在使用 term queries , 不进行分析,因此区分大小写。

尝试使用 match query (已分析)改为:

var searchResults = client.Search<Product>(s => s
    .From(0)
    .Size(100)
    .Query(q => 
        q.Match(m => m.OnField(p => p.ProductName).Query(searchString)) || 
        q.Match(m => m.OnField(p => p.Description).Query(searchString))
     )
);

更进一步——因为您在两个不同的字段上查询相同的文本,您可以使用 multi match query而不是结合两个术语查询:
var searchResults = client.Search<Product>(s => s
    .From(0)
    .Size(100)
    .Query(q => q
        .MultiMatch(m => m
            .OnFields(p => p.Product, p => p.Description)
            .Query(searchText)
        )
     )
);

为了更好地理解分析,mapping and analysis来自 The Definitive Guide 的部分是一本很棒的书。

关于asp.net - 带有 NEST 查询问题的 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536429/

相关文章:

php - 设置elasticsearch php客户端的connect_timeout

elasticsearch - 在ElasticSearch 2.x及更高版本中为另一种类型重新索引

c# - 将 TransactionScope 与 Entity Framework 6 结合使用

asp.net - 找不到与所有表的请求 URI (...) 匹配的 HTTP 资源

c# - 替换 1 到 0 或 1 关系的端点

javascript - 在 ASP.NET MVC 中动态生成 Javascript、CSS

html - 如何在样式表中使用带 * 的 not css 选择器

javascript - 如何在javascript中父下拉列表的onchange事件上设置列表框的多个选定值

elasticsearch - 通过查询删除记录中的ElasticSearch错误

c# - 在 asp.net 中仅从 ListView 中获取最后一行值