Elastic Search正则表达式未按预期工作。

huangapple go评论63阅读模式
英文:

Elastic Search Regex are not working as expected

问题

我在使弹性搜索正则表达式工作时遇到了问题。我有一个看起来像这样的文档:

{"content": "keySyAtUXpd8JxrpUH2Sd"}

我尝试了以下正则表达式 key[0-9A-Za-z_]{18},它在regexer.com上与字符串完全匹配,但当我从弹性搜索查询请求时,它没有显示任何匹配项。

这是我正在使用的请求:

curl -XGET 'https://localhost:9200/_search?pretty' -H 'Content-Type: application/json' -H 'Authorization: Basic redacted' -k -d '{
  "query": { "regexp": { "content": "key[0-9A-Za-z_]{18}" } }
}'

我还尝试了带有 .*key[0-9A-Za-z_]{18}.* 的正则表达式,尝试将 - 转义为 \\-,但似乎也不起作用。

英文:

I have go the problem in making elastic search regex work. I have a document that looks like this:

{"content": "keySyAtUXpd8JxrpUH2Sd"}

I have trying the following regex key[0-9A-Za-z_]{18} which perfectly matches with the string in regexer.com but when I query the request from elastic search it doesn't show any hits.

Here's the request that i'm using:

curl -XGET 'https://localhost:9200/_search?pretty' -H 'Content-Type: application/json' -H 'Authorization: Basic redacted' -k -d '{
  "query": { "regexp": { "content": "key[0-9A-Za-z_]{18}" } }
}'

I have also tried the regex with .*key[0-9A-Za-z_]{18}.*, tried to escape - as \\- but it doesn't seems to be working as well.

答案1

得分: 0

你应该添加 -H "Content-Type: application/json"

尝试这样做:

curl -u elastic:password -XPOST "https://localhost:9200/test_regex/_bulk?refresh" -H "Content-Type: application/json" -d'
{"index":{"_id":"1"}}
{"content": "keySyAtUXpd8JxrpUH2Sd"}
'
--- 
curl -u elastic:password -XGET "https://localhost:9200/_search?pretty" -H "Content-Type: application/json" -d'
{
  "query": { "regexp": { "content": "key[0-9A-Za-z_]{18}" } }
}'

Elastic Search正则表达式未按预期工作。

英文:

You should add -H "Content-Type: application/json"

Try this:

curl -u elastic:password -XPOST "https://localhost:9200/test_regex/_bulk?refresh" -H "Content-Type: application/json" -d'
{"index":{"_id":"1"}}
{"content": "keySyAtUXpd8JxrpUH2Sd"}
'

curl -u elastic:password -XGET "https://localhost:9200/_search?pretty" -H "Content-Type: application/json" -d'
{
  "query": { "regexp": { "content": "key[0-9A-Za-z_]{18}" } }
}'

Elastic Search正则表达式未按预期工作。

答案2

得分: 0

You need to run the regexp query against the content.keyword field

curl -XGET 'https://localhost:9200/_search?pretty' -H 'Content-Type: application/json' -H 'Authorization: Basic redacted' -k -d '{
  "query": { "regexp": { "content.keyword": "key[0-9A-Za-z_]{18}" } }
}'

PS: easier to test and provide feedback with real content and real queries Elastic Search正则表达式未按预期工作。

英文:

You need to run the regexp query against the content.keyword field

curl -XGET 'https://localhost:9200/_search?pretty' -H 'Content-Type: application/json' -H 'Authorization: Basic redacted' -k -d '{
  "query": { "regexp": { "content.keyword": "key[0-9A-Za-z_]{18}" } }
}'

PS: easier to test and provide feedback with real content and real queries Elastic Search正则表达式未按预期工作。

huangapple
  • 本文由 发表于 2023年6月1日 20:04:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定