Skip to content

Instantly share code, notes, and snippets.

@lukas-vlcek
Last active December 25, 2015 11:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukas-vlcek/6972064 to your computer and use it in GitHub Desktop.
Save lukas-vlcek/6972064 to your computer and use it in GitHub Desktop.
Testing search time analyzer. This works as well now.
#!/bin/sh
echo "Elasticsearch version and build:"
curl localhost:9200; echo; echo;
echo "Delete index"
curl -X DELETE 'localhost:9200/i'; echo; echo;
echo "Create index with analysis and mappings"
curl -X PUT 'localhost:9200/i' -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"index_time" : {
"type" : "custom",
"tokenizer" : "standard",
"filter" : ["lowercase"]
},
"search_time" : {
"type" : "custom",
"tokenizer" : "standard",
"filter" : ["lowercase","synonym"]
}
},
"filter" : {
"synonym" : {
"type" : "synonym",
"synonyms" : [
"fast, quick"
]
}}},
"mappings" : {
"t" : {
"properties" : {
"text" : {
"type" : "string",
"index_analyzer" : "index_time",
"search_analyzer" : "search_time"
}}}}}}'; echo; echo;
# Wait for all the index shards to be allocated
curl -s -X GET 'http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=5s' > /dev/null
echo "Test synonyms for 'fast': should output two tokens"
echo ===========================
curl -X POST 'localhost:9200/i/_analyze?analyzer=search_time&format=text&text=fast'; echo; echo;
echo "Index data: 'Quick brown fox'"; curl -X POST 'localhost:9200/i/t' -d '{
"text" : "Quick brown fox"
}'; echo; echo;
echo "Refresh Lucene reader"; curl -X POST 'localhost:9200/i/_refresh'; echo; echo;
echo "Testing search"
echo ===========================
echo "1) query_string: fast - is search_analyzer used?";
curl -X GET 'localhost:9200/_search' -d '{"query":{"query_string":{"query":"fast","default_field":"text"}}}'; echo; echo;
echo "2) query_string: fast - forcing search_analyzer";
curl -X GET 'localhost:9200/_search' -d '{"query":{"query_string":{"query":"fast","default_field":"text","analyzer":"search_time"}}}'; echo; echo;
@lukas-vlcek
Copy link
Author

Ok, the issue is in wrong nesting level of mapping section.

Fixed recreation script is this:

#!/bin/sh

echo "Elasticsearch version and build:"
curl localhost:9200; echo; echo;

echo "Delete index"
curl -X DELETE 'localhost:9200/i'; echo; echo;

echo "Create index with analysis and mappings"
curl -X PUT 'localhost:9200/i' -d '{
  "settings" : {
    "analysis" : {
      "analyzer" : {
        "index_time" : {
          "type" : "custom",
          "tokenizer" : "standard",
          "filter" : ["lowercase"]
        },
        "search_time" : {
          "type" : "custom",
          "tokenizer" : "standard",
          "filter" : ["lowercase","synonym"]
        }
      },
      "filter" : {
        "synonym" : {
          "type" : "synonym",
          "synonyms" : [
            "fast, quick"
          ]
  }}}},
  "mappings" : {
    "t" : {
      "properties" : {
        "text" : {
          "type" : "string",
          "index_analyzer" : "index_time",
          "search_analyzer" : "search_time"
}}}}}'; echo; echo;

# Wait for all the index shards to be allocated
curl -s -X GET 'http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=5s' > /dev/null

echo "Test synonyms for 'fast': should output two tokens"
echo ===========================
curl -X POST 'localhost:9200/i/_analyze?analyzer=search_time&format=text&text=fast'; echo; echo;

echo "Index data: 'Quick brown fox'"; curl -X POST 'localhost:9200/i/t' -d '{
  "text" : "Quick brown fox"
}'; echo; echo;

echo "Refresh Lucene reader"; curl -X POST 'localhost:9200/i/_refresh'; echo; echo;

echo "Testing search"
echo ===========================
echo "1) query_string: fast - is search_analyzer used?";
curl -X GET 'localhost:9200/_search' -d '{"query":{"query_string":{"query":"fast","default_field":"text"}}}'; echo; echo;

echo "2) query_string: fast - forcing search_analyzer";
curl -X GET 'localhost:9200/_search' -d '{"query":{"query_string":{"query":"fast","default_field":"text","analyzer":"search_time"}}}'; echo; echo;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment