Elasticsearch NEST – Examples for mapping between Query and C#

During my training with Elasticsearch I would like to map a query with GET/POST method to C# syntax of NEST. It’s very helpful for me to see how NEST composes its internal queries and sends to Elasticsearch server. I just post my sample project here, maybe it’ll help you too.

1. Indexing Employee Documents

PUT /megacorp/employee/1
{
  "about": "I love to go rock climbing",
  "age": 25,
  "firstName": "John",
  "id": 1,
  "interests": [
    "sports",
    "music"
  ],
  "lastName": "Smith"
}
Employee employee = new Employee()
{
    Id = 1,
    FirstName = "John",
    LastName = "Smith",
    Age = 25,
    About = "I love to go rock climbing",
    Interests = new List<string>() { "sports", "music" }
};
await client.IndexAsync(employee);

2. Retrieving a Document

GET /megacorp/employee/1
{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 9,
  "found" : true,
  "_source" : {
    "about" : "I love to go rock climbing",
    "age" : 25,
    "firstName" : "John",
    "id" : 1,
    "interests" : [ "sports", "music" ],
    "lastName" : "Smith"
  }
}
public async Task<T> GetById(int id)
{
    var response = await client.GetAsync<T>(new DocumentPath<T>(id), g => g.Index(defaultIndex).Type(defaultType));
    return response.Source;
}

3. Search Lite

We will search for all employees, with this request

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{}
public async Task<IEnumerable<T>> SearchAsync()
{
    var response = await client.SearchAsync<T>(s => s.Type(defaultType));
    return response.Documents;
}

4. Search with Query DSL

We can represent the search for all Smiths like so

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
    "query": {
    "match": {
        "lastName": {
            "query": "Smith"
        }
    }
    }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q => q.Match(m => m.Field(f => f.LastName).Query("Smith"))));
    Result = result.Documents;
}

5. Search with Query String

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "query": {
    "query_string": {
      "query": "Smith",
      "fields": [
        "lastName"
      ]
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q => q.QueryString(qs =>
                    qs.Fields(f => f.Field(fi => fi.LastName))
                        .Query("Smith"))));
    Result = result.Documents;
}

6. More-Complicated Searches

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "lastName": {
              "query": "smith"
            }
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gt": 30.0
            }
          }
        }
      ]
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q => q.Bool(b =>
                    b.Filter(f =>
                        f.Range(r =>
                            r.Field(fi => fi.Age).GreaterThan(30)))
                   .Must(m =>
                        m.Match(ma =>
                            ma.Field(fie => fie.LastName).Query("smith")))
                    )));
    Result = result.Documents;
}

7. Full-Text Search

We are going to search for all employees who enjoy “rock climbing”, “rock” or “climbing”.

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "query": {
    "match": {
      "about": {
        "query": "rock climbing"
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q =>
                    q.Match(m =>
                        m.Field(f => f.About).Query("rock climbing"))
                   ));
    Result = result.Documents;
}

8. Phrase Search

Finding individual words in a field is all well and good, but sometimes you want to
match exact sequences of words or phrases. For instance, we could perform a query
that will match only employee records that contain both “rock” and “climbing” and
that display the words are next to each other in the phrase “rock climbing.”

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "query": {
    "match": {
      "about": {
        "type": "phrase",
        "query": "rock climbing"
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q =>
                    q.MatchPhrase(m =>
                        m.Field(f => f.About).Query("rock climbing"))
                   ));
    Result = result.Documents;
}

9. Highlighting Our Searches

Many applications like to highlight snippets of text from each search result so the user
can see why the document matched the query

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "highlight": {
    "fields": {
      "about": {}
    }
  },
  "query": {
    "match": {
      "about": {
        "type": "phrase",
        "query": "rock climbing"
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q =>
                    q.MatchPhrase(m =>
                        m.Field(f => f.About).Query("rock climbing")))
                .Highlight(h => h.Fields(fi => fi.Field(fie => fie.About)))
        );
    Result = result.Hits;
}

10. Analytics

Let’s find the most popular interests enjoyed by our employees

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Aggregations(a =>
                    a.Terms("all_interests", t =>
                        t.Field(f => f.Interests)))
        );
    Result = result.Aggregations;
}

11. Analytics limit

If we want to know the popular interests of people called Smith, we can just add the appropriate query into the mix:

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  },
  "query": {
    "match": {
      "lastName": {
        "query": "smith"
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Query(q =>
                    q.Match(m =>
                        m.Field(fi =>
                            fi.LastName).Query("smith")))
                .Aggregations(a =>
                    a.Terms("all_interests", t =>
                        t.Field(f => f.Interests)))
        );
    Result = result.Aggregations;
}

12. Analytics with Average

POST http://localhost:9200/megacorp/employee/_search?pretty=true
{
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      },
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
public async Task ExecuteAsync()
{
    var result = await client.SearchAsync<Employee>(
        s =>
            s.Type(typeEmployee)
                .Aggregations(a =>
                    a.Terms("all_interests", t =>
                        t.Field(f =>
                            f.Interests)
                        .Aggregations(ag =>
                            ag.Average("avg_age", av =>
                                av.Field(fi => fi.Age)))
                    )
                )
 
        );
    Result = result.Aggregations;
}

13. Retrieving Part of a Document

GET http://localhost:9200/website/blog/123?pretty=true&fields=title%2Ctext
public async Task ExecuteAsync()
{
    var response =
        await client.GetAsync(new DocumentPath<Blog>(123), g =>
                    g.Fields(f => f.Title, f => f.Text));
    Result = response.Fields;
}

14. Updating a Whole Document

PUT http://localhost:9200/website/blog/123?pretty=true
{
  "date": "2014-01-01T00:00:00",
  "id": 123,
  "text": "I am staring to get the hang of this...",
  "title": "My first blog entry"
}
public async Task ExecuteAsync()
{
    var blog = new Blog()
    {
        Id = 123,
        Title = "My first blog entry",
        Text = "I am staring to get the hang of this...",
        Date = new DateTime(2014, 1, 1)
    };
    Result = await client.UpdateAsync(new DocumentPath<Blog>(123), u => u.Doc(blog));
}

15. Partial Updates to Documents

POST http://localhost:9200/website/blog/1/_update?pretty=true
{
  "doc": {
    "tags": [
      "testing"
    ],
    "views": 0
  }
}
public async Task ExecuteAsync()
{
    var blog = new Blog()
    {
        Id = 1,
        Title = "My first blog entry",
        Text = "Just trying this out..."
    };
    await client.IndexAsync(blog);
 
    dynamic dyn = new ExpandoObject();
    dyn.Tags = new List<string>() { "testing" };
    dyn.Views = 0;
 
    Result = await client.UpdateAsync<Blog, dynamic>(new DocumentPath<Blog>(1), u =>
         u.Doc(dyn));
}

16. Deleting a Document

DELETE http://localhost:9200/website/blog/123?pretty=true
public async Task ExecuteAsync()
{
    Result = await client.DeleteAsync(new DocumentPath<Blog>(123));
}

17. Cluster Health

GET http://localhost:9200/_cluster/health?pretty=true
public async Task ExecuteAsync()
{
    Result = await client.ClusterHealthAsync();
}

18. Add an Index

PUT http://localhost:9200/blogs?pretty=true
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 3
  }
}
public async Task ExecuteAsync()
{
    Result = await client.CreateIndexAsync("blogs", c =>
                 c.Settings(s =>
                     s.NumberOfShards(3)
                     .NumberOfReplicas(1)));
}

19. Retrieving Part of a Document

GET http://localhost:9200/website/blog/123?pretty=true&_source_include=title%2Ctext
public async Task ExecuteAsync()
{
    Result =
        await client.GetAsync<Blog>(new DocumentPath<Blog>(123), g =>
             g.SourceInclude("title", "text"));
}

20. Using Versions from an External System

