如何在ElasticSearch中搜索词干?

时间:2019-06-23 11:36:50

标签: elasticsearch lucene full-text-search full-text-indexing fulltext-index

我在ElasticSarch中创建了一个索引:

PUT /test
{
    "settings": {
        "analysis" : {
            "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["lowercase", "my_stemmer"]
                }
            },
            "filter" : {
                "my_stemmer" : {
                    "type" : "stemmer",
                    "name" : "german"
                }
            }
        }
    }
}

当我尝试使用自己创建的分析仪执行某项操作时,它会起作用:

POST /test/_analyze
{
  "analyzer": "my_analyzer",
  "text":     "telefonierend"
}

它返回我“ telefonier”。没错。

但是怎么可能做这样的事情:

  1. 有一个文件带有单词“ telefonieren”(末尾只有“ n”)。
  2. 我要搜索“ telefonierend”(末尾为“ nd”)。
  3. 之所以匹配,是因为词干相等。

如果我尝试搜索“ telefonieren”,它将找不到包含单词“ telefonierend”的文件:

GET /test/_search
{
  "query": { "match": { "text": "telefonierend" } }
}

1 个答案:

答案 0 :(得分:0)

我想,我自己弄清楚了。

我编辑了GET请求:

  1. 我将分析器“ my_analyzer”放入查询中,并且
  2. 我将“ match”替换为“ match_phrase_prefix”。

现在看起来像这样:

GET /test/_search{  
   "query":{  
      "match_phrase_prefix":{  
         "text":{  
            "query":"telefonieren",
            "analyzer":"my_analyzer"
         }
      }
   }
}

有没有更好的解决方案(我在ElasticSearch方面没有太多经验)。

为什么这种解决方案不是最好的: 当文件包含“ telefonierend”并且我搜索“ telefonierend”时,得分将与搜索“ telefonieren”相同。