NEST API to configure built in analyzers


#1

Hi,

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?

[String(Name = "FULLADDRESS", Analyzer = "Simple")]
public string FullAddress { get; set; }

About to test this but how do I decorate using the pattern analyzer?


(Greg Marzouka) #2

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")
				)
			)
		)
	)
);

You may find the docs on auto mapping useful. Hope that helps!


#3

That was the perfect answer. I was close but got a little lost. I wish there were more examples in the documentation


(system) #4