匹配短语查询
{
"query": {
"match_phrase": {
"approved_labelled_products.companies": "SOMETHING INC"
}
}返回特定的结果,但match_phrase_prefix查询
{
"query": {
"match_phrase_prefix": {
"approved_labelled_products.companies": "SOME.*"
}
}
}返回一个空的结果集
"hits":
{
"total": 0,
"max_score": null,
"hits": []
}match_phrase_prefix至少必须返回由match_phrase查询获得的数据,但它没有返回。
数据的映射如下
"approved_labelled_products": {
"properties": {
"companies": {
"type": "keyword",
"null_value": "NULL",
"ignore_above": 9500
}
}
}发布于 2019-03-02 21:11:31
match_phrase和match_phrase_prefix查询是全文搜索查询,要求数据字段为text类型。它与您正在使用的keyword类型有很大的不同,现在让我解释一下您现在可以做什么,区别是什么。
我能让match_phrase_prefix工作吗?
是的,如果将字段类型更改为match_phrase_prefix,则可以使用text。
如何使用keyword字段搜索前缀?
keyword按原样存储和查询,没有任何分析。将其视为一个字符串;要查找所有具有给定前缀的字段的文档,只需使用prefix查询即可。
让我们定义我们的映射并插入几个文档:
PUT myindex
{
"mappings": {
"_doc": {
"properties": {
"approved_labelled_products": {
"properties": {
"companies": {
"type": "keyword",
"null_value": "NULL",
"ignore_above": 9500
}
}
}
}
}
}
}
POST myindex/_doc
{
"approved_labelled_products": {
"companies": "SOMETHING INC"
}
}现在我们可以发出如下查询:
POST myindex/_doc/_search
{
"query": {
"prefix": {
"approved_labelled_products.companies": "SOME"
}
}
}请注意,由于实际上没有执行任何分析,所以请求是区分大小写的,通过字符串"some"查询将不会返回结果。
text字段有何不同?
在索引期间,text字段为分析,这意味着输入字符串被拆分为令牌、低胁迫、保存一些元信息并构造一个倒置指数。
这允许有效地获取包含特定令牌或令牌组合的文档。
为了说明这一点,我们可以使用分析API。让我们先看看Elasticsearch如何分析keyword字段的数据:
POST _analyze
{
"analyzer" : "keyword",
"text": "SOMETHING INC"
}这将返回:
{
"tokens": [
{
"token": "SOMETHING INC",
"start_offset": 0,
"end_offset": 13,
"type": "word",
"position": 0
}
]
}正如您所看到的,它是一个包含所有大写字母的单个令牌。
现在,让我们看看standard分析器的功能(默认情况下text字段使用的分析器):
POST _analyze
{
"analyzer" : "standard",
"text": "SOMETHING INC"
}它将返回:
{
"tokens": [
{
"token": "something",
"start_offset": 0,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "inc",
"start_offset": 10,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 1
}
]
}正如您所看到的,它已经产生了两个令牌,都是低限制的。
希望这能帮上忙!
发布于 2019-03-01 05:34:34
您不必在match_phrase_prefix查询中使用通配符表达式。
用这个代替:
{
"query": {
"match_phrase_prefix": {
"approved_labelled_products.companies": "SOME"
}
}
}https://stackoverflow.com/questions/54938244
复制相似问题