Exemplo n.º 1
0
        public void TestPartialFields()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .PartialFields(x => x.PartialField("partial1").Include("id", "name").Exclude("number"));
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{
                partial_fields: { 
                    ""partial1"" : { 
                        ""include"" : [ ""id"", ""name"" ],
                        ""exclude"" : [ ""number"" ] 
                    } 
                } 
            }";

            Assert.True(json.JsonEquals(expected));
        }
        public void BothConditionless()
        {
            var s = new SearchDescriptor <ElasticSearchProject>().From(0).Size(10)
                    .Query(q => q
                           .Conditionless(qs => qs
                                          .Query(qcq => qcq.Term("name", ""))
                                          .Fallback(qcf => qcf.Term("name", "")
                                                    )
                                          )
                           );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10 }";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 3
0
        public void CompletionSuggestDescriptorDefaultFuzzinessTest()
        {
            var completionSuggestDescriptor = new CompletionSuggestDescriptor <ElasticsearchProject>()
                                              .OnField("suggest")
                                              .Text("n")
                                              .Fuzzy();

            var json = TestElasticClient.Serialize(completionSuggestDescriptor);

            var expected = @"{
                              field: ""suggest"",
                              fuzzy: {},
                            }";

            Assert.IsTrue(json.JsonEquals(expected), json);
        }
Exemplo n.º 4
0
        public void ExistFilter()
        {
            var s = new SearchDescriptor <ElasticSearchProject>().From(0).Size(10)
                    .Filter(filter => filter
                            .Exists(f => f.Name)
                            );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
						exists : { field : ""name"" }
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 5
0
        public void MatchAllFilter()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Filter(f => f.MatchAll());

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
						match_all : {}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 6
0
        public void TestScriptScore()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .Query(q => q
                           .FunctionScore(fs => fs
                                          .Functions(func => func
                                                     .ScriptScore(ss => ss
                                                                  .Script("_score / pow(param1, param2)")
                                                                  .Params(p => p
                                                                          .Add("param1", 1.75)
                                                                          .Add("param2", 4)
                                                                          )
                                                                  .Lang("mvel")
                                                                  ).Filter(filter => filter
                                                                           .Term("term1", "termValue")
                                                                           ).Weight(5)
                                                     )
                                          )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{
                          query: {
                            function_score: {
                              functions : [
                                {
                                    script_score: {
                                        script: ""_score / pow(param1, param2)"",
                                        params: {
                                            param1: 1.75,
                                            param2: 4
                                        },
                                        lang: ""mvel""
                                    },
                                    filter:{
                                        term : {
                                            ""term1"":""termValue""
                                        }
                                    },
									weight: 5
                                }
                              ]
                            }
                          }
                        }";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 7
0
        public void BoolQueryMetadata()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(qd => qd
                           .Bool(b => b
                                 .Must(q => q.MatchAll())
                                 .MustNot(q => q.Term(p => p.Name, "elasticsearch.pm"))
                                 .Should(q => q.Term(p => p.Name, "elasticflume"))
                                 .MinimumNumberShouldMatch(1)
                                 .Boost(2.0)
                                 )
                           );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				query : {
						""bool"": {
							minimum_number_should_match: 1,
							boost: 2.0,
						
							""must"": [
								{
									""match_all"": {}
								}
							],
							""must_not"": [
								{
									""term"": {
										""name"": { value: ""elasticsearch.pm"" }
									}
								}
							],
							""should"": [
								{
									""term"": {
										""name"": { value: ""elasticflume"" }
									}
								}
							]
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 8
0
        public void SpanOrQuery()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .SpanOr(sn => sn
                                   .Clauses(
                                       c => c.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1),
                                       c => c.SpanFirst(sf => sf
                                                        .MatchTerm(f => f.Name, "elasticsearch.pm", 1.1)
                                                        .End(3)
                                                        )
                                       )
                                   )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{
				span_or: { 
					clauses: 
					[{
						span_term: { 
								name: {
									value: ""elasticsearch.pm"",
									boost: 1.1
								}
							}
						},
						{ 
							span_first: {
								match: {
									span_term: {
										name: {
											value: ""elasticsearch.pm"",
											boost: 1.1
										}
									}
								},
								end: 3
							}
						}
					]
			}}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 9
0
        public void TestSort()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Fields(e => e.Id, e => e.Name)
                    .SortAscending(e => e.LOC);
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
					sort: {
						""loc.sort"": ""asc""
					},
					fields: [""id"", ""name""]
				}"                ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 10
0
        public void TestSortAscendingOnSortField()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .SortAscending(f => f.Name);
            var json     = TestElasticClient.Serialize(s);
            var expected = @"  {
          from: 0,
          size: 10,
          sort: {
            ""name.sort"" : ""asc""
            }          
        }";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 11
0
        public void TestFuzzyQuery()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .Fuzzy(fz => fz
                                  .OnField(f => f.Name)
                                  .Value("elasticsearcc")
                                  )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{ fuzzy: { name : { value : ""elasticsearcc"" } }}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 12
0
        public void TestMoreLikeThisAllQuery()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .MoreLikeThis(fz => fz
                                         .OnFields(f => f.Name)
                                         .LikeText("elasticsearcc")
                                         .MaxQueryTerms(25)
                                         .MinDocumentFrequency(1)
                                         .MaxDocumentFrequency(2)
                                         .MinTermFrequency(1)
                                         .MaxDocumentFrequency(2)
                                         .MinWordLength(1)
                                         .MaxWordLength(2)
                                         .StopWords(new[] { "thou", "shall" })
                                         .BoostTerms(1.4)
                                         .TermMatchPercentage(12)
                                         .Boost(1.1)
                                         .Analyzer("my_analyzer")
                                         )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{ mlt: { 
				fields : [""name"" ],
				like_text : ""elasticsearcc"",
				percent_terms_to_match: 12.0,
				stop_words: [
					""thou"",
					""shall""
				],
				min_term_freq: 1,
				max_query_terms: 25,
				min_doc_freq: 1,
				max_doc_freq: 2,
				min_word_len: 1,
				max_word_len: 2,
				boost_terms: 1.4,
				boost: 1.1,
				analyzer: ""my_analyzer""
			}}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 13
0
        public void BoolQueryOverload()
        {
            var q1 = Query <ElasticsearchProject> .Term(p => p.Name, "elasticsearch.pm");

            var q2 = Query <ElasticsearchProject> .Term(p => p.Name, "elasticflume");

            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query((q1 & q2) | (q1 & q2));

            var json = TestElasticClient.Serialize(s);

            var expected = "{}";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 14
0
        public void TestFieldsWithExclusionsByProperty()
        {
            var fields = typeof(ElasticsearchProject).GetProperties()
                         .Select(x => x.Name.ToCamelCase())
                         .Except(new List <string> {
                "followers", "contributors", "nestedFollowers", "myGeoShape"
            }).ToList();

            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Fields(fields.ConvertAll(x => x.ToCamelCase()).ToArray());

            var json = TestElasticClient.Serialize(s);

            const string expected = @"{
		  from: 0,
		  size: 10,
		  fields: [
			""id"",
			""name"",
			""version"",
			""country"",
			""content"",
			""lOC"",
			""origin"",
			""startedOn"",
			""pingIP"",
			""longValue"",
			""floatValue"",
			""doubleValue"",
			""boolValue"",
			""intValues"",
			""floatValues"",
			""locScriptField"",
			""stupidIntIWantAsLong"",
			""myAttachment"",
			""myBinaryField"",
			""product"",
            ""myStringArrayField"",
			""suggest""
		  ]
		}"        ;

            Assert.True(json.JsonEquals(expected));
        }
Exemplo n.º 15
0
        public void TestSortDescending()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .SortDescending(f => f.Country);
            var json     = TestElasticClient.Serialize(s);
            var expected = @"  {
          from: 0,
          size: 10,
          sort: {
            country : ""desc""
            }          
        }";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 16
