我有一些代码应从搜索中返回5个匹配项。
如果我在浏览器中尝试查询,则会得到5个结果:
http://localhost:9200/_search?q=Testing
如果我使用SENSE编辑器,它还会显示5个结果:
Server=localhost:9200
POST _search
{
"query": {
"query_string": {
"query": "Testing"
}
}
}
但是我在控制器中的C#代码无法获得任何匹配。我想念什么?
Uri localhost = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(localhost);
setting.SetDefaultIndex("videos");
var client = new ElasticClient(setting);
var result = client.Search<SearchHint>(
body => body.Query(
query => query.QueryString(
qs => qs.Query(keys))));
var results = new SearchResults()
{
Results = result.Documents.ToList() <-- this has 0 items
};
编辑1:
public class SearchHint
{
public string Id { get; set; }
public string Title { get; set; }
public int NumItems { get; set; }
public bool IsList { get; set; }
public SearchHint(string id, string title, int numItems, bool isList)
{
Id = id;
Title = title;
NumItems = numItems;
IsList = isList;
}
}
编辑2:索引中有4种类型(视频\列表,视频\视频,视频\作者,视频\类别)。任何搜索都应搜索所有类型,而不是任何特定类型。
我认为这个问题与NEST预设搜寻类型的方式有关。除非您[ElasticType]
在SearchHint
类中指定了属性,否则它将使用以下URL查询Elasticsearch:
/_all/searchhint/_search
尝试将与您在索引中使用的类型相对应的类型名添加到类定义中,如下所示(将mytype替换为索引的适当值。此外,如果索引项上的字段与默认映射不匹配约定(驼峰式),您将不会填充数据。
[ElasticType(Name = "mytype"]
public class SearchHint
{
// Will map to field with name title in your index.
//Use the following property to specify an alternate name
//[ElasticProperty(Name="Title")]
public string Title { get; set;}
}
请参阅有关推理的NEST文档以了解所有工作原理的概述。
更新:以前的内容仅适用于单个类型内的搜索。如果要跨多种类型进行搜索,则需要.AllTypes()
在搜索查询中进行指定,而无需[ElasticType]
在类中指定属性。
var result = client.Search<SearchHint>(
body => body
.AllTypes()
.Query(
query => query.QueryString(
qs => qs.Query(keys))));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句