# Visual Basic Examples for ElasticSearch.net and NEST

**URL:** <https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 3, 2020, 2:07am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304 "2020-04-03T02:07:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 3, 2020, 2:07am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/1 "2020-04-03T02:07:24Z")

</div>

Hi. I am trying to update a Visual Basic project to use the new [Elasticsearch.Net](http://Elasticsearch.Net) and NEST api versions. Unfortunately whenever I try to find examples all I can find is c#.

Can anyone show me some examples of the following calls in Visual Basic?:

Indicies.Create  
Ingest.Putpipeline  
Search

Below are examples of the syntax issues I am having:

#1 - Indicies.Create - method Mappings is obsolete as of version 7.x

```auto
            Dim result = client.Indices.Create(index_name,
                                                Function(c) c.Mappings(Function(ms) ms.Map(Of IndexFile)(Function(m) m.AutoMap().AllField(Function(al) al.Enabled(False)).Properties(Function(ps) ps.Object(Of Attachment)(Function(a) a.Name(Function(n) n.Attachment).AutoMap())))
                                                                           ).Settings(Function(a) a.Analysis(Function(aa) aa.Analyzers(Function(am) am.Custom("filename_analyzier", Function(ca) ca.Filters("standard", "lowercase", "stop").Tokenizer("whitespace"))))))

```

#2 - Ingest.PutPipline - no version of PutPipeline found matching the parameters I am passing

```auto
            Return Me.client.Ingest.PutPipeline("attachments",
                    Function(p) p.Description("Document Attacment pipeline").Processors(
                        Function(pr) pr.Attachment(Of IndexFile)(
                            Function(a) a.Field(
                                Function(f) f.FileContent).TargetField(
                                    Function(f) f.Attachment)).Remove(Of IndexFile)(Function(r) r.Field(Function(f) f.FileContent)))).IsValid

```

#3 - Search - method Mappings is obsolete as of version 7.x

```auto
            Dim queryResult = Me.client.Search(Of IndexFile)(Function(d) d.Index(indexName).AllTypes().Query(Function(q) q.Match(Function(m) m.Field(Function(a) a.Attachment.Content).Query(queryTerm))).From(0).Size(1000))

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 13, 2020, 8:15am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/2 "2020-04-13T08:15:00Z")

</div>

> [@kvickery](#):
>
> Indicies.Create - method Mappings is obsolete as of version 7.x

Remove the `.Mappings(..)` call and use `.Map(Of IndexFile)` on `c` i.e.

```vb
Dim result = client.Indices.Create(index_name,
		Function(c) c.Map(Of IndexFile)(Function(m)

```

> [@kvickery](#):
>
> #2 - Ingest.PutPipline - no version of PutPipeline found matching the parameters I am passing

`Field` on `RemoveProcessor` takes a `Fields` or a `Func<FieldsDescriptor, IPromise<Fields>>`. Assuming `FileContent` is a String property, something like

```vb
	Dim result = client.Ingest.PutPipeline("attachments",
					Function(p) p.
						Description("Document Attacment pipeline").
						Processors(
							Function(pr) pr.
								Attachment(Of IndexFile)(
									Function(a) a.Field(
										Function(f) f.FileContent).TargetField(
											Function(f) f.Attachment)).
								Remove(Of IndexFile)(Function(r) r.Field(Function(f) f.Field(Of String)(Function (ff) ff.FileContent)))))

```

> [@kvickery](#):
>
> #3 - Search - method Mappings is obsolete as of version 7.x

I think you mean `AllTypes()` is obsolete? Type mappings are obsolete and will be removed in a future major version. As such, the 7.x client does not have an `AllTypes()` method. If you remove this, it should be fine

```vb
Dim indexName As String = "my_index"
Dim queryTerm As String = "query"

Dim queryResult = client.Search(Of IndexFile)(Function(d) d.Index(indexName).Query(Function(q) q.Match(Function(m) m.Field(Function(a) a.Attachment.Content).Query(queryTerm))).From(0).Size(1000))

```

You may want to look at a [C# to Visual Basic converter](https://converter.telerik.com/); there are [quite a few examples in C# in the documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html), on here and [stackoverflow](https://stackoverflow.com/questions/tagged/nest), which may be helpful.

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 14, 2020, 4:42pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/3 "2020-04-14T16:42:26Z")

</div>

Thanks for the help Russ. I have made progress. The code converter you suggested is very useful.

I'm hoping you can help me out with a couple more api usages I have not been able to figure out yet.

First, in the Indices.Create call below I am getting a warning saying AllField is obsolete. How can I remove the AllField but still have the search work the same?

```auto
            Dim result = client.Indices.Create(index_name,
                                                Function(ms) ms.Map(Of IndexFile)(
                                                    Function(m) m.AutoMap().AllField(
                                                        Function(al) al.Enabled(False)).Properties(
                                                            Function(ps) ps.Object(Of Attachment)(
                                                                Function(a) a.Name(
                                                                    Function(n) n.Attachment).AutoMap()))
                                        ).Settings(
                                            Function(a) a.Analysis(
                                                Function(aa) aa.Analyzers(
                                                    Function(am) am.Custom("filename_analyzier",
                                                        Function(ca) ca.Filters("standard", "lowercase", "stop").Tokenizer("whitespace"))))))

```

Second, I have a search call with a function that builds the query. On the following line of code I get the "overload resolution failed" error unless I remove the ".Operator([Operator].And)", though this works fine with the Match method. Why can't I use the Operator suffix with MatchPhrase?

```auto
query = query And q.MatchPhrase(
    Function(m) m.Field("attachment.content").Query(keyword_str).Operator([Operator].And))

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 15, 2020, 12:58am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/4 "2020-04-15T00:58:00Z")

</div>

> [@kvickery](#):
>
> ```vb
> .AllField( Function(al) al.Enabled(False))
> 
> ```

You can remove this. [The `_all` field has been removed in 6.0.0+](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/mapping-all-field.html) and the replacement for it is to use `copy_to` on fields that you wish to search across, to copy values into an _`_all`_-like field that you define. Since your code is disabling the `_all` field however, we can simply remove this, as there is no `_all` field to disable anymore 😃

> [@kvickery](#):
>
> .Operator([Operator].And)

You can remove this. This operator isn't valid for a `match_phrase` query, which is looking for the query input as a phrase within documents.

You may have been setting it in an earlier version of NEST [such as 5.x](https://github.com/elastic/elasticsearch-net/blob/80e9b969d3fbb51039bc61c02ba05c31334135f6/src/Nest/QueryDsl/FullText/Match/MatchPhraseQuery.cs), because of the hierarchy of query types in the client allowing it. It would have simply been ignored by Elasticsearch though. In NEST 7.x, the match family of queries were refactored in 6.x client, to match a change in query DSL structure in Elasticsearch.

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 15, 2020, 1:56am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/5 "2020-04-15T01:56:37Z")

</div>

Thanks for your help again.

For the **match\_phrase** query, it was being used in conjunction with other **match** and **match\_phrase** queries, so my understanding was the And Operator was needed to build the query properly.

For example, a search might involve a combination of the term "swamp" and "pacific chorus frog", which would result in a **match** query combined with a **match\_phrase** query. I will make sure the search combination works during my testing.

Thanks.

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 15, 2020, 2:11am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/6 "2020-04-15T02:11:15Z")

</div>

I just realized I was probably not understanding the intent of the **Operator** method. I though it was relating the different query functions as they got combined, but I see now it probably dictates to the **Match** function if for a set of terms if just one term needs to be found or all.

So yes, it makes sense the **MatchPattern** function would have no use for the **Operator** method.

Sorry - I have never worked with Elasticsearch before and am having to learning quickly to upgrade an application to use the newer version of the server.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 15, 2020, 11:40am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/7 "2020-04-15T11:40:20Z")

</div>

> [@kvickery](#):
>
> Sorry - I have never worked with Elasticsearch before and am having to learning quickly to upgrade an application to use the newer version of the server.

No worries, happy learning! 🎉

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 15, 2020, 6:52pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/8 "2020-04-15T18:52:27Z")

</div>

Hi again Russ. One last question (I hope).

My call to **Ingest.PutPipelineRequest** is failing with "**No processor type exists with name [attachment]**".

It sounds like I need to install the **ingest-attachment** plugin. I saw a post saying to use the command below for Windows but the suggested bat file is not present in my OpenDistro installation - maybe old instructions. Do you know how I can install or enable the plugin? I thought the OpenDistro package included plugins.

```auto
elasticsearch-plugin.bat install ingest-attachment

```

This is how I am starting Elasticsearch, under Windows 10:

```auto
docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" amazon/opendistro-for-elasticsearch:1.4.0

```

Here is my Indices.Create call:

```auto
            Dim result = client.Indices.Create(index_name,
                                                Function(ms) ms.Map(Of IndexFile)(
                                                    Function(m) m.AutoMap().AllField(
                                                        Function(al) al.Enabled(False)).Properties(
                                                            Function(ps) ps.Object(Of Attachment)(
                                                                Function(a) a.Name(
                                                                    Function(n) n.Attachment).AutoMap()))
                                        ).Settings(
                                            Function(a) a.Analysis(
                                                Function(aa) aa.Analyzers(
                                                    Function(am) am.Custom("filename_analyzier",
                                                        Function(ca) ca.Filters("standard", "lowercase", "stop").Tokenizer("whitespace"))))))

```

And here is my Ingest.PutPipelineRequest call:

```auto
            Dim putPipelineResponse = client.Ingest.PutPipeline("attachments",
                                                Function(p) p.Description("Document Attachment Pipeline").Processors(
                                                    Function(pr) pr.Attachment(Of IndexFile)(
                                                        Function(a) a.Field(Function(f) f.FileContent).TargetField(
                                                            Function(f) f.Attachment)).Remove(Of IndexFile)(
                                                                Function(r) r.Field(
                                                                    Function(f) f.Field(Of String)(
                                                                        Function(ff) ff.FileContent)))))

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 16, 2020, 1:00am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/9 "2020-04-16T01:00:38Z")

</div>

You'll need to install the ingest-attachment plugin. I'm not familiar enough with the OpenDistro fork to help here. You're right, that normally, one would call `elasticsearch-plugin.bat` to install the plugin. You may want to ask on the Amazon forums about this 👍

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 16, 2020, 1:05am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/10 "2020-04-16T01:05:45Z")

</div>

Update: I did some research and finally found what I think are the rights steps, though the outcome is not a success.

I created a **Dockerfile** as:  
` `  
`FROM elasticsearch:6.6.2 RUN bin/elasticsearch-plugin install --batch ingest-attachment`  
` `

and then performed:  
` `  
`docker build -t elasticsearch-wao .`  
` `  
There was a warning from the build saying " **WARNING: plugin requires additional permissions**".

Then I created a new bat file to launch my build as:  
` `  
`docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" elasticsearch-wao:latest`  
` `

I don't see any errors during startup but I don't see the final "node initialized" message either. " **docker ps**" show the server as up, but " **curl**" commands fail with the message:  
` `  
`curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid.`  
` `  
And communication with my client application is failing now (" **handshake failed due to an unexpected packet format**".

I feel like I am almost there but I am still missing a step (or two).

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 16, 2020, 1:21am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/11 "2020-04-16T01:21:32Z")

</div>

I created a docker image and ran it using your steps. I was able to curl Elasticsearch without problems

```auto
$ curl http://localhost:9200
{
  "name" : "Yl39C87",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "kXDQhIcUQsOxgo50gW8_2w",
  "version" : {
    "number" : "6.6.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "3bd3e59",
    "build_date" : "2019-03-06T15:16:26.864148Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

```

The error message implies some issue in establishing a security context. What curl command are you running?

An aside, if you're using 6.6.2 Elasticsearch, you'll want to [use the latest 6.x version of the .NET client, currently 6.8.5](https://www.nuget.org/packages/NEST/6.8.5).

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 16, 2020, 1:41am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/12 "2020-04-16T01:41:24Z")

</div>

Below is a screen shot showing my curl command results. Let me know if you have trouble reading it.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/d/5d8fa4366add92333d195b83adc80c6f6c60002f.png)

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 16, 2020, 2:03am UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/13 "2020-04-16T02:03:00Z")

