第二次使用时,scroll_id返回0次点击

时间:2017-10-10 11:36:30

标签: elasticsearch nest

我有这段代码在第一次搜索后得到scroll_id:

var initSearch = client.LowLevel.Search<dynamic>(INDEX, TYPE, QUERY, x => x.AddQueryString("scroll", "1m").AddQueryString("size", "2"));

string scrollId = initSearch.Body["_scroll_id"].ToString();

then I used the scrollid during the 2nd search but it didn't return any hits

var scrollSearch = client.LowLevel.ScrollGet<dynamic>(x => 
x.AddQueryString("scroll", "1m").AddQueryString("scroll_id", scrollId));
scrollId = scrollSearch.Body["_scroll_id"].ToString();

var searchHits = int.Parse(scrollSearch.Body["hits"]["total"].ToString());

searchHits.Count为零。可能是什么原因造成的?此外,当我再次循环进入scrollSearch时,我希望scrollid会改变,但它不会改变值。

1 个答案:

答案 0 :(得分:0)

size 2将在每个回复中返回2个文档,包括第一个回复。因此,如果给定查询的总文档小于或等于2,则将在第一个响应中返回所有文档。以下面的例子为例

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "messages";
    var connectionSettings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex)
        .PrettyJson()
        .DisableDirectStreaming()
        .OnRequestCompleted(response =>
            {
                if (response.RequestBodyInBytes != null)
                {
                    Console.WriteLine(
                        $"{response.HttpMethod} {response.Uri} \n" +
                        $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                }
                else
                {
                    Console.WriteLine($"{response.HttpMethod} {response.Uri}");
                }

                Console.WriteLine();

                if (response.ResponseBodyInBytes != null)
                {
                    Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                             $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                             $"{new string('-', 30)}\n");
                }
                else
                {
                    Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                             $"{new string('-', 30)}\n");
                }
            });

    var client = new ElasticClient(connectionSettings);

    if (client.IndexExists(defaultIndex).Exists)
    {
        client.DeleteIndex(defaultIndex);
    }

    client.IndexMany(new[]
    {
        new Message { Content = "message 1" },
        new Message { Content = "message 2" },
        new Message { Content = "message 3" },
        new Message { Content = "message 4" },
        new Message { Content = "message 5" },
        new Message { Content = "message 6" },
    });

    client.Refresh(defaultIndex);

    var searchResponse = client.Search<Message>(s => s
        .Scroll("1m")
        .Size(2)
        .Query(q => q
            .Terms(t => t
                .Field(f => f.Content.Suffix("keyword"))
                .Terms("message 1", "message 2")
            )
        )
    );

    searchResponse = client.Scroll<Message>("1m", searchResponse.ScrollId);
}

public class Message
{
    public string Content { get; set; }
}

搜索和滚动响应返回

------------------------------

POST http://localhost:9200/messages/message/_search?pretty=true&scroll=1m 
{
  "size": 2,
  "query": {
    "terms": {
      "content.keyword": [
        "message 1",
        "message 2"
      ]
    }
  }
}

Status: 200
{
  "_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAADGFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAxxZzNUp4bVFXSFJoLTNWMmdjUHotYWRBAAAAAAAAAMgWczVKeG1RV0hSaC0zVjJnY1B6LWFkQQAAAAAAAADJFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAyhZzNUp4bVFXSFJoLTNWMmdjUHotYWRB",
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "messages",
        "_type" : "message",
        "_id" : "AV8IkTSbM7nzQBTCbQok",
        "_score" : 0.6931472,
        "_source" : {
          "content" : "message 1"
        }
      },
      {
        "_index" : "messages",
        "_type" : "message",
        "_id" : "AV8IkTSbM7nzQBTCbQol",
        "_score" : 0.6931472,
        "_source" : {
          "content" : "message 2"
        }
      }
    ]
  }
}

------------------------------

POST http://localhost:9200/_search/scroll?pretty=true 
{
  "scroll": "1m",
  "scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAADGFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAxxZzNUp4bVFXSFJoLTNWMmdjUHotYWRBAAAAAAAAAMgWczVKeG1RV0hSaC0zVjJnY1B6LWFkQQAAAAAAAADJFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAyhZzNUp4bVFXSFJoLTNWMmdjUHotYWRB"
}

Status: 200
{
  "_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAADGFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAxxZzNUp4bVFXSFJoLTNWMmdjUHotYWRBAAAAAAAAAMgWczVKeG1RV0hSaC0zVjJnY1B6LWFkQQAAAAAAAADJFnM1SnhtUVdIUmgtM1YyZ2NQei1hZEEAAAAAAAAAyhZzNUp4bVFXSFJoLTNWMmdjUHotYWRB",
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.6931472,
    "hits" : [ ]
  }
}

------------------------------

由于给定查询只有2个匹配文档,size设置为2,因此在第一个响应中返回两个文档,后面的滚动响应不包含任何匹配。

您可以使用初始搜索响应中的total来确定是否需要调用滚动API以获取更多文档。

实际_scroll_id值是一个实现细节,可能会也可能不会更改后续调用的值。我不建议在其值上建立任何逻辑,但只使用后续滚动请求中最后一个滚动响应返回的_scroll_id值。