Skip to content

Instantly share code, notes, and snippets.

@jfiedler
Last active August 29, 2015 13:57
Show Gist options
  • Save jfiedler/9485429 to your computer and use it in GitHub Desktop.
Save jfiedler/9485429 to your computer and use it in GitHub Desktop.
package com.demandware.elasticsearch.regression;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.junit.Assert.assertEquals;
import java.nio.charset.Charset;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.node.Node;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.common.io.Resources;
/**
* Test case validating a regression seen while migrating to ES 1.0. Problem was on search service side with dynamic
* mappings templates. The advertised notion around 'multi_field' did not work as expected. Secondly, the the mappings
* (i.e. custom analyzer) did not seem to be applied correctly when we used the old 'multi_field' notation.
*/
public class _DynamicMappingTemplate
{
private Client client;
private Node node;
@Before
public void setup()
{
// fire up ES with local discovery transient storage
Settings settings = ImmutableSettings.settingsBuilder().put( "gateway.type", "none" ).build();
node = nodeBuilder().local( true ).data( true ).settings( settings ).node();
client = node.client();
// wait for node to be up
client.admin().cluster().prepareHealth( "_all" ).setWaitForYellowStatus().execute().actionGet();
}
@After
public void cleanup()
{
if ( client != null )
{
client.close();
}
if ( node != null )
{
node.close();
}
}
@Test
public void testName()
throws Exception
{
// load custom index settings (i.e. custom analyzer)
Settings settings =
ImmutableSettings.settingsBuilder().loadFromClasspath( "settings/custom-analyzer-1.yml" ).build();
// load the type mapping definition (i.e. dynamic template based)
String mapping =
Resources
.toString( Resources.getResource( "mappings/dynamic-template-1.json" ), Charset.forName( "UTF-8" ) );
// create the test index and apply the type mappings
client.admin().indices().prepareCreate( "test-index" ).setSettings( settings )
.addMapping( "test-type", mapping ).execute().actionGet();
// wait for yellow cluster state (making sure the index is there)
client.admin().cluster().prepareHealth( "test-index" ).setWaitForYellowStatus().execute().actionGet();
// index some data and refresh
client.prepareIndex( "test-index", "test-type" ).setSource( "myString", "1111-2222" ).execute().actionGet();
client.admin().indices().prepareRefresh( "test-index" ).execute().actionGet();
// search for exact match on not analyzed field (exact match)
SearchResponse resp =
client.prepareSearch( "test-index" ).setTypes( "test-type" )
.setQuery( termQuery( "myString", "1111-2222" ) ).execute().actionGet();
assertEquals( 1, resp.getHits().getTotalHits() );
// search with matching keyword on text field (custom analyzer)
resp =
client.prepareSearch( "test-index" ).setTypes( "test-type" )
.setQuery( matchQuery( "myString.text", "1111-2222" ).operator( MatchQueryBuilder.Operator.AND ) )
.execute().actionGet();
assertEquals( 1, resp.getHits().getTotalHits() );
// search with token that should have been produced by the custom analyzer
resp =
client.prepareSearch( "test-index" ).setTypes( "test-type" )
.setQuery( matchQuery( "myString.text", "11112222" ).operator( MatchQueryBuilder.Operator.AND ) )
.execute().actionGet();
assertEquals( 1, resp.getHits().getTotalHits() );
// search with reordered number groups (whould hit with standard but not with custom analyzer)
resp =
client.prepareSearch( "test-index" ).setTypes( "test-type" )
.setQuery( matchQuery( "myString.text", "2222-1111" ).operator( MatchQueryBuilder.Operator.AND ) )
.execute().actionGet();
assertEquals( 0, resp.getHits().getTotalHits() );
}
}
index:
analysis:
analyzer:
dw_docid:
type : custom
tokenizer : dw_docid_tokenizer
filter : [dw_docid_filter]
filter:
dw_docid_filter:
type: word_delimiter
generate_word_parts: false
generate_number_parts : true
catenate_words: false
catenate_numbers: true
catenate_all: false
split_on_case_change: false
preserve_original: false
stem_english_possessive: false
tokenizer:
dw_docid_tokenizer:
type: path_hierarchy
delimiter: '-'
{
"test-type" : {
"dynamic_templates" : [
{
"template_1" : {
"match" : "*",
"mapping" : {
"type" : "multi_field",
"fields" : {
"{name}" : {
"type": "string",
"index" : "not_analyzed"
},
"text" : {
"type": "string",
"index" : "analyzed",
"analyzer": "dw_docid"
}
}
}
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment