简体   繁体   中英

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. Each document in the index has a field named file_name in a nested document. 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.

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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