我目前使用弹性搜索,并有几种类型的查询,其中我使用match_phrase查询。我正在使用的索引使用了一个文本信息的英语分析器。当我搜索短语时,我期望得到准确的结果,但如果我的搜索词有一个英语单词——比如remove——它也会标记出"removed"、"removed"、"removed"这样的单词。
如何在我的短语匹配中防止这种情况?对于这样的查询,除了match_phrase之外,还有更好的选择吗?
这在不改变分析器的情况下是否可能?下面是我的查询(结构化的,所以它可以做其他事情):
query: {
fields : ['_id', 'ownerId'],
from: 0,
size: 20,
query: {
filtered: {
filter: {
and: [group ids]
},
query: {
bool: {
must: {
match_phrase: {
text: "remove"
}
}
}
}
}
}
}
这是我的索引:
[MappingTypes.MESSAGE]: {
properties: {
text: {
type: 'string',
index: 'analyzed',
analyzer: 'english',
term_vector: 'with_positions_offsets'
},
ownerId: {
type: 'string',
index: 'not_analyzed',
store: true
},
groupId: {
type: 'string',
index: 'not_analyzed',
store: true
},
itemId: {
type: 'string',
index: 'not_analyzed',
store: true
},
createdAt: {
type: 'date'
},
editedAt: {
type: 'date'
},
type: {
type: 'string',
index: 'not_analyzed'
}
}
}
您可以使用多字段以不同的方式使用字段(一个用于精确匹配,一个用于部分匹配等)。
您可以使用标准分析器(也是默认分析器)摆脱词干提取。您可以使用以下映射
创建索引POST test_index
{
"mappings": {
"test_type": {
"properties": {
"text": {
"type": "string",
"index": "analyzed",
"analyzer": "english",
"term_vector": "with_positions_offsets",
"fields": {
"standard": {
"type": "string"
}
}
},
"ownerId": {
"type": "string",
"index": "not_analyzed",
"store": true
},
"groupId": {
"type": "string",
"index": "not_analyzed",
"store": true
},
"itemId": {
"type": "string",
"index": "not_analyzed",
"store": true
},
"createdAt": {
"type": "date"
},
"editedAt": {
"type": "date"
},
"type": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
之后,当你想要精确匹配时,你需要使用text.standard
,当你想要执行词干提取(想要匹配移除的移除)时,你可以恢复到text
你也可以更新当前的映射,但是在这两种情况下你都必须重新索引你的数据。
PUT test_index/_mapping/test_type
{
"properties": {
"text": {
"type": "string",
"index": "analyzed",
"analyzer": "english",
"term_vector": "with_positions_offsets",
"fields": {
"standard": {
"type": "string"
}
}
}
}
}
这有帮助吗?