英文:
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}" } }
}'
英文:
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}" } }
}'
答案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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论