简体   繁体   English

如何通过索引文档中字段的部分文本来搜索Elasticsearch索引?

[英]How to search a elasticsearch index by partial text of a field in the indexed document?

I have an ElsaticSearch index where I keep certain data. 我有一个ElsaticSearch索引,其中保存了某些数据。 Each document in the index has a field named file_name in a nested document. 索引中的每个文档在嵌套文档中都有一个名为file_name的字段。 So a doc looks like 所以文档看起来像

{
  ...
  "file_data":{
     "file_name": "sample_filename_acp24_20180223_1222.json"
  }
  ...
}

I want my search to return above document if I search for sample , filename , acp24 and 20180223 and likewise. 如果我搜索samplefilenameacp2420180223等,我希望我的搜索返回上面的文档。

So far I tried following analyzers and full text search queries. 到目前为止,我尝试了跟踪分析器和全文搜索查询。 But still it doesn't return the above doc if I searched for acp24 , 20180223 . 但仍然,如果我搜索了它不返回上述文档acp2420180223

Index Mapping 索引对应

{
"index_name": {
    "mappings": {
        "type": {
            "properties": {
                "file_data": {
                    "type": "nested",
                    "properties": {
                        "file_name": {
                            "type": "text",
                            "analyzer": "keyword_analyzer",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                 }
                             }
                         }
                     }
                 }
             }                  
         }
     }
 }
}       

Analyzer 分析仪

{
  "analysis": {
    "analyzer": {
      "keyword_analyzer":{
        "type": "pattern",
        "pattern":"\\W|_", 
        "lowercase": true
      } 
    }
  }
}

Search Query 搜索查询

{
  "query": {
    "match_phrase_prefix": {
      "_all": {
        "query": "20180223",
        "analyzer": "keyword_analyzer"
      }
    }
  }
}

Any help on how to achieve this is very much appreciated. 非常感谢您提供有关如何实现此目标的帮助。 I have spent so many hours with this and still couldn't find a solution. 我花了很多时间来解决这个问题,但仍然找不到解决方案。

If I understand right, you could use the wildcard query : 如果我理解正确,则可以使用通配符查询

POST /my_index

{
  "query" : {
        "wildcard" : {
            "file_data.file_name" : {
                     "wildcard" : "sample_*filename_acp24*", "boost" : 2.0  
            }
        }
   }
}

(tested with elasticsearch 6.1, might need to change the syntax for other versions) (经过elasticsearch 6.1的测试,可能需要更改其他版本的语法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM