c# - 如何在NEST中为内部数组创建MoreLikeThis查询?

标签 c# elasticsearch nest elasticsearch-net

我想为此json查询创建NEST等效项:

{
    "query": {
        "more_like_this" : {
            "fields" : ["storages.items"],
            "like" :  ["a","b"],
            "min_term_freq": 1,
            "min_doc_freq": 1
        }
    }
}

我的模型是:
public class Data
{
    public string Name { get; set; }

    public InnerStorage[] Storages { get; set; }

}

public class InnerStorage
{
    public string[] Items { get; set; }
}

问题是我找不到将字符串数组传递给MoreLikeThis参数的方法。

LikeDescriptor仅包含
Text(string likeText)


Document(Func<LikeDocumentDescriptor<T>, ILikeDocument> selector)

我只能创建的请求是这样的
 var data =
      client.Search<Data>(
        x =>
          x.Query(
            q =>
              q.MoreLikeThis(
                s =>
                  s.Fields(Field.Create("storages.items"))
                    .Like(sel => sel.Document(d => d.Document(
                      new Data(){Storages =new[]{new InnerStorage(){Items = new[] {"a", "b"}}}}
                                                       ))))));

它包含完整的数据文档(但我只想传递字符串数组(项)),并为以下内容创建错误的请求:
"like": [{
                "_index": null,
                "_type": "data",
                "doc": {
                    "storages": [{
                        "items": ["a", "b"]
                    }]
                }
            }]

最佳答案

您可以通过为每个术语调用fluent方法Text将更多的术语添加到查询的类似部分中,如以下示例所示:

var searchResponse = client.Search<Document>(s => s
    .Query(q => q
        .MoreLikeThis(mlt => mlt
            .Fields(f => f.Field(ff => ff.Title))
            .Like(l => l.Text("a").Text("b")))));

希望能帮助到你。

关于c# - 如何在NEST中为内部数组创建MoreLikeThis查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556092/

相关文章:

elasticsearch - 通过 url 设置 Kibana 仪表板过滤器

elasticsearch - 如何使用 NEST Bulk Api 将文档添加到 elasticsearch

elasticsearch - 如何使用Nest在ElasticSearch中以纯文本形式搜索字段?

c# - 单个队列消息上的多个 Azure WebJob 函数?

c# - 动态改变 Texture2D

django - 如何将索引属性强制为数字(或 float )?

string - Elasticsearch.NET搜索记录以字符串字段(NEST)中的值开头

c# - T 之间的映射 --> IHandler<T>

c# - AutoFac - 在 app_start 上初始化重量级单例

amazon-web-services - AWS缓冲区在流传输到Lambda之前先登录CloudWatch