简体   繁体   English

通过通配符搜索elasticsearch字符串字段

[英]Search elasticsearch string field by wildcard

My elasticsearch index has a "message" field of type string. 我的elasticsearch索引具有字符串类型的“ message”字段。 I'm trying to search for records that contain a partial match on the message field. 我正在尝试搜索在消息字段上包含部分匹配项的记录。 For example, if the record contains "Hello World", then searching for "hello", "Hello", "ow", etc... would result in a match. 例如,如果记录包含“ Hello World”,则搜索“ hello”,“ Hello”,“ ow”等将导致匹配。

Here is a sample record in the index: 这是索引中的示例记录:

{
        "_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
        ]
      },

Here is the relevant part of the class that mirrors the index: 这是反映索引的类的相关部分:

public class ElasticsearchLogEntry
    {
        [Text(Name = "@timestamp")]
        public DateTime TimeStamp { get; set; }
        public string Level { get; set; }
        [Text]
        public string Message { get; set; }
    }

And here is the relevant part of the query that I have so far, which only matches on the full string: 这是我到目前为止查询的相关部分,仅与完整字符串匹配:

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();
        }

Any ideas how I can get it working correctly? 有什么想法可以使它正常工作吗?

I had assumed that I was simply getting 0 results back, but it turns out I was actually getting an error: 我以为我只是返回0个结果,但事实证明我实际上是出错了:

{ServerError: 400Type: parsing_exception Reason: "[match] query does not support [type]"} {ServerError:400Type:parsing_exception原因:“ [match]查询不支持[type]”}

I thought this was because I'm setting a Type in my query, however even after removing it I still get the error. 我以为这是因为我在查询中设置了类型,但是即使将其删除,我仍然会收到错误消息。 I think this is because I'm connecting to a ES 6 instance on Amazon while using the NEST / ElasticSearch.Net 5.x nuget packages. 我认为这是因为我正在使用NEST / ElasticSearch.Net 5.x nuget包连接到Amazon上的ES 6实例。 See this . 看到这个

Unfortunately, the whole reason why I'm using the 5.x nuget packages is because 6 didn't work for me or the other developers on my team. 不幸的是,我之所以使用5.x nuget软件包的全部原因是因为6不适用于我或团队中的其他开发人员。 The api calls always give this error : api调用始终会出现此错误

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

So this leaves me in a bit of a bind... 所以这让我有点束缚...

EDIT: They released an update to the ES AWS nuget package that supports 6.x, so I was able to upgrade everything and get past these errors. 编辑:他们发布了支持6.x的ES AWS nuget软件包的更新,因此我能够升级所有内容并克服这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM