Set Elasticsearch timestamp path on an index using NEST? - elasticsearch

I need to add a timestamp path to my index using NEST, not sure how to make this happen:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-timestamp-field.html
I have been messing with NEST but I cannot figure this out.
I have been reading the docs here but have not found what i am looking for:
http://nest.azurewebsites.net/nest/quick-start.html

Using the fluent API, this can be done when creating your index:
var response = client.CreateIndex("myindex", c => c
.AddMapping<MyType>(m => m
.MapFromAttributes()
.TimestampField(t => t
.SetDisabled(false)
.SetPath(o => o.MyTimestampField)
)
)
);
Or updating an existing index:
var response = client.Map<MyType>(m => m
.TimestampField(t => t
.SetDisabled(false)
.SetPath(o => o.MyTimestampField)
)
);
Hope that helps.

Related

Using Elasticsearch Nest 7.x to query 5.x index

I have a project using Nest 7.x and there is a query I need to make to an older 5.x elasticsearch index. When I make a call like this, I get the following error. I am guessing it is due to how the mapping types were changed in version 6 and greater. Is there any way around this to query an older index?
var result = _elasticClient.GetAsync<Category>(id)
Invalid NEST response built from a successful (404) low level call on
GET: /myindex/_doc/15437
Request: <Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to
force it to be set on the response.>
Response: {"_index":"2020-01-13","_type":"_doc","_id":"15437","found":false}
As a workaround, I did this and it appears to work. Not sure if there are any better solutions?
var response = _elasticClient.SearchAsync<Category>(s => s
.Query(q => q
.Bool(b => b
.Must(
bs => bs.Term(p => p.Id, id),
bs => bs.Term(p => p.Field("_type").Value("category"))
)
)
)
)

ElasticSearch NEST multi match returns all result

I have this C# code which is expected to match 2 fields using multi-match Elastic Search type. I am using NEST package.
var response = await _elasticClient.SearchAsync<FileDocument>(
s => s.Query(q => q.MultiMatch(c => c
.Fields(f => f.Field(p => p.FileName).Field(query))
.Fields(f => f.Field(p => p.Metadata).Field(query))
)));
Problem is no matter what text I passed in, it returns all the result. Anything I miss out?
In order to efficiently debug these types of issue, you need to inspect the HTTP request going to Elasticsearch, ultimately your query builder will be converted to search JSON and will be executed against Elasticsearch.
I am not aware of nest but have written the answer for Java code, which prints the Elasticsearch query in JSON format.
Although my guess is that, you are not sending the correct HTTP method which should be POST and you might be sending it with GET, which is causing ES to ignore your search query and return all documents.
Solved after adding .Query(query)
var response = await _elasticClient.SearchAsync<FileDocument>(
s => s.Query(q => q.MultiMatch(c => c
.Fields(f => f.Field(p => p.FileName).Field(p=>p.Metadata))
.Query(query)
))
);
Reference - https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/multi-match-usage.html

How to : ElasticSearch .NET and NEST 5.X Multimatch with wildcard

I has been search a lot of sample from Internet, however i still could not find any sample on Wildcard Search with more than one fields, can anyone help me with some example? Im very new into ElasticSearch. Below is what im trying to do with wildcard, but it work for one field.
How can i combine below Wildcard with MultiMatch in C#?
var result = client.Search<Metadata>(x => x
.Index("indexname")
.Type("Metadata")
.MatchAll()
.Query(q => q
.Wildcard(c => c
.Name("Query")
.Boost(1.1)
.Field(p => p.Title)
.Value("input*")
.Rewrite(MultiTermQueryRewrite.TopTermsBoost(10))
)
)
);
How can i add in below multi fields support like in Multimatch?
.Fields(f => f
.Fields(f1 => f1.Title, f2 => f2.Keywords)
)

NEST MultiGet search all types possible?

I have got unique document ids (across all types) and I would like to check which document already exists in elasticsearch index. I try to search
var duplicateCheck = _elasticClient
.MultiGet(m => m.GetMany<object>(notices.Select(s => s.Id)).Fields("Id"));
but it returns wrong result - every document has set found property to false.
update
there is workaround here
var exisitngDocIds = _elasticClient.Search<ZPBase>(s => s
.AllTypes()
.Query(q => q.Ids(notices.Select(z=>z.Id)))
.Fields("Id")
.Take(notices.Count)
);
notices = notices.Where(q => !exisitngDocIds.Hits.Any(s => s.Id == q.Id)).ToList();
From the Multi Get API documentation I realized that you can use something similar to the following code to solve your problem:
var response = _elasticClient.MultiGet(m => m
.Index(MyIndex)
.Type("")
.GetMany<ZPBase>(noticeIds));
Note the empty string passed as the Type.

how do i set similarity in nest for elasticsearch on a per field basis

i have not been able to 'programmatically' set the similarity on a field in elasticsearch using Nest.
here's an example of how i set up my index. it's within the multifield mapping where i'd like to set the similarity so i can experiment with things like BM25 similarity...
(see the props > multifield section below)...
var createInd = client.CreateIndex("myindex", i =>
{
i
.Analysis(a => a.Analyzers(an => an
.Add("nameAnalyzer", nameAnalyzer)
)
.AddMapping<SearchData>(m => m
.MapFromAttributes()
.Properties(props =>
{
props
.MultiField(mf => mf
//title
.Name(s => s.Title)
.Fields(f => f
.String(s => s.Name(o => o.Title).Analyzer("nameAnalyzer"))
.String(s => s.Name(o => o.Title.Suffix("raw")).Index(FieldIndexOption.not_analyzed))
)
);
...
It was just recently made possible with this commit to set the similarity on a string field. You can now do this:
.String(s => s.Name(o => o.Title).Similarity("my_similarity")
This is assumming you already have the similarity added to your index. NEST is lacking a bit of flexibility at the moment for actually configuring similarities. Right now you have to use the CustomSimilaritySettings class. For example:
var bm25 = new CustomSimilaritySettings("my_similarity", "BM25");
bm25.SimilarityParameters.Add("k1", "2.0");
bm25.SimilarityParameters.Add("b", "0.75");
var settings = new IndexSettings();
settings.Similarity = new SimilaritySettings();
settings.Similarity.CustomSimilarities.Add(bm25);
client.CreateIndex("myindex", c => c.InitializeUsing(settings));
It would be nice to be able to do this via the fluent API when creating an index. I am considering sending a PR for this before the 1.0RC release.

Resources