首页 文章

如何在Elasticsearch中对分析/标记化字段进行排序?

提问于
浏览
5

我们在索引中存储 title 字段,并希望将该字段用于两个目的:

  • 我们正在使用ngram过滤器进行分析,因此我们可以提供自动完成和即时结果

  • 我们希望能够在 title 字段上使用ASC排序列出结果而不是得分 .

索引/过滤器/分析器的定义如下:

array(
    'number_of_shards' => $this->shards,
    'number_of_replicas' => $this->replicas,
    'analysis' => array(
        'filter' => array(
            'nGram_filter' => array(
                'type' => 'nGram',
                'min_gram' => 2,
                'max_gram' => 20,
                'token_chars' => array('letter','digit','punctuation','symbol')
            )
        ),

        'analyzer' => array(
            'index_analyzer' => array(
                'type' => 'custom',
                'tokenizer' =>'whitespace',
                'char_filter' => 'html_strip',
                'filter' => array('lowercase','asciifolding','nGram_filter')
            ),
            'search_analyzer' => array(
                'type' => 'custom',
                'tokenizer' =>'whitespace',
                'char_filter' => 'html_strip',
                'filter' => array('lowercase','asciifolding')
            )
        )
    )
),

当我们对 title 字段进行排序时,我们遇到的问题是无法预测的结果 . 在做了一点搜索之后,我们在ElasticSearch的 sort 手册页的末尾找到了这个...(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_memory_considerations

对于基于字符串的类型,不应分析/标记化排序的字段 .

How can we both analyze the field and sort on it later? Do we need to store the field twice with one using not_analyzed in order to sort? Since the field _source is also storing the title value in it's original state, can that not be used to sort on?

1 回答

  • 7

    您可以在Elasticsearch中使用Multi Field Type的内置概念 .

    multi_field类型允许映射具有相同值的多个core_types . 这可以非常方便,例如,当想要映射字符串类型时,一旦它被分析,一次它没有被分析 .

    在Elasticsearch Reference中,请查看String Sorting and Multi Fields指南,了解如何设置所需内容 .

    请注意,Elasticsearch 0.90.X和1.X之间的多字段映射配置已更改 . 根据您的版本使用适当的以下指南:

相关问题