【问题标题】:Elasticsearch terms aggregation and lowercase valuesElasticsearch 术语聚合和小写值
【发布时间】:2020-09-20 17:28:30
【问题描述】:

我正在使用以下搜索查询来填充自动完成下拉列表,其中包含基于用户类型的值。

{
    _source: 'event',
    query: {
        simple_query_string: {
            query: ''+term+'*', // converts to string; adds * to match prefix
            fields: ['event'] 
        }
    },
    size:0,
    track_total_hits: false,
    aggs: {
        filterValues: {
            composite: {
                size: 100,
                sources: [
                    { "filterValue": { "terms": { "field": 'event', "missing_bucket": true } } }
                ],
                after: { 'event': after }
            },
        }
    }
}

用于索引的字段值:UYB 4.9.0 AJF 5 Qnihsbm

目前如果用户输入第一个字母uU,Elasticsearch会以小写uyb 4.9.0 ajf 5 qnihsbm返回上述值。我怎样才能保持这种行为,但返回的值与它被索引时完全一样?即UYB 4.9.0 AJF 5 Qnihsbm

字段映射

"mappings": {
    "properties": {
        "event": {
            "type": "keyword",
            "normalizer": "normalizer_1"
        },
        .....
    }
}

ES 配置

"settings": {
    "analysis": {
        "normalizer": {
            "normalizer_1": {
                "type": "custom",
                "char_filter": [],
                "filter": ["lowercase", "asciifolding"]
            }
        }
    }
},

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl elasticsearch-query


    【解决方案1】:

    您的映射中应该有另一个字段不是小写的,并且是您搜索的那个。

    "mappings": {
        "properties": {
            "event": {
                "type": "keyword",
                "fields": {
                    "search": {
                        "type": "keyword",
                        "normalizer": "normalizer_1",
                    }
                }
            },
            .....
        }
    }
    

    然后您的查询需要在 event.search 而不是 `event` 上运行

        simple_query_string: {
            query: ''+term+'*', // converts to string; adds * to match prefix
            fields: ['event.search'] 
        }                      ^
                               |
                           add this
    

    其余的都可以保持不变。

    【讨论】:

      【解决方案2】:

      如果您启用了_source,那么您可以使用source filtering 检索索引值。

      https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html 了解有关_source 字段及其用例的更多信息。

      【讨论】:

      • 这不是问题,他已经在使用源过滤并返回event 字段。问题是关键字字段是小写的,聚合返回的是小写的值。
      • @Val 我看到了,没注意到
      猜你喜欢
      • 2017-07-19
      • 2014-07-09
      • 2019-12-06
      • 1970-01-01
      • 2021-06-06
      • 2021-06-06
      • 2015-11-06
      • 2015-01-21
      • 2020-10-30
      相关资源
      最近更新 更多