如何同时制作字段关键字类型和文本类型以同时启用聚合和自由文本搜索

How to make a field Keyword type and Text type at the same time to enable Aggregations and free text search simultainously

我们有一个带有 ElasticSearch 后端的应用程序。我正在尝试为某些字段启用自由文本搜索并聚合相同的字段。自由文本搜索期望字段为文本类型,聚合器期望它们为关键字类型。 Copy_to 似乎无法将关键字复制到文本字段。

此聚合适用于关键字类型:

            var aggs = await _dataService.Search<Vehicle>(s => s
                .Size(50)
                .Query(q => q.MatchAll())
                .Aggregations(a => a
                    .Terms("stockLocation_city", c => c
                        .Field(f => f.StockLocation.City)
                        .Size(int.MaxValue)
                        .ShowTermDocCountError(false)
                    )
                    .Terms("stockLocation_country", c => c
                        .Field(f => f.StockLocation.Country)
                        .Size(int.MaxValue)
                        .ShowTermDocCountError(false)
                    )
               )
            );

架构如下所示:

            "stockLocation": {
                "type": "object",
                "properties": {
                    "full_text": {
                        "type": "text",
                        "analyzer": "custom_analyzer"
                    },
                    "city": {
                        "type": "keyword", 
                        "copy_to":"full_text"
                    },
                    "country": {
                        "type": "keyword",
                        "copy_to":"full_text"
                    }
                }
            }

使用复制到 full_text 属性:

的文本字段进行全文搜索的查询
            var qieryDescriptor = query.SimpleQueryString(p => p.Query(searchQuery.FreeText));

以及 ElasticClient 实例化:

        public ElasticSearchService(ElasticSearchOptions elasticSearchOptions)
        {
            _elasticSearchOptions = elasticSearchOptions;

            var settings = new ConnectionSettings(
                _elasticSearchOptions.CloudId,
                new Elasticsearch.Net.BasicAuthenticationCredentials(_elasticSearchOptions.UserName, _elasticSearchOptions.Password)
                )
                .DefaultIndex(_elasticSearchOptions.IndexAliasName)
                .DefaultMappingFor<Vehicle>(m => m.IndexName(_elasticSearchOptions.IndexAliasName));

            _elasticClient = new ElasticClient(settings);
        }

我查看了文档,但没有在任何地方看到这个特定的用例,所以我一定是做错了什么。

如何在同一字段上同时启用聚合和自由文本搜索?

干杯

您正在寻找的是“多字段”功能: Multi-Fields

那样你在文档中有相同的条目并且引擎索引它两次 - 一次作为全文,一次作为关键字。