Elasticsearch:在下划线上分割单词;搜索没有任何结果

时间:2015-08-05 16:06:24

标签: elasticsearch

我正在配置一个tokenizer,它通过下划线char以及所有其他标点符号来分割单词。我决定使用word_delimiter过滤器。然后我将我的分析器设置为所需字段的默认值。

我有两个问题:

  • Analyzer将字符串拆分为单词,但不保留原始字符串,尽管有preserve_original选项。见分析查询。
  • 按下划线分割的子字符串搜索仍然不会产生任何结果

这是我的模板,数据对象,分析器测试和搜索请求:

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"
        }
      }
}

请建议一个正确的方法来实现我的目标。谢谢!

1 个答案:

答案 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

使用查询DSL查询:

POST myindex/mytype/_search
 {
     "query": {
         "query_string": {
             "default_field": "request", 
             "query": "key"
         }
     }
 }

使用查询DSL的另一个查询:

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"
            }
         }
      ]
   }
}

如果要省略要搜索的特定字段(未推荐),可以在创建索引时为索引中的所有映射设置默认分析器。 (注意,不推荐使用此功能,出于性能/稳定性原因,不应使用它。)

使用默认映射创建索引,以使用“简单”分析器

分析_all字段
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次点击)。