PUT http://localhost:9200/website/blog/2?pretty=true&version=25&version_type=external
{
  "date": "0001-01-01T00:00:00",
  "id": 0,
  "text": "Starting to get the hang of this...",
  "title": "My first external blog entry",
  "views": 0
}
public async Task ExecuteAsync()
{
    var getResponse = (await client.GetAsync<Blog>(new DocumentPath<Blog>(2)));
    var blog = getResponse.Source;
    long version;
    if (blog == null)
    {
        blog = new Blog()
        {
            Title = "My first external blog entry",
            Text = "Starting to get the hang of this..."
        };
        var result = await client.IndexAsync(blog, i => i.Id(2));
        version = result.Version;
    }
    else
        version = getResponse.Version;
 
    Result = await client.IndexAsync(blog, i => i.Id(2).Version(version + 5).VersionType(VersionType.External));
}

21. Using Scripts to Make Partial Updates

POST http://localhost:9200/website/blog/1/_update?pretty=true
{
  "script": "ctx._source.views += 1"
}
public async Task ExecuteAsync()
{
    Result = await client.UpdateAsync(new DocumentPath<Blog>(1), u =>
        u.Script("ctx._source.views += 1"));
}
POST http://localhost:9200/website/blog/1/_update?pretty=true
{
  "script": "ctx._source.tags+=new_tag",
  "params": {
    "new_tag": "search"
  }
}
public async Task ExecuteAsync()
{
    Result = await client.UpdateAsync(new DocumentPath<Blog>(1), u =>
        u.Script("ctx._source.tags+=new_tag")
        .Params(p => p.Add("new_tag", "search")));
}
POST http://localhost:9200/website/blog/1/_update?pretty=true
{
  "script": "ctx.op = ctx._source.views == count ? 'delete' : 'none'",
  "params": {
    "count": "1"
  }
}
Result = await client.UpdateAsync(new DocumentPath<Blog>(1), u =>
                u.Script("ctx.op = ctx._source.views == count ? 'delete' : 'none'")
                .Params(p => p.Add("count", "1")));

22. Updating a Document That May Not Yet Exist

Imagine that we need to store a page view counter in Elasticsearch. Every time that a user views a page, we increment the counter for that page. But if it is a new page, we can’t be sure that the counter already exists. If we try to update a nonexistent document, the update will fail.
In cases like these, we can use the upsert parameter to specify the document that should be created if it doesn’t already exist

POST http://localhost:9200/website/pageviews/1/_update?pretty=true
{
  "script": "ctx._source.views+=1",
  "upsert": {
    "id": 0,
    "views": 1
  }
}
public async Task ExecuteAsync()
{
    Result = await client.UpdateAsync(new DocumentPath<PageViews>(1), u =>
        u.Script("ctx._source.views+=1")
        .Upsert(new PageViews() { Views = 1 }));
}

23. Updates and Conflicts

POST http://localhost:9200/website/pageviews/1/_update?pretty=true&retry_on_conflict=5
{
  "script": "ctx._source.views+=1",
  "upsert": {
    "id": 1,
    "views": 0
  }
}
Result = await client.UpdateAsync(new DocumentPath<PageViews>(1), u =>
                u.Script("ctx._source.views+=1")
                    .Upsert(new PageViews { Id = 1, Views = 0 })
                    .RetryOnConflict(5));

24. Retrieving Multiple Documents

POST http://localhost:9200/_mget?pretty=true
{
  "docs": [
    {
      "_index": "website",
      "_type": "blog",
      "_id": 2
    },
    {
      "_index": "website",
      "_type": "pageviews",
      "_id": 1
    }
  ]
}
public async Task ExecuteAsync()
{
    Result = await client.MultiGetAsync(m =>
        m.Get<Blog>(g =>
            g.Id(2))
        .Get<PageViews>(ge =>
            ge.Id(1))
        );
}
POST http://localhost:9200/website/blog/_mget?pretty=true
{
  "ids": [
    2,
    1
  ]
}
 
 
 
Result = await client.MultiGetAsync(m =>
    m.Index(indexWebsite)
    .Type(typeBlog)
    .GetMany<Blog>(new long[] { 2, 1 }));

100. Source code

Source code: https://bitbucket.org/hintdesk/dotnet-elasticsearch-nest-examples-for-mapping-between-query

