Extend Elasticsearch client to simplify ES queries (. NET Core/Framework)

The ElasticSearch. net client provides two ways to perform query operations

Constructing queries based on objects

New IdsQuery {Name = "named_query", Boost = 1.1, Values = new List<Id> {1, 2, 3, 4},}

Queries based on lambda syntax

Q. Ids (c = > c. Name (" named_query "). The Boost (1.1) Values (1, 2, 3, 4))

Both methods have their own advantages and disadvantages. The main reason is that when the conditions are a little complicated, it feels like being in a nesting doll, layer by layer, especially lambda

Such as:

var searchResults = this.Client.Search<Project>(s => s
    .Query(q => q
        .Bool(b => b
            .Should(
                bs => bs.Term(p => p.Name, "x"),
                bs => bs.Term(p => p.Name, "y")
            )
        )
    )
);

The official document has simplified method and Writing the Boolean queries | Elasticsearch.Net and NEST: the. NET clients [7] x | Elastic

However, this kind of method call is still troublesome and cannot avoid method combination

For general logical queries, the operator (D,! =,>,<) is a little bit cleaner

The following example ES version is 7.x

  1. Install the NuGet package: cr.elasticsearch
  2. using CRL;

    using CRL.Elasticsearch;

Define the data source

var builder = DBConfigRegister.GetInstance(); builder.UseElasticsearch(); builder.RegisterDBAccessBuild(dbLocation => { var dBAccessBuild= new DBAccessBuild(DBType.ES, "Http://127.0.0.1:9200/testIndex"); // define the connection setting Func< connectionSettings, ConnectionSettings> func = (setting) => { setting.DefaultMappingFor<GoodsInfo>(m => m.IndexName("testIndex")); setting.DefaultMappingFor<GoodsInfoChild>(m => m.IndexName("testIndex")); return setting; }; dBAccessBuild.Data = func; return dBAccessBuild; });

Define the query object

public class GoodsService:BaseProvider<GoodsInfo>
{
}

The initial data

public static void CreateMapping()
        {
            var service = new GoodsService();
            service.DropTable();
            service.CreateEsIndex();
            var list = new List<GoodsInfo>();
            for (int i = 1; i < 11; i++)
            {
                list.Add(new GoodsInfo { Id = i.ToString(), DataType = "Goods", Name = "goods" + i, Number = i });
            }
            service.BatchInsert(list);
            var service2 = new GoodsInfoChildService();
            var childs = new List<GoodsInfoChild>();
            childs.Add(new GoodsInfoChild("1", "100", "History") { GroupCode = "001" });
            childs.Add(new GoodsInfoChild("1", "200", "History") { GroupCode = "001" });
            service2.BatchInsert(childs);

        }

Logical query

var service = new GoodsService(); var list = service.QueryList(b => b.DataType == "Goods" && b.Id ! = "2"); Console.WriteLine($"count should 9 {list.Count == 9}");

Group the results

  public static void testAggregationCount()
    {
        var service = new GoodsService();
        var query = service.GetLambdaQuery();
        query.Where(b => b.DataType == "Goods");
        query.GroupBy(b => new { b.DataType, b.Name });
        var list = query.ToAggregationCount();
        Console.WriteLine($"count should 2 {list.Count == 2}");
    }

Specify the query method

public static void testMethodAll()
        {
            var service = new GoodsService();
            var query = service.GetLambdaQuery();
            var ids = new List<Id>();
            ids.Add("1");
            query.Where(b => b.WithIdsQuery(ids));
            query.Where(b => b.Name.WithWildcardQuery("goods*"));
            query.Where(b => b.Name.WithTermQuery("goods"));
            query.Where(b => b.Name.WithMatchQuery("goods", Operator.And));
            query.Where(b => b.Name.WithMatchPhraseQuery("goods"));
            query.Where(b => b.WithMultiMatchQuery(new string[] { "Name", "DataType" }, "goods", Operator.And, TextQueryType.BestFields));
            query.Where(b => b.WithQueryBase(new WildcardQuery
            {
                Field = "Name",
                Value = "goods"
            }));
        }

Subquery judgment

    public static void testChildQuery()
    {
        var service = new GoodsService();
        var query = service.GetLambdaQuery().Where(b => b.DataType == "Goods");
        var queryChild = query.CreateQuery<GoodsInfoChild>();
        queryChild.Where(b => b.GroupCode == "001");
        query.HasChild(queryChild);
        var list = query.ToList();
        Console.WriteLine($"count should 1 {list.Count == 1}");
    }

Source sample reference

Data/esTest · Hubroxxl /CRL – Code Cloud – Open Source China (gitee.com)