NEST Api Search在NEST中返回null,但在Kibana中有效 [英] NEST Api SearchAfter return null in NEST but works in Kibana

查看:69
本文介绍了NEST Api Search在NEST中返回null,但在Kibana中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序中仅将弹性搜索用于文档搜索,因此我们没有任何专家.我能够成功使用TermQuerySimpleQueryStringQueryMatchPhraseQuery.但是我在文档中发现使用From&分页的Size不适用于生产,建议使用之后搜索".

但是我的实现返回null.这让我感到困惑,如Nest API Object Initializer语法

在这里,我将size=1用于测试以及SearchAfter中的硬代码_id值.

NEST生成的查询是:

{
  "size": 1,
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ],
  "search_after": [
    "0fc3ccb625f5d95b973ce1462b9f7"
  ],
  "query": {
    "match": {
      "content": {
        "query": "lahore",
        "fuzziness": "AUTO",
        "prefix_length": 3,
        "max_expansions": 10
      }
    }
  }
}

ES的响应确实显示成功,但未返回任何结果.

  • 结果确实会在基巴纳语中返回
  • 查询状态成功
  • 但是...
  • 在NEST中返回的总数为0
  • 在kibana中,排序值为空我使用TrackScores = true解决了此问题

以下是调试信息:

Valid NEST response built from a successful low level call on POST: /extract/_source/_search?typed_keys=true
# Audit trail of this API call:
 - [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.1002662
# Request:
{"size":1,"sort":[{"_id":{"order":"desc"}}],"search_after":["0fc3ccb625f5d95b973ce1462b9f7"],"query":{"match":{"content":{"query":"lahore","fuzziness":"AUTO","prefix_length":3,"max_expansions":10}}}}
# Response:
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

所以请告诉我哪里错了,可能是什么问题以及如何解决.


更新2:

控制器中的代码:

连接字符串:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DisableDirectStreaming();
settings.DefaultIndex("extract");
var client = new ElasticClient(settings);

查询:

var query = (dynamic)null;
query = new MatchQuery
 {
    Field = "content",
    Query = content,
    Fuzziness = Fuzziness.Auto,
    PrefixLength = 3,
    MaxExpansions = 10
   };

查询生成器

var request = new SearchRequest<ElasticSearchJsonObject.Rootobject>
            {
                Sort = new List<ISort>
                {
                    new SortField { Field = "_id", Order = SortOrder.Descending}
                },
                SearchAfter = new List<object> {
                   documentid //sent as parameter
                },                    
                Size = 1, //for testing 1 other wise 10
                TrackScores = true,
                Query = query
            };

JSON查询 我使用此代码获取上面发布的查询.然后,该查询通过GET <my index name>/_Search传递给kibana,在这里有效

var stream = new System.IO.MemoryStream();
client.SourceSerializer.Serialize(request, stream);
var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());

ES响应

string responseJson = "";
                ElasticSearchJsonObject.Rootobject response = new ElasticSearchJsonObject.Rootobject();
                var res = client.Search<object>(request);
                if (res.ApiCall.ResponseBodyInBytes != null)
                {
                    responseJson = System.Text.Encoding.UTF8.GetString(res.ApiCall.ResponseBodyInBytes);
                    try
                    {
                        response = JsonConvert.DeserializeObject<ElasticSearchJsonObject.Rootobject>(responseJson);
                    }
                    catch (Exception)
                    {
                        var model1 = new LoginSignUpViewModel();
                        return PartialView("_NoResultPage", model1);
                    }
                }

这是出问题的地方.上面的调试信息是从response

捕获的

ElasticSearchJsonObject

我认为问题可能出在这里吗?该类是通过在Search请求中获取NEST的响应而生成的.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ESAPI
{
    public class ElasticSearchJsonObject
    {
        public class Rootobject
        {
            public int took { get; set; }
            public bool timed_out { get; set; }
            public _Shards _shards { get; set; }
            public Hits hits { get; set; }
        }

        public class _Shards
        {
            public int total { get; set; }
            public int successful { get; set; }
            public int skipped { get; set; }
            public int failed { get; set; }
        }

        public class Hits
        {
            public int total { get; set; }
            public float max_score { get; set; }
            public Hit[] hits { get; set; }
        }

        public class Hit
        {
            public string _index { get; set; }
            public string _type { get; set; }
            public string _id { get; set; }
            public float _score { get; set; }
            public _Source _source { get; set; }
        }

        public class _Source
        {
            public string content { get; set; }
            public Meta meta { get; set; }
            public File file { get; set; }
            public Path path { get; set; }
        }

        public class Meta
        {
            public string title { get; set; }
            public Raw raw { get; set; }
        }

        public class Raw
        {
            public string XParsedBy { get; set; }
            public string Originator { get; set; }
            public string dctitle { get; set; }
            public string ContentEncoding { get; set; }
            public string ContentTypeHint { get; set; }
            public string resourceName { get; set; }
            public string ProgId { get; set; }
            public string title { get; set; }
            public string ContentType { get; set; }
            public string Generator { get; set; }
        }

        public class File
        {
            public string extension { get; set; }
            public string content_type { get; set; }
            public DateTime last_modified { get; set; }
            public DateTime indexing_date { get; set; }
            public int filesize { get; set; }
            public string filename { get; set; }
            public string url { get; set; }
        }

        public class Path
        {
            public string root { get; set; }
            public string _virtual { get; set; }
            public string real { get; set; }
        }
    }
}