</div>

The 6.6.2 docker image does not have basic authentication or TLS enabled. These features are available for free in

- 6.8.0+ (and less than 7.0.0)
- 7.1.0+

[Check out the blog post for more details](https://www.elastic.co/blog/security-for-elasticsearch-is-now-free).

There's an example of configuring a cluster with Docker Compose in the docs, [with TLS and configuring a bootstrap password](https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-tls-docker.html)

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 17, 2020, 5:32pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/14 "2020-04-17T17:32:09Z")

</div>

Hi Russ.

I thought I would give you an update. Silly mistake on my part that. When I setup my **Dockerfile** to create my custom build with the plugin I created my build from elasticsearch:7.6.1 - because OpenDistro 1.4.0 relates to elasticsearch 7.6.1. Made sense at the time.  
` `  
`FROM elasticsearch:7.6.1 RUN bin/elasticsearch-plugin install --batch ingest-attachment`  
` `  
But I should have been basing my build on the **OpenDistro** build that I had already been working with.  
` `  
`FROM amazon/opendistro-for-elasticsearch:1.4.0 RUN bin/elasticsearch-plugin install --batch ingest-attachment`  
` `  
This build starts up properly (node initialized), responds to my **curl** commands, and best of all the **Ingest.PutPipelineRequest** call works now.

