- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我按如下方式为我的查询编制索引:
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 库。
最佳答案
有计划improve the documentation对于 5.x,一旦 GA 版本发布;我知道文档在很多地方可能会更清晰,非常感谢这方面的任何帮助:)
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 mappings是最强大的,可以表达在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 使用 LogEntryModel
发出请求时或 PercolatedQuery
作为请求中的通用类型参数(例如 T
中的 .Search<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)
)
)
)
)
);
Query
PercolatedQuery
上的属性(property)被映射为 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"
}
}
}
}
}
]
}
}
这是渗透文档实例的示例。渗透也可以针对已经索引的文档运行
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>()
)
)
);
关于c# - 使用 NEST 渗透,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40683926/
我是新来的,所以我会尽量使我的帖子清晰易读。 在浏览某些站点的日志时,我遇到了一些我想在封闭服务器中重新创建/测试的黑客攻击尝试。我制作了一个简单的 PHP 网页,它获取一个名为“id”的变量,并且没
我有一个 NSView,它覆盖了其父窗口的内容 View 。该 View 有一个单击事件处理程序,可将其从内容 View 中删除。在这个观点之中,我还有另外一个观点。当我在此内部 View 中拖动鼠标
我正在读取 csv 中的链接并使用它来抓取。我希望 Node 渗透在抓取之间等待,可能是在抓取 10 个站点之后。我尝试使用 sleep ,但它似乎在错误的时间 sleep ,而下一个站点正在被抓取,
页面部分HTML代码 3 303 eur if (price_json.price != '0') { var price_container = docume
我按如下方式为我的查询编制索引: client.Index(new PercolatedQuery { Id = "std_query", Query = new QueryConta
我有一个使用多个 的表单.它们的位置使得弹出式 CSS 日历应该出现在其他人的字段上。但是,其他日期的文本字段最终位于日历的前面。 这只是一个 IE 问题,因为 Firefox 和 Safari 工
我正在编写一个与外部站点连接的 Electron 应用程序。我发现,cheerio 无法抓取需要登录的网站。所以我现在使用渗透,但它显示以下错误。 (get) 已加载 [get] https://so
我想实现一个Osmosis的任务,我想读取.osm.pbf文件并将其写入数据库: C:\osmosis-latest\bin>osmosis --read-pbf file="C:\sbox\maps
我是一名优秀的程序员,十分优秀!