Nest Elasticsearch通配符查询用作查询字符串,但不能使用流畅的API

问题描述 投票:0回答:1

我的索引中大约有一百个测试文档,这些文档是使用NBuilder构建的:

[
  {
    "title" : "Title1",
    "text" : "Text1"
  },
  {
    "title" : "Title2",
    "text" : "Text2"
  },
  {
    "title" : "Title3",
    "text" : "Text3"
  }
]

我想用通配符查询它们,以查找所有以“ Text”开头的“ text”项。但是,当我在Nest中使用两个通配符方法时,会得到两个不同的结果。

var response = await client.SearchAsync<FakeFile>(s => s.Query(q => q
    .QueryString(d => d.Query("text:Text*")))
    .From((page - 1) * pageSize)
    .Size(pageSize));

这将返回100个结果。但是我正在尝试使用流利的API而不是querystring。

var response = await client.SearchAsync<FakeFile>(s => s
    .Query(q => q
        .Wildcard(c => c
            .Field(f => f.Text)
            .Value("Text*"))));

这将返回0个结果。我是Elasticsearch的新手。我试图使该示例尽可能简单,以确保我逐个理解它。我不知道为什么第二个查询没有返回任何内容。请帮助。

c# elasticsearch nest
1个回答
0
投票

假设您的映射如下所示:

{
  "mappings": {
    "doc": {
      "properties": {
        "title": {
          "type": "text"
        },
        "text":{
          "type": "text"
        }
      }
    }
  }
}

尝试一下(Value应该为小写):

var response = await client.SearchAsync<FakeFile>(s => s
.Query(q => q
    .Wildcard(c => c
        .Field(f => f.Text)
        .Value("text*"))));

或者这个(不知道f.Text是否具有text属性):

var response = await client.SearchAsync<FakeFile>(s => s
.Query(q => q
    .Wildcard(c => c
        .Field("text")
        .Value("text*"))));

Kibana语法:

GET index/_search
{
  "query": {
    "wildcard": {
      "text": {
        "value": "text*"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.