弹性体“起始于”短语中的第一个单词 [英] Elasticsearch "starts with" first word in phrases

查看:131
本文介绍了弹性体“起始于”短语中的第一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Elasticsearch实现我的内容的A-Z导航。
我需要的是显示以例如开头的所有结果。 a,b,c,...等。



我试过:

 code>query:{
match_phrase_prefix:{
title:{
query:a
}
}
}

上面提到的查询还会显示结果,字符串中的一个单词以a开头。
示例:



title:Apfelpfannkuchen,



title:Affogato ,



title:Kalbsschnitzel a n A ceto Balsamico,



我想只显示第一个字开头的短语。



这里我使用的映射:

  $ params = array(
'index'=>'my_index',
'body'=> array b $ b'settings'=>数组(
'number_of_shards'=> 1,
'index'=>数组(
'analysis'=>数组b'filter'=>数组(
'nGram_filter'=>数组(
'type'=>'nGram',
'min_gram'=> 2,
'max_gram'=> 2 0,
'token_chars'=>数组('letter','digit','punctuation','symbol')

),
'analyzer'=>数组(
'nGram_analyzer'=>数组(
'type'=>'custom',
'tokenizer'=>'空格',
'filter'= > array('lowercase','asciifolding','nGram_filter')
),
'whitespace_analyzer'=>数组(
'type'=>'custom',
'tokenizer'=>'空格',
'filter'=>数组('小写','asciifolding')
),
'analyzer_startswith'=&
'tokenizer'=>'关键字',
'filter'=>'小写'




),
'mappings'=> array(
'tags'=> array(
'_all'=> array(
'type'=>'string',
'index_analyzer'=> 'nGram_analyzer',
'search_analyzer'=>'whitespace_analyzer'
),
'properties'=> array()

),
'posts'=>数组(
'_all'=>数组(
'index_analyzer'=>'nGram_analyzer',
'search_analyzer'=>'whitespace_analyzer'
),
'properties'=>数组(
'title'=>数组(
'type'=>'string',
'ind ex_analyzer'=>'analyzer_startswith',
'search_analyzer'=> 'analyzer_startswith'





);


解决方案

如果您使用默认映射,那么它将无法正常工作为你。



您需要使用关键字标记器小写过滤器在映射。



映射将是:

  {
设置:{
index:{
analysis:{
analyzer:{
analyzer_startswith:{
tokenizer:keyword ,
过滤器:小写
}
}
}
}
},
映射:{
test_index:{
properties:{
title:{
search_analyzer:analyzer_startswith,
index_analyzer:analyzer_startswith,
type:string
}
}
}
}
}

上搜索查询test_index

 code> {
query:{
match_phrase_prefix:{
title:{
查询:a
}
}
}
}

它将以 a


开头返回所有帖子标题

I try to implement an A - Z navigation for my content with Elasticsearch. What I need, is displaying all results which begins with e.g. a,b,c,... etc.

I've tried:

"query": {
        "match_phrase_prefix" : {
        "title" : {
            "query" : "a"
        }
      }
    }

The query mentioned above also display results, where within the string a word begins with a. Example:

"title": "Apfelpfannkuchen",

"title": "Affogato",

"title": "Kalbsschnitzel an Aceto Balsamico",

I want to display only phrase where the FIRST word begins with a.

Here the mapping I use:

$params = array(
            'index' => 'my_index',
            'body' => array(
                'settings' => array(
                    'number_of_shards' => 1,
                    'index' => array(
                        'analysis' => array(
                            'filter' => array(
                                'nGram_filter' => array(
                                    'type' => 'nGram',
                                    'min_gram' => 2,
                                    'max_gram' => 20,
                                    'token_chars' => array('letter', 'digit', 'punctuation', 'symbol')
                                )
                            ),
                            'analyzer' => array(
                                'nGram_analyzer' => array(
                                    'type' => 'custom',
                                    'tokenizer' => 'whitespace',
                                    'filter' => array('lowercase', 'asciifolding', 'nGram_filter')
                                ),
                                'whitespace_analyzer' => array(
                                    'type' => 'custom',
                                    'tokenizer' => 'whitespace',
                                    'filter' => array('lowercase', 'asciifolding')
                                ),
                                'analyzer_startswith' => array(
                                    'tokenizer' => 'keyword',
                                    'filter' => 'lowercase'
                                )
                            )
                        )
                    )
                ),
                'mappings' => array(
                    'tags' => array(
                        '_all' => array(
                            'type' => 'string',
                            'index_analyzer' => 'nGram_analyzer',
                            'search_analyzer' => 'whitespace_analyzer'
                        ),
                        'properties' => array()

                    ),
                    'posts' => array(
                        '_all' => array(
                            'index_analyzer' => 'nGram_analyzer',
                            'search_analyzer' => 'whitespace_analyzer'
                        ),
                        'properties' => array(
                            'title' => array(
                                'type' => 'string',
                                'index_analyzer' => 'analyzer_startswith',
                                'search_analyzer' => 'analyzer_startswith'
                            )
                        )
                    )
                )
            )
        );

解决方案

If you are using default mapping then it will not work for you.

You need to use keyword tokenizer and lowercase filter in mapping.

Mapping Will be :

{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },
    "mappings": {
        "test_index": {
            "properties": {
                "title": {
                    "search_analyzer": "analyzer_startswith",
                    "index_analyzer": "analyzer_startswith",
                    "type": "string"
                }
            }
        }
    }
}

Search query on test_index :

{
    "query": {
        "match_phrase_prefix": {
            "title": {
                "query": "a"
            }
        }
    }
}

It will return all post title starting with a

这篇关于弹性体“起始于”短语中的第一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