在Elasticsearch中将字符串与减号匹配 [英] Match string with minus character in elasticsearch

查看:270
本文介绍了在Elasticsearch中将字符串与减号匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在数据库中,我有以下条目:

So in DB I have this entry:

Mark-Whalberg

使用术语搜索时

Mark-Whalberg

我不匹配.

为什么?减号是我所了解的字符吗?它表示排除"吗?

Why? Is minus a special character what I understand? It symbolizes "exclude"?

查询是这样的:

{"query_string":{"query":' Mark-Whalberg ',"default_operator":"AND"}}

{"query_string": {"query": 'Mark-Whalberg', "default_operator": "AND"}}

搜索其他所有内容,例如:

Searching everything else, like:

Mark
Whalberg
hlb
Mark Whalberg

返回一个匹配项.

这是否存储为两个不同的块?在搜索字词中包含减号时,如何获得匹配?

Is this stored as two different pieces? How can I get a match when including the minus sign in the search term?

--------------编辑--------------

--------------EDIT--------------

这是当前查询:

var fields = [
    "field1",
    "field2",
];

{"query_string":{"query": '*Mark-Whalberg*',"default_operator": "AND","fields": fields}};

推荐答案

您有分析仪配置问题.

让我解释一下.在ElasticSearch中定义索引时,没有为该字段指示任何分析器.这表示它是 标准分析器 将适用.

根据文档:

标准分析仪

标准分析仪是默认分析仪,如果没有,则使用默认分析仪指定的.它提供了基于语法的标记化(基于Unicode标准中指定的Unicode文本分段算法附件#29),并且适用于大多数语言.

The standard analyzer is the default analyzer which is used if none is specified. It provides grammar based tokenization (based on the Unicode Text Segmentation algorithm, as specified in Unicode Standard Annex #29) and works well for most languages.

还要回答您的问题:

为什么?减号是我所了解的字符吗?它象征着排除"?

Why? Is minus a special character what I understand? It symbolizes "exclude"?

对于Standard Analyzer,.这并不意味着排除",而是一种特殊的字符,在分析后将被删除.

For the Standard Analyzer, yes it is. It doesn't mean "exclude" but it is a special char that will be deleted after analysis.

来自文档:

术语查询为什么不匹配我的文档?

[...]有多种分析文本的方法:默认标准分析器会删除大多数标点符号,将文本分解为单个单词,和小写字母.例如,标准分析仪将字符串"Quick Brown Fox!"术语[快速,棕色,狐狸].[...]

[...] There are many ways to analyze text: the default standard analyzer drops most punctuation, breaks up text into individual words, and lower cases them. For instance, the standard analyzer would turn the string "Quick Brown Fox!" into the terms [quick, brown, fox]. [...]

示例:

如果您输入以下文字:

"The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."

然后标准分析仪将生成:

Then the Standard Analyzer will produce :

[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog's, bone ]

如果您不想使用分析仪,则有两种解决方案:

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