0
        public void TestUpdate()
        {
            var s = new UpdateDescriptor <ElasticSearchProject>()
                    .Script("ctx._source.counter += count")
                    .Params(p => p
                            .Add("count", 4)
                            );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"  {
	      script: ""ctx._source.counter += count"",
	      params: {
	        count: 4
	      }
	    }"    ;

            Assert.True(json.JsonEquals(expected), json);
        }
        public void TestFuzzyNumericQuery()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .FuzzyNumeric(fz => fz
                                         .OnField(f => f.LOC)
                                         .Value(200)
                                         .MinSimilarity(12)
                                         )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{ fuzzy: { loc : { min_similarity: 12.0, value : 200.0 } }}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 18
0
        public void LimitFilter()
        {
            var s = new SearchDescriptor <ElasticSearchProject>().From(0).Size(10)
                    .Filter(filter => filter
                            .Limit(100)
                            );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
						limit : { 
							value : 100
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 19
0
        public void TestBasics()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .Skip(0)
                    .Take(10)
                    .Explain()
                    .Version()
                    .MinScore(0.4);
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ 
				from: 0, size: 10,
				explain: true, 
				version: true,
				min_score: 0.4
			}"            ;

            Assert.True(json.JsonEquals(expected));
        }
Exemplo n.º 20
0
        public void CompletionSuggestDescriptorFuzzinessDoubleTest()
        {
            var completionSuggestDescriptor = new CompletionSuggestDescriptor <ElasticsearchProject>()
                                              .OnField("suggest")
                                              .Text("n")
                                              .Fuzziness(f => f.Fuzziness(0.4));

            var json = TestElasticClient.Serialize(completionSuggestDescriptor);

            var expected = @"{
                              fuzzy: {
                                fuzziness: 0.4
                              },
                              field: ""suggest""
                            }";

            Assert.IsTrue(json.JsonEquals(expected), json);
        }
Exemplo n.º 21
0
        public void TypeFilter()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Filter(f => f.Type("my_type"));

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
						type : { 
							value : ""my_type""
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 22
0
        public void IdsQuery()
        {
            var s = new SearchDescriptor <ElasticsearchProject>().From(0).Size(10)
                    .Query(filter => filter
                           .Ids(new[] { "1", "4", "100" })
                           );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				query : {
						ids : { 
							values : [""1"", ""4"", ""100""]
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
        public void ScriptedMetric()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Aggregations(a => a
                                  .ScriptedMetric("profit", sm => sm
                                                  .Language("groovy")
                                                  .InitScript("init script")
                                                  .MapScript("map script")
                                                  .CombineScript("combine script")
                                                  .ReduceScript("reduce script")
                                                  .ReduceParams(rp => rp
                                                                .Add("param1", "value1")
                                                                .Add("param2", "value2")
                                                                )
                                                  )
                                  );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"
				{ 
					from: 0, 
					size: 10, 
					aggs :  {
						""profit"" :  {
							scripted_metric : {
							lang: ""groovy"",
							init_script : ""init script"",
							map_script : ""map script"",
							combine_script: ""combine script"",
							reduce_script: ""reduce script"",
							reduce_params: {
								param1: ""value1"",
								param2: ""value2""
							}
						} 
					}
				}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
        public void TestNestedPathObject()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .Sort(sort => sort
                          .OnField(e => e.Id)
                          .NestedPath(f => f.Name)
                          );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"
                {
                  ""sort"": {
                    ""id"": {
                      ""nested_path"": ""name""
                    }
                  }
                }";

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 25
0
        public void SuffixMakesItIntoPropertyName()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q.Term(f => f.Name.Suffix("sort"), "value"));
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10,
			query: {
		  term: {
			""name.sort"": {
			  value: ""value""
			}
		  }
		}
	  }"    ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 26
0
        public void SpanNotQuery()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .SpanNot(sf => sf
                                    .Pre(1)
                                    .Post(2)
                                    .Dist(3)
                                    .Include(e => e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
                                    .Exclude(e => e.SpanTerm(f => f.Name, "elasticsearch.pm", 1.1))
                                    .Boost(2.2)
                                    )
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{
				span_not: { 
					include: { 
						span_term: { 
							name: {
								value: ""elasticsearch.pm"",
								boost: 1.1
							}
						}
					},
					exclude: { 
						span_term: { 
							name: {
								value: ""elasticsearch.pm"",
								boost: 1.1
							}
						}
					},
					boost: 2.2,
					pre: 1,
					post: 2,
					dist: 3
			}}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 27
0
        public void IdsFilterWithTypes()
        {
            var s = new SearchDescriptor <ElasticSearchProject>().From(0).Size(10)
                    .Filter(filter => filter
                            .Ids(new [] { "my_type", "my_other_type" }, new[] { "1", "4", "100" })
                            );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
						ids : { 
							type: [""my_type"", ""my_other_type""],
							values : [""1"", ""4"", ""100""]
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 28
0
        public void SpanTermQuery()
        {
            var s = new SearchDescriptor <ElasticSearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(q => q
                           .SpanTerm(f => f.Name, "elasticsearch.pm", 1.1)
                           );
            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, query : 
			{ span_term: { 
				name: {
					value: ""elasticsearch.pm"",
					boost: 1.1
				}
			}}}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
Exemplo n.º 29
0
        public void TermsQueryParams()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Query(ff => ff.Terms(f => f.Name, new [] { "elasticsearch.pm", "nest" })
                           );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				query : {
						terms: {
							""name"": [""elasticsearch.pm"", ""nest""],
						}
					}
			}"            ;

            Assert.True(json.JsonEquals(expected), json);
        }
        public void GeoShapeMultiLineStringFilter()
        {
            var s = new SearchDescriptor <ElasticsearchProject>()
                    .From(0)
                    .Size(10)
                    .Filter(f => f
                            .Cache(true)
                            .Name("my_geo_filter")
                            .GeoShapeMultiLineString(p => p.Origin, d => d
                                                     .Coordinates(new[] {
                new[] { new[] { 102.0, 2.0 }, new[] { 103.0, 2.0 }, new[] { 103.0, 3.0 }, new[] { 102.0, 3.0 } },
                new[] { new[] { 100.0, 0.0 }, new[] { 101.0, 0.0 }, new[] { 101.0, 1.0 }, new[] { 100.0, 1.0 } },
                new[] { new[] { 100.2, 0.2 }, new[] { 100.8, 0.2 }, new[] { 100.8, 0.8 }, new[] { 100.2, 0.8 } }
            }
                                                                  )
                                                     .Relation(GeoShapeRelation.Intersects)
                                                     )
                            );

            var json     = TestElasticClient.Serialize(s);
            var expected = @"{ from: 0, size: 10, 
				filter : {
					geo_shape: {
						origin: {
							shape: {
								coordinates: [
									[ [ 102.0, 2.0 ], [ 103.0, 2.0 ], [ 103.0, 3.0 ], [ 102.0, 3.0 ] ],
									[ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ] ],
									[ [ 100.2, 0.2 ], [ 100.8, 0.2 ], [ 100.8, 0.8 ], [ 100.2, 0.8 ] ]
								],
								type: ""multilinestring""
							},
							relation: ""intersects""
						},
						_cache: true,
						_name: ""my_geo_filter""
					}
				}
			}"            ;

            Assert.IsTrue(json.JsonEquals(expected), json);
        }