Note that there are some explanatory texts on larger screens.

plurals
  1. POFilename search with ElasticSearch
    text
    copied!<p>I want to use ElasticSearch to search filenames (not the file's content). Therefore I need to find a part of the filename (exact match, no fuzzy search).</p> <p>Example:<br> I have files with the following names:</p> <pre><code>My_first_file_created_at_2012.01.13.doc My_second_file_created_at_2012.01.13.pdf Another file.txt And_again_another_file.docx foo.bar.txt </code></pre> <p>Now I want to search for <code>2012.01.13</code> to get the first two files.<br> A search for <code>file</code> or <code>ile</code> should return all filenames except the last one.</p> <p>How can i accomplish that with ElasticSearch?</p> <p>This is what I have tested, but it always returns zero results:</p> <pre><code>curl -X DELETE localhost:9200/files curl -X PUT localhost:9200/files -d ' { "settings" : { "index" : { "analysis" : { "analyzer" : { "filename_analyzer" : { "type" : "custom", "tokenizer" : "lowercase", "filter" : ["filename_stop", "filename_ngram"] } }, "filter" : { "filename_stop" : { "type" : "stop", "stopwords" : ["doc", "pdf", "docx"] }, "filename_ngram" : { "type" : "nGram", "min_gram" : 3, "max_gram" : 255 } } } } }, "mappings": { "files": { "properties": { "filename": { "type": "string", "analyzer": "filename_analyzer" } } } } } ' curl -X POST "http://localhost:9200/files/file" -d '{ "filename" : "My_first_file_created_at_2012.01.13.doc" }' curl -X POST "http://localhost:9200/files/file" -d '{ "filename" : "My_second_file_created_at_2012.01.13.pdf" }' curl -X POST "http://localhost:9200/files/file" -d '{ "filename" : "Another file.txt" }' curl -X POST "http://localhost:9200/files/file" -d '{ "filename" : "And_again_another_file.docx" }' curl -X POST "http://localhost:9200/files/file" -d '{ "filename" : "foo.bar.txt" }' curl -X POST "http://localhost:9200/files/_refresh" FILES=' http://localhost:9200/files/_search?q=filename:2012.01.13 ' for file in ${FILES} do echo; echo; echo "&gt;&gt;&gt; ${file}" curl "${file}&amp;pretty=true" done </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload