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

Vol*_*ych 4 elasticsearch

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

我有两个问题:

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

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

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" }
            }
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

数据对象:

POST simple/0 
{ "request" : "GET /queue/1/under_score-hyphenword/poll?ttl=300&limit=10" }
Run Code Online (Sandbox Code Playgroud)

这将返回标记:"under","score","hyphenword",但没有"underscore_splits_words":

POST simple/_analyze?analyzer=underscore_splits_words
{"/queue/1/under_score-hyphenword/poll?ttl=300&limit=10"}
Run Code Online (Sandbox Code Playgroud)

搜索结果

击中:

GET simple/_search?q=hyphenword
Run Code Online (Sandbox Code Playgroud)

击中:

POST simple/_search
{ 
"query": {
        "query_string": {
          "query": "hyphenword"
        }
      }
}
Run Code Online (Sandbox Code Playgroud)

小姐:

GET simple/_search?q=score
Run Code Online (Sandbox Code Playgroud)

小姐:

POST simple/_search
{ 
"query": {
        "query_string": {
          "query": "score"
        }
      }
}
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*ona 6

您应该可以使用"简单"分析器来实现此功能.不需要自定义分析器,因为简单的分析器结合使用字母标记器和小写标记器(因此,任何非字母字符都表示新标记).您没有获得任何匹配的原因是您没有在查询中指定字段,因此您正在查询_all字段,这主要是为了方便全文搜索.

创建索引

PUT myindex
{
    "mappings":     {
        "mytype": {
            "properties": {
                "request": {
                    "type": "string",
                    "analyzer": "simple"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

插入文档

POST myindex/mytype/1 
{ "request" : "GET /queue/1/key_word-hyphenword/poll?ttl=300&limit=10" }
Run Code Online (Sandbox Code Playgroud)

查询文档

GET myindex/mytype/_search?q=request:key
Run Code Online (Sandbox Code Playgroud)

使用Query DSL查询:

POST myindex/mytype/_search
 {
     "query": {
         "query_string": {
             "default_field": "request", 
             "query": "key"
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

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

POST myindex/mytype/_search
{
    "query": {
        "bool": {
            "must": [
                { "match": { "request": "key"}}
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查询的输出看起来正确:

{
   "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"
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

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

使用默认映射创建索引,以使用"简单"分析器分析_all字段

PUT myindex
{
    "mappings":     {
        "_default_": {
            "index_analyzer": "simple"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

插入文档

POST myindex/mytype/1 
{ "request" : "GET /queue/1/key_word-hyphenword/poll?ttl=300&limit=10" }
Run Code Online (Sandbox Code Playgroud)

查询索引而不指定字段

GET myindex/mytype/_search?q=key
Run Code Online (Sandbox Code Playgroud)

你会得到相同的结果(1击中).