0

我有一些代码应该从搜索中返回 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 种类型(视频\列表、视频\视频、视频\作者、视频\类别)。任何搜索都应该搜索所有类型,而不是任何特定类型。

4

1 回答 1

1

我认为这个问题与 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))));
于 2014-05-19T15:24:52.040 回答