我确信这可以用来获得回应.

请注意,在简单搜索的情况下,此代码有效:

因此对于我的代码下面的此查询,它是有效的:

var request = new SearchRequest
                {
                    From = 0,
                    Size = 20,
                    Query = query
                };

="p>建议不要使用进行深度分页,因为需要从所有分片中获取文档的数量深层页面,只有在最终返回整体有序结果集时才被丢弃.此操作是Elasticsearch的分布式特性所固有的,并且在深度分页方面对于许多分布式系统来说都是常见的.

使用 search_after ,您可以以无状态方式分页转发文档,这是必需的

  • 对第一个搜索响应返回的文档进行排序(默认情况下,文档按_score排序)
  • 将来自一个搜索请求的匹配中的最后一个文档的排序字段的值传递为下一个请求的"search_after": []的值.

在使用后搜索文档中,搜索请求按NumberOfCommits降序排序,然后按Name降序排序.用于每个这些排序字段的值在SearchAfter(...)中传递,分别是Project.First.NumberOfCommitsProject.First.Name属性的值.这告诉Elasticsearch返回具有与每个字段的排序约束相对应并与请求中提供的值相关的排序字段值的文档.例如,以NumberOfCommits降序提供的值为775意味着Elasticsearch只应考虑值小于775的文档(并且对所有排序字段和提供的值都这样做).

如果您需要进一步研究任何NEST文档,请单击页面上的"EDIT"链接:

上的Elasticsearch文档

,它将带您到文档的github存储库,并带有页面的原始asciidoc markdown:

在该页面内,将链接回到原始NEST源代码,从该源代码生成asciidoc.在这种情况下,原始文件是in docs here.

My code looks like this:

var request = new SearchRequest<ElasticSearchJsonObject._Source>
 {
    //Sort = new List<ISort>
    //{
    //    new SortField { Field = Field<ElasticSearchJsonObject>(p=>)}
    //},
    SearchAfter = new List<object> {

    },                    
    Size = 20,
    Query = query
  };                               

Reality is I don't understand this. Over here ElasticSearchJsonObject._Source is the class to map returned results.

My documents are simple text documents and I only want documents sorted according to score so document Id is not relevant.

There was already a question like this on SO but I can't find it somehow.


Update

After looking at answer I updated my code and though query obtained does work. It return result in kibana but not in NEST.

This is the new updated code:

var request = new SearchRequest<ElasticSearchJsonObject.Rootobject>
            {
                Sort = new List<ISort>
                {
                    new SortField { Field = "_id", Order = SortOrder.Descending}
                },
                SearchAfter = new List<object> {
                   "0fc3ccb625f5d95b973ce1462b9f7"
                },                    
                Size = 1,
                Query = query
            };

Over here I am using size=1 just for test as well as hard code _id value in SearchAfter.

The query generated by NEST is:

{
  "size": 1,
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ],
  "search_after": [
    "0fc3ccb625f5d95b973ce1462b9f7"
  ],
  "query": {
    "match": {
      "content": {
        "query": "lahore",
        "fuzziness": "AUTO",
        "prefix_length": 3,
        "max_expansions": 10
      }
    }
  }
}

The response from the ES does say successful but no results are returned.

  • Results do return in Kibana
  • Query status is successful
  • But...
  • Total returned is 0 in NEST
  • Sort value is null in kibana I used TrackScores = true to solve this issue

Here is the debug information:

Valid NEST response built from a successful low level call on POST: /extract/_source/_search?typed_keys=true
# Audit trail of this API call:
 - [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.1002662
# Request:
{"size":1,"sort":[{"_id":{"order":"desc"}}],"search_after":["0fc3ccb625f5d95b973ce1462b9f7"],"query":{"match":{"content":{"query":"lahore","fuzziness":"AUTO","prefix_length":3,"max_expansions":10}}}}
# Response:
{"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

So please tell me where I am wrong and what can be the problem and how to solve it.


Update 2:

Code in Controller:

Connection String:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
settings.DisableDirectStreaming();
settings.DefaultIndex("extract");
var client = new ElasticClient(settings);

Query:

var query = (dynamic)null;
query = new MatchQuery
 {
    Field = "content",
    Query = content,
    Fuzziness = Fuzziness.Auto,
    PrefixLength = 3,
    MaxExpansions = 10
   };

Query Builder

var request = new SearchRequest<ElasticSearchJsonObject.Rootobject>
            {
                Sort = new List<ISort>
                {
                    new SortField { Field = "_id", Order = SortOrder.Descending}
                },
                SearchAfter = new List<object> {
                   documentid //sent as parameter
                },                    
                Size = 1, //for testing 1 other wise 10
                TrackScores = true,
                Query = query
            };

JSON Query I use this code to get query I posted above. This query is then passed to kibana with GET <my index name>/_Search and there it works

var stream = new System.IO.MemoryStream();
client.SourceSerializer.Serialize(request, stream);
var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());

ES Response

string responseJson = "";
                ElasticSearchJsonObject.Rootobject response = new ElasticSearchJsonObject.Rootobject();
                var res = client.Search<object>(request);
                if (res.ApiCall.ResponseBodyInBytes != null)
                {
                    responseJson = System.Text.Encoding.UTF8.GetString(res.ApiCall.ResponseBodyInBytes);
                    try
                    {
                        response = JsonConvert.DeserializeObject<ElasticSearchJsonObject.Rootobject>(responseJson);
                    }
                    catch (Exception)
                    {
                        var model1 = new LoginSignUpViewModel();
                        return PartialView("_NoResultPage", model1);
                    }
                }

This is where things go wrong. Above debug information was captured from response

ElasticSearchJsonObject

Some how I think problem might be here somewhere? The class is generated by taking response from NEST in Search request.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ESAPI
{
    public class ElasticSearchJsonObject
    {
        public class Rootobject
        {
            public int took { get; set; }
            public bool timed_out { get; set; }
            public _Shards _shards { get; set; }
            public Hits hits { get; set; }
        }

        public class _Shards
        {
            public int total { get; set; }
            public int successful { get; set; }
            public int skipped { get; set; }
            public int failed { get; set; }
        }

        public class Hits
        {
            public int total { get; set; }
            public float max_score { get; set; }
            public Hit[] hits { get; set; }
        }

        public class Hit
        {
            public string _index { get; set; }
            public string _type { get; set; }
            public string _id { get; set; }
            public float _score { get; set; }
            public _Source _source { get; set; }
        }

        public class _Source
        {
            public string content { get; set; }
            public Meta meta { get; set; }
            public File file { get; set; }
            public Path path { get; set; }
        }

        public class Meta
        {
            public string title { get; set; }
            public Raw raw { get; set; }
        }

        public class Raw
        {
            public string XParsedBy { get; set; }
            public string Originator { get; set; }
            public string dctitle { get; set; }
            public string ContentEncoding { get; set; }
            public string ContentTypeHint { get; set; }
            public string resourceName { get; set; }
            public string ProgId { get; set; }
            public string title { get; set; }
            public string ContentType { get; set; }
            public string Generator { get; set; }
        }

        public class File
        {
            public string extension { get; set; }
            public string content_type { get; set; }
            public DateTime last_modified { get; set; }
            public DateTime indexing_date { get; set; }
            public int filesize { get; set; }
            public string filename { get; set; }
            public string url { get; set; }
        }

        public class Path
        {
            public string root { get; set; }
            public string _virtual { get; set; }
            public string real { get; set; }
        }
    }
}

I am sure this can be used to get response.

Please note that in case of simple search this code works:

so for this query below my code is working:

var request = new SearchRequest
                {
                    From = 0,
                    Size = 20,
                    Query = query
                };

解决方案

Using from/size is not recommended for deep pagination because of the amount of documents that need to be fetched from all shards for a deep page, only to be discarded when finally returning an overall ordered result set. This operation is inherent to the distributed nature of Elasticsearch, and is common to many distributed systems in relation to deep pagination.

With search_after, you can paginate forward through documents in a stateless fashion and it requires

  • the documents returned from the first search response are sorted (documents are sorted by _score by default)
  • passing the values for the sort fields of the last document in the hits from one search request as the values for "search_after": [] for the next request.

In the Search After Usage documentation, a search request is made with sort on NumberOfCommits descending, then by Name descending. The values to use for each of these sort fields are passed in SearchAfter(...) and are the values of Project.First.NumberOfCommits and Project.First.Name properties, respectively. This tells Elasticsearch to return documents that have values for the sort fields that correspond to the sort constraints for each field, and relate to the values supplied in the request. For example, sort descending on NumberOfCommits with a supplied value of 775 means that Elasticsearch should only consider documents with a value less than 775 (and to do this for all sort fields and supplied values).

If you ever need to dig further into any NEST documentation, click the "EDIT" link on the page:

which will take you to the github repository of the documentation, with the original asciidoc markdown for the page:

Within that page will be a link back to the original NEST source code from which the asciidoc was generated. In this case, the original file is SearchAfterUsageTests.cs in the 6.x branch

这篇关于NEST Api Search在NEST中返回null,但在Kibana中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