【问题标题】:Search elasticsearch string field by wildcard通过通配符搜索 elasticsearch 字符串字段
【发布时间】: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


    【解决方案1】:

    我原以为我只是得到了 0 个结果,但事实证明我实际上得到了一个错误:

    {ServerError: 400Type: parsing_exception 原因:“[匹配] 查询没有 不支持[类型]"}

    我认为这是因为我在我的查询中设置了一个类型,但是即使在删除它之后我仍然得到错误。我认为这是因为我在使用 NEST / ElasticSearch.Net 5.x nuget 包时连接到 Amazon 上的 ES 6 实例。见this

    不幸的是,我使用 5.x nuget 包的全部原因是因为 6 不适合我或我团队中的其他开发人员。 api 调用总是给this error:

    UnexpectedElasticsearchClientException:找不到方法: 'Elasticsearch.Net.PostData`1 Elasticsearch.Net.RequestData.get_PostData()'。

    所以这让我有点纠结……

    编辑:他们发布了支持 6.x 的 ES AWS nuget 包的更新,因此我能够升级所有内容并克服这些错误。

    【讨论】:

    • 如您所见,NEST 5.x 与 Elasticsearch 6.x 不兼容。更新到 NEST 6.x 时,确保 Elasticsearch.Net 包也更新到兼容的版本(通常与 NEST 相同)。
    • @RussCam 不幸的是,我无法升级到 NEST 6.x,因为 ElasticSearch AWS 包与它不兼容 (github.com/bcuff/elasticsearch-net-aws/issues/33)。您能想到的任何解决方法?
    • 如果您的索引中没有多种类型,请尝试注释掉 .Type("upstream") 行。
    • @Justin 最简单的方法可能是针对 NEST 6.x 构建 Elasticsearch AWS 包,使用它然后针对项目打开 PR
    • 他们发布了 ES AWS 包的更新以支持 6.x,所以现在一切正常。
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    相关资源
    最近更新 更多