【问题标题】:Using NEST to percolate使用 NEST 进行渗透
【发布时间】:2016-11-18 18:36:35
【问题描述】:

我正在按如下方式索引我的查询:

client.Index(new PercolatedQuery
{
    Id = "std_query",
    Query = new QueryContainer(new MatchQuery
    {
        Field = Infer.Field<LogEntryModel>(entry => entry.Message),
        Query = "just a text"
    })
}, d => d.Index(EsIndex));

client.Refresh(EsIndex);

现在,我如何使用 ES 的过滤器功能来匹配传入文档与此查询?说这方面缺乏 NEST 文档将是一种轻描淡写的说法。我尝试使用client.Percolate 调用,但现在已弃用,他们建议使用搜索 api,但不告诉如何将其与 percolator 一起使用...

我正在使用 ES v5 和相同版本的 NEST lib。

【问题讨论】:

    标签: c# elasticsearch nest elasticsearch-percolate


    【解决方案1】:

    一旦 GA 版本发布,我们计划在 5.x 中improve the documentation;我知道文档在许多地方可能会更清晰,并且非常感谢这方面的任何帮助:)

    The documentation for the Percolate query 是从 the integration test for it 生成的。在这里提取所有部分作为示例,using details from you other question。首先,让我们定义 POCO 模型

    public class LogEntryModel
    {
        public string Message { get; set; }
    
        public DateTimeOffset Timestamp { get; set; }
    }
    
    public class PercolatedQuery
    {
        public string Id { get; set; }
    
        public QueryContainer Query { get; set; }
    }
    

    我们将流畅地映射所有属性,而不是使用映射属性。 fluent 映射是最强大的,可以表达 Elasticsearch 中的所有映射方式。

    现在,创建连接设置和客户端以使用 Elasticsearch。

    var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
    var logIndex = "log_entries";
    var connectionSettings = new ConnectionSettings(pool)
        // infer mapping for logs
        .InferMappingFor<LogEntryModel>(m => m
            .IndexName(logIndex)
            .TypeName("log_entry")
        )
        // infer mapping for percolated queries
        .InferMappingFor<PercolatedQuery>(m => m
            .IndexName(logIndex)
            .TypeName("percolated_query")
        );
    
    var client = new ElasticClient(connectionSettings);
    

    我们可以指定索引名称和类型名称来推断我们的 POCO;也就是说,当 NEST 使用LogEntryModelPercolatedQuery 作为请求中的泛型类型参数(例如.Search&lt;T&gt;() 中的T)发出请求时,它将使用推断的索引名称和类型名称,如果它们不是在请求中指定。

    现在,删除索引,以便我们可以从头开始

    // delete the index if it already exists
    if (client.IndexExists(logIndex).Exists)
        client.DeleteIndex(logIndex);
    

    并创建索引

    client.CreateIndex(logIndex, c => c
        .Settings(s => s
            .NumberOfShards(1)
            .NumberOfReplicas(0)
        )
        .Mappings(m => m
            .Map<LogEntryModel>(mm => mm
                .AutoMap()
            )
            .Map<PercolatedQuery>(mm => mm
                .AutoMap()
                .Properties(p => p
                    // map the query field as a percolator type
                    .Percolator(pp => pp
                        .Name(n => n.Query)
                    )
                )
            )
        )
    );
    

    PercolatedQuery 上的 Query 属性映射为 percolator 类型。这是 Elasticsearch 5.0 中的新功能。映射请求看起来像

    {
      "settings": {
        "index.number_of_replicas": 0,
        "index.number_of_shards": 1
      },
      "mappings": {
        "log_entry": {
          "properties": {
            "message": {
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              },
              "type": "text"
            },
            "timestamp": {
              "type": "date"
            }
          }
        },
        "percolated_query": {
          "properties": {
            "id": {
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              },
              "type": "text"
            },
            "query": {
              "type": "percolator"
            }
          }
        }
      }
    }
    

    现在,我们已准备好为查询编制索引

    client.Index(new PercolatedQuery
    {
        Id = "std_query",
        Query = new MatchQuery
        {
            Field = Infer.Field<LogEntryModel>(entry => entry.Message),
            Query = "just a text"
        }
    }, d => d.Index(logIndex).Refresh(Refresh.WaitFor));
    

    查询索引后,让我们过滤文档

    var logEntry = new LogEntryModel
    {
        Timestamp = DateTimeOffset.UtcNow,
        Message = "some log message text"
    };
    
    // run percolator on the logEntry instance
    var searchResponse = client.Search<PercolatedQuery>(s => s
        .Query(q => q
            .Percolate(p => p
                // field that contains the query
                .Field(f => f.Query)
                // details about the document to run the stored query against.
                // NOTE: This does not index the document, only runs percolation
                .DocumentType<LogEntryModel>()
                .Document(logEntry)
            )
        )
    );
    
    // outputs 1
    Console.WriteLine(searchResponse.Documents.Count());
    

    ID为"std_query"的渗透查询返回searchResponse.Documents

    {
      "took" : 117,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "log_entries",
            "_type" : "percolated_query",
            "_id" : "std_query",
            "_score" : 0.2876821,
            "_source" : {
              "id" : "std_query",
              "query" : {
                "match" : {
                  "message" : {
                    "query" : "just a text"
                  }
                }
              }
            }
          }
        ]
      }
    }
    

    这是一个渗透文档实例的示例。 Percolation 也可以针对已编入索引的文档运行

    var searchResponse = client.Search<PercolatedQuery>(s => s
        .Query(q => q
            .Percolate(p => p
                // field that contains the query
                .Field(f => f.Query)
                // percolate an already indexed log entry
                .DocumentType<LogEntryModel>()
                .Id("log entry id")
                .Index<LogEntryModel>()
                .Type<LogEntryModel>()
            )
        )
    );
    

    【讨论】:

    • 不是每个英雄都穿着斗篷。如果我在星期一之前没有一个可行的解决方案,我的老板会扯掉我的脑袋。你真的救了我的命,所以谢谢你:)。
    • 不用担心,很高兴它有帮助:)
    • 非常感谢您提供这个不错的过滤器代码示例。
    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多