I'm trying to figure out how to set one of my fields to use the Pattern Analyzer with "\W|_" Basically the 'fullAddress' field could include an underscore within the postcode, eg WC1V_6hh.
I want to do a wildcard search eg. WC1V*
It looks like I should use the 'Simple Analyzer' which would break up the underscore and use lowercase. However, I'm sure I've seen that if a number was part of the underscore term then the simple / standard analysers wouldn't break it up. So in that case I would use the Pattern Analyser with the regex pattern \W|_
If I was to use the Simple Analyzer then I can decorate the poco property like this?
Yes, you can use either attributes or the fluent API to assign an analyzer to a field. Just FYI though, you need to create the index with the analyzer configured first.
For instance, if you have the following POCO:
public class MyDoc
{
[String(Name="mField", Analyzer = "my_pattern_analyzer")]
public string MyField { get; set; }
}
Then you'd create your index with my_pattern_analyzer, and use AutoMap() to pick up the String attribute set on the MyField property:
var response = client.CreateIndex("myindex", c => c
.Settings(s => s
.Analysis(a => a
.Analyzers(az => az
.Pattern("my_pattern_analyzer", p => p
.Pattern("\\W|_")
.Lowercase()
)
)
)
)
.Mappings(ms => ms
.Map<MyDoc>(m => m.AutoMap())
)
);
You can also use just the fluent API, which is actually preferred since it can handle more complex mappings that can't be expressed using attributes alone.
public class MyDoc
{
public string MyField { get; set; }
}
var response = client.CreateIndex("myindex", c => c
.Settings(s => s
.Analysis(a => a
.Analyzers(az => az
.Pattern("my_pattern_analyzer", p => p
.Pattern("\\W|_")
.Lowercase()
)
)
)
)
.Mappings(ms => ms
.Map<MyDoc>(m => m
.AutoMap()
.Properties(ps => ps
.String(str => str
.Name(p => p.MyField)
.Analyzer("my_pattern_analyzer")
)
)
)
)
);
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.