Elasticsearch NEST – Examples for mapping between Query and C#的更多相关文章

  1. ElasticSearch NEST笔记

    ElasticSearch NEST笔记 1. 什么是ElasticSearch? ElasticSearch is a powerful open source search and analyti ...

  2. Creating a custom analyzer in ElasticSearch Nest client

     Creating a custom analyzer in ElasticSearch Nest client Question: Im very very new to elasticsearch ...

  3. elasticsearch index 之 put mapping

    elasticsearch index 之 put mapping   mapping机制使得elasticsearch索引数据变的更加灵活,近乎于no schema.mapping可以在建立索引时设 ...

  4. Elasticsearch(八)【NEST高级客户端--Mapping映射】

    要使用NEST与Elasticsearch进行交互,我们需要能够将我们的解决方案中的POCO类型映射到存储在Elasticsearch中的反向索引中的JSON文档和字段.本节介绍NEST中可用的所有不 ...

  5. Elasticsearch NEST 控制字段名称命名格式

    在使用NEST操作elasticsearch时,字段名会根据model中字段,默认为首字母小写. 如果需要调整NEST的默认明个规则,可以在 ConnectionSettings中进行自定义. var ...

  6. Elasticsearch NEST使用指南:映射和分析

    NEST提供了多种映射方法,这里介绍下通过Attribute自定义映射. 一.简单实现 1.定义业务需要的POCO,并指定需要的Attribute [ElasticsearchType(Name = ...

  7. ElasticSearch基础之映射mapping

    [01]什么是mapping? 首先去看看官方文档,非常重要:https://www.elastic.co/guide/en/elasticsearch/reference/current/mappi ...

  8. elasticsearch入门使用(三) Query DSL

    Elasticsearch Reference [6.2] » Query DSL 参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/refe ...

  9. Elasticsearch学习系列之mapping映射

    什么是映射 为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成全文本(Full-text)或精确(Exact-value)的字符串值,Elasticsearch需要知道每个字段里面 ...

随机推荐

  1. 温(Xue)习排序算法

    最近忙着找工作,虽然排序算法用得到的情况不多,但不熟悉的话心里始终还是感觉没底. 于是今天给温习了其中的四个排序算法(与其说是温习,不如说是学习...因为感觉自己好像从来木有掌握过它们...) 一.选 ...

  2. JVM配置参数

    .堆内存相关的JVM参数 —Xms 初始堆大小 —Xmx 最大堆大小 —Xss 线程栈大小 —XX:MinHeapFreeRatio 设置堆空间最小空闲比例 —XX:MaxHeapFreeRatio ...

  3. YUI前端优化之Server篇

    二.网站Server 篇:使用内容分发网络为文件头指定Expires或Cache-ControlGzip压缩文件内容配置ETag尽早刷新输出缓冲使用GET来完成AJAX请求 11.使用内容分发网络 用 ...

  4. [模板]LIS(最长上升子序列)

    转载自:最长上升子序列(LIS)长度的O(nlogn)算法 最长上升子序列nlogn算法 在川大oj上遇到一道题无法用n^2过于是,各种纠结,最后习得nlogn的算法 最长递增子序列,Longest ...

  5. button作用类似于submit

    不想提交,可使用以下 <a href="javascript:;" >修改</a>

  6. 添加普通用户为sudo用户

    https://www.linuxidc.com/Linux/2017-01/139361.htm 1.打开sudoers文件 切换到root用户下,cd root,运行visudo命令,visudo ...

  7. MyBatis 实用篇(一)入门

    MyBatis 实用篇(一)入门 MyBatis(http://www.mybatis.org/mybatis-3/zh/index.html) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程 ...

  8. SGU 194 Reactor Cooling (有容量和下界的可行流)

    题意:给定上一个有容量和下界的网络,让你求出一组可行解. 析:先建立一个超级源点 s 和汇点 t ,然后在输入时记录到每个结点的下界的和,建边的时候就建立c - b的最后再建立 s 和 t , 在建立 ...

  9. pyhon 去除列表中重复元素

    Python set() 函数 描述 set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集.差集.并集等. 语法 set 语法: class set([iterabl ...

  10. ubuntu16.04 安装jdk 错误解决

    错误 $ apt-get install openjdk-9-jdk Errors were encountered while processing: /var/cache/apt/archives ...