Thanks again for your guidance.

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 17, 2020, 10:30pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/15 "2020-04-17T22:30:48Z")

</div>

Hi again.

All of my API calls are working now. The only problem I seem to have left is when I stop my Elasticsearch container (via docker stop) and start it up again the repository (indexes, etc.) are gone. Why are my created indexes etc. not being saved?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 17, 2020, 11:00pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/16 "2020-04-17T23:00:33Z")

</div>

Hey @kvickery, this is probably better asked as a new discussion topic, as it's about Docker and not .NET client with Visual Basic.

I suspect that the issue is `path.data` used by Elasticsearch to persist data is not pointing to a persistent location e.g. [a docker volume](https://docs.docker.com/storage/volumes/), but to [the writable container layer which doesn't persist when the container no longer exists.](https://docs.docker.com/storage/).

See [the documentation for an example of setting up a volume with Docker compose](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html).

---

<div class="post-metadata">

**Author:** ![kvickery](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@kvickery](https://discuss.elastic.co/u/kvickery)\
**Post date:** [April 17, 2020, 11:56pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/17 "2020-04-17T23:56:41Z")

</div>

Thanks for pointing me in the right direction. I just needed to use docker-compose so I could define persistent volume.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 15, 2020, 11:56pm UTC](https://discuss.elastic.co/t/visual-basic-examples-for-elasticsearch-net-and-nest/226304/18 "2020-05-15T23:56:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
