public void UsingStaticPropertyField() { Field fieldString = "name"; /** but for expressions this is still rather involved */ var fieldExpression = Field <Project>(p => p.Name); /** this can be even shortened even further using a static import. * Now that is much terser than our first example using the constructor! */ fieldExpression = Field <Project>(p => p.Name); Expect("name") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); /** You can specify boosts in the field using a string, as well as using `Nest.Infer.Field` */ fieldString = "name^2.1"; fieldString.Boost.Should().Be(2.1); fieldExpression = Field <Project>(p => p.Name, 2.1); fieldExpression.Boost.Should().Be(2.1); Expect("name^2.1") .WhenSerializing(fieldString) .WhenSerializing(fieldExpression); }
public void NameCanSpecifyBoost() { Field fieldString = "name^2"; var fieldStringConstructor = new Field("name^2"); var fieldStringCreate = new Field("name^2", 3); //<1> NEST will take the boost from the name fieldString.Name.Should().Be("name"); fieldStringConstructor.Name.Should().Be("name"); fieldStringCreate.Name.Should().Be("name"); fieldString.Boost.Should().Be(2); fieldStringConstructor.Boost.Should().Be(2); fieldStringCreate.Boost.Should().Be(2); }
public void UsingConstructors() { var fieldString = new Field("name"); var fieldProperty = new Field(typeof(Project).GetProperty(nameof(Project.Name))); Expression <Func <Project, object> > expression = p => p.Name; var fieldExpression = new Field(expression); Expect("name") .WhenSerializing(fieldExpression) .WhenSerializing(fieldString) .WhenSerializing(fieldProperty); }
public void ImplicitConversion() { Field fieldString = "name"; Field fieldProperty = typeof(Project).GetProperty(nameof(Project.Name)); Expression <Func <Project, object> > expression = p => p.Name; Field fieldExpression = expression; Expect("name") .WhenSerializing(fieldString) .WhenSerializing(fieldProperty) .WhenSerializing(fieldExpression); }
public void UsingConstructorAlsoSetsComparisonValue() { /** When using the constructor and passing a value for `Name`, `Property` or `Expression`, * `ComparisonValue` is also set on the `Field` instance; this is used when * * - determining `Field` equality * - getting the hash code for a `Field` instance */ var fieldStringWithBoostTwo = new Field("name^2"); var fieldStringWithBoostThree = new Field("name^3"); Expression <Func <Project, object> > expression = p => p.Name; var fieldExpression = new Field(expression); var fieldProperty = new Field(typeof(Project).GetProperty(nameof(Project.Name))); fieldStringWithBoostTwo.GetHashCode().Should().NotBe(0); fieldStringWithBoostThree.GetHashCode().Should().NotBe(0); fieldExpression.GetHashCode().Should().NotBe(0); fieldProperty.GetHashCode().Should().NotBe(0); fieldStringWithBoostTwo.Should().Be(fieldStringWithBoostTwo); //<1> <<field-name-with-boost,Fields can constructed with a name that contains a boost>> }