【发布时间】:2018-08-08 14:32:42
【问题描述】:
我的弹性搜索索引有一个字符串类型的“消息”字段。我正在尝试搜索在消息字段中包含部分匹配的记录。例如,如果记录包含“Hello World”,则搜索“hello”、“Hello”、“o w”等...将导致匹配。
这是索引中的示例记录:
{
"_index": "applicationlogs",
"_type": "upstream",
"_id": "d7Hj12EBs1AV7_j_56mg",
"_score": null,
"_source": {
"@timestamp": "2018-02-27T09:33:16.7817021-06:00",
"level": "Information",
"messageTemplate": "Elasticsearch log level configured to Information",
"message": "Elasticsearch log level configured to Information",
"fields": {
"SourceContext": "Rpr.Agg.Program",
"ApplicationVersion": "1.0.0.0",
"ServiceName": "BDF_SERVICE_SWBR2DEV-JTC01",
"MachineName": "SWBR2DEV-JTC01",
"Application": 8
}
},
"sort": [
1519745596781
]
},
这是反映索引的类的相关部分:
public class ElasticsearchLogEntry
{
[Text(Name = "@timestamp")]
public DateTime TimeStamp { get; set; }
public string Level { get; set; }
[Text]
public string Message { get; set; }
}
这里是我到目前为止查询的相关部分,它只匹配完整的字符串:
public async Task<IEnumerable<ElasticsearchLogEntry>> GetAllLogEntries(string search, int skip, int take)
{
var filters = new List<Func<QueryContainerDescriptor<ElasticsearchLogEntry>, QueryContainer>>();
if (!String.IsNullOrEmpty(search))
filters.Add(fq => fq.Match(m => m.Field("message").Query(search)));
var response = await client.SearchAsync<ElasticsearchLogEntry>(i => i
.Index("applicationlogs")
.Type("upstream")
.Query(q => q.Bool(bq => bq.Filter(filters)))
.Sort(s => s.Descending(so => so.TimeStamp))
.Skip(skip)
.Take(take));
return response.Documents.AsEnumerable();
}
有什么方法可以让它正常工作吗?
【问题讨论】:
标签: c# elasticsearch nest elasticsearch-5 elasticsearch-net