在弹性搜索查询中获取不匹配完整字符串的数据

我的数据以以下格式存储在弹性搜索中

 {
            "_index": "wallet","_type": "wallet","_id": "5dfcbe0a6ca963f84470d852","_score": 0.69321066,"_source": {
                "email": "test20011@gmail.com","wallet": "test20011@operatorqa2.akeodev.com","countryCode": "+91","phone": "7916318809","name": "test20011"
            }
        },{
            "_index": "wallet","_id": "5dfcbe0a6ca9634d1c70d856","_source": {
                "email": "test50011@gmail.com","wallet": "test50011@operatorqa2.akeodev.com","phone": "3483330496","name": "test50011"
            }
        },"_id": "5dfcbe0a6ca96304b370d857","_source": {
                "email": "test110021@gmail.com","wallet": "test110021@operatorqa2.akeodev.com","phone": "2744697207","name": "test110021"
            }
        }

如果我们使用下面的查询,则找不到该记录

   {
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "wallet": {
                            "query": "operatorqa2.akeodev.com","operator": "and"
                        }
                    }
                },{
                    "match": {
                        "email": {
                            "query": "operatorqa2.akeodev.com","operator": "and"
                        }
                    }
                }
            ]
        }
    }
}

记录应该找到我是否在查询下面传递

    {
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "wallet": {
                            "query": "test20011@operatorqa2.akeodev.com",{
                    "match": {
                        "email": {
                            "query": "test20011@operatorqa2.akeodev.com","operator": "and"
                        }
                    }
                }
            ]
        }
    }
}

我已经在电子邮件和钱包字段上创建了索引。 每当用户通过电子邮件或钱包搜索数据时,我不确定用户发送的字符串是电子邮件还是钱包,因此我正在使用bool

记录应查找用户是否发送了完整的电子邮件地址或完整的电子钱包地址。 请帮助我找到解决方法

newke2730 回答:在弹性搜索查询中获取不匹配完整字符串的数据

正如其他社区成员提到的那样,在问这样的问题时,您应该指定您正在使用的Elasticsearch的版本并提供映射。

从具有默认映射的Elasticsearch版本5开始,您只需更改查询即可针对字段的确切版本而不是针对分析的版本进行查询。默认情况下,Elasticsearch将字符串映射到类型为text(对于全文搜索为分析)和keyword(对于完全匹配搜索为未分析)的多字段。在查询中,您将随后查询<fieldname>.keyword字段:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "wallet.keyword": "test20011@operatorqa2.akeodev.com"
                    }
                },{
                    "match": {
                        "email.keyword": "test20011@operatorqa2.akeodev.com"
                    }
                }
            ]
        }
    }
}

如果您使用的是版本5之前的Elasticsearch版本,请将索引属性从analyzed更改为not_analyzed,然后重新索引数据。

映射段:

{
  "email": {
    "type" "string","index": "not_analyzed"
  }
}

您的查询仍然不需要使用and运算符。它看起来与我上面发布的查询相同,除了必须查询emailwallet字段,而不是email.keywordwallet.keyword

我可以向您推荐以下Elastic博客文章,涉及该主题:Strings are dead,long live strings!

,

由于我没有到您的索引架构的映射,因此我假设您使用的是ES默认值(可以使用mapping API获得此值),对于您来说,是wallet和{{1 }}字段将使用默认分析器(标准分析器)定义为email

此分析器不会将这些文本识别为邮件ID,而是会为text创建三个令牌,您可以使用analyze APIs进行检查。

http://localhost:9200/_analyze?text=test50011@operatorqa2.akeodev.com&tokenizer=standard

test50011@operatorqa2.akeodev.com

您这里需要的是custom analyzer for mails using UAX URI Mail tokenizer,该字段用于电子邮件字段。这将为{ "tokens": [ { "token": "test50011","start_offset": 0,"end_offset": 9,"type": "<ALPHANUM>","position": 1 },{ "token": "operatorqa2","start_offset": 10,"end_offset": 21,"position": 2 },{ "token": "akeodev.com","start_offset": 22,"end_offset": 33,"position": 3 } ] } 生成一个适当的令牌(仅1个),如下所示:

http://localhost:9200/_analyze?text=test50011@operatorqa2.akeodev.com&tokenizer=uax_url_email

test50011@operatorqa2.akeodev.com

现在,您可以看到它并没有分裂{ "tokens": [ { "token": "test50011@operatorqa2.akeodev.com","type": "<EMAIL>","position": 1 } ] } ,因此,当您使用相同的查询进行搜索时,它也会生成相同的令牌,并且ES会在令牌之间进行令牌匹配。

如果需要帮助,请告诉我,它的设置和使用非常简单。

本文链接:https://www.f2er.com/2884447.html

大家都在问