![](/img/trans.png)
[英]How to search a string in the elasticsearch document(indexed) in golang?
[英]How to search a elasticsearch index by partial text of a field in the indexed document?
我有一个ElsaticSearch索引,其中保存了某些数据。 索引中的每个文档在嵌套文档中都有一个名为file_name
的字段。 所以文档看起来像
{
...
"file_data":{
"file_name": "sample_filename_acp24_20180223_1222.json"
}
...
}
如果我搜索sample
, filename
, acp24
和20180223
等,我希望我的搜索返回上面的文档。
到目前为止,我尝试了跟踪分析器和全文搜索查询。 但仍然,如果我搜索了它不返回上述文档acp24
, 20180223
。
索引对应
{
"index_name": {
"mappings": {
"type": {
"properties": {
"file_data": {
"type": "nested",
"properties": {
"file_name": {
"type": "text",
"analyzer": "keyword_analyzer",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
分析仪
{
"analysis": {
"analyzer": {
"keyword_analyzer":{
"type": "pattern",
"pattern":"\\W|_",
"lowercase": true
}
}
}
}
搜索查询
{
"query": {
"match_phrase_prefix": {
"_all": {
"query": "20180223",
"analyzer": "keyword_analyzer"
}
}
}
}
非常感谢您提供有关如何实现此目标的帮助。 我花了很多时间来解决这个问题,但仍然找不到解决方案。
如果我理解正确,则可以使用通配符查询 :
POST /my_index
{
"query" : {
"wildcard" : {
"file_data.file_name" : {
"wildcard" : "sample_*filename_acp24*", "boost" : 2.0
}
}
}
}
(经过elasticsearch 6.1的测试,可能需要更改其他版本的语法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.