我正在配置一个tokenizer,它通过下划线char以及所有其他标点符号来分割单词。我决定使用word_delimiter过滤器。然后我将我的分析器设置为所需字段的默认值。
我有两个问题:
这是我的模板,数据对象,分析器测试和搜索请求:
PUT simple
{
"template" : "simple",
"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"underscore_splits_words" : {
"tokenizer" : "standard",
"filter" : ["word_delimiter"],
"generate_word_parts" : true,
"preserve_original" : true
}
}
}
},
"mappings": {
"_default_": {
"properties" : {
"request" : { "type" : "string", "analyzer" : "underscore_splits_words" }
}
}
}
}
}
数据对象:
POST simple/0
{ "request" : "GET /queue/1/under_score-hyphenword/poll?ttl=300&limit=10" }
这会返回令牌:"","得分","连字符",但没有" underscore_splits_words":
POST simple/_analyze?analyzer=underscore_splits_words
{"/queue/1/under_score-hyphenword/poll?ttl=300&limit=10"}
搜索结果
命中:
GET simple/_search?q=hyphenword
命中:
POST simple/_search
{
"query": {
"query_string": {
"query": "hyphenword"
}
}
}
小姐:
GET simple/_search?q=score
小姐:
POST simple/_search
{
"query": {
"query_string": {
"query": "score"
}
}
}
请建议一个正确的方法来实现我的目标。谢谢!
答案 0 :(得分:6)
您应该可以使用“简单”分析器来实现此功能。不需要自定义分析器,因为简单的分析器结合使用字母标记器和小写标记器(因此,任何非字母字符都表示新标记)。您没有获得任何匹配的原因是您没有在查询中指定字段,因此您正在查询_all字段,这主要是为了方便全文搜索。
PUT myindex
{
"mappings": {
"mytype": {
"properties": {
"request": {
"type": "string",
"analyzer": "simple"
}
}
}
}
}
POST myindex/mytype/1
{ "request" : "GET /queue/1/key_word-hyphenword/poll?ttl=300&limit=10" }
GET myindex/mytype/_search?q=request:key
POST myindex/mytype/_search
{
"query": {
"query_string": {
"default_field": "request",
"query": "key"
}
}
}
POST myindex/mytype/_search
{
"query": {
"bool": {
"must": [
{ "match": { "request": "key"}}
]
}
}
}
查询的输出看起来是正确的:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.095891505,
"hits": [
{
"_index": "myindex",
"_type": "mytype",
"_id": "1",
"_score": 0.095891505,
"_source": {
"request": "GET /queue/1/key_word-hyphenword/poll?ttl=300&limit=10"
}
}
]
}
}
如果要省略要搜索的特定字段(未推荐),可以在创建索引时为索引中的所有映射设置默认分析器。 (注意,不推荐使用此功能,出于性能/稳定性原因,不应使用它。)
PUT myindex
{
"mappings": {
"_default_": {
"index_analyzer": "simple"
}
}
}
POST myindex/mytype/1
{ "request" : "GET /queue/1/key_word-hyphenword/poll?ttl=300&limit=10" }
GET myindex/mytype/_search?q=key
您将获得相同的结果(1次点击)。