Specify default analyzer in NEST or Elasticsearch - elasticsearch

How can I specify default analyzer in NEST? Or alternative in Elasticsearch? I want change standard analyzer to language analyzer!

If you are using automap in nest you can use an attribute like so
public class A
{
[Text(Analyzer = "NameOfTheAnalyzer")]
public string Prop1 { get; set; }
}
If you want the default mapping you can set it like so
var request = new CreateIndexRequest(indexName)
{
Mappings = new Mappings()
{
["_default_"] = new TypeMapping()
{
Properties = new Properties
{
["id"] = new KeywordProperty { Index = false },
["title"] = new TextProperty { Analyzer = "NameOfTheAnalyzer" }
}
}
}
};
var create = client.CreateIndex(request);

Related

NEST how to setup completion suggestion context using annotation [duplicate]

This question already has an answer here:
ElasticSearch 5.x Context Suggester NEST .Net
(1 answer)
Closed 4 years ago.
I"m trying to setup context for my completion suggestion attribute on my POJO class however, most of the documentation online are based on configuration setting. Any ideas how I can achieve this?
[Completion]
public CompletionField Suggest { get; set; }
For indexing:
[Completion(Name = "FieldNameSuggest")]
public CompletionField Suggest
{
get
{
List<string> data = new List<string>();
data.AddRange(new List<string>() {"word1",word2"word3",... });//for suggest
return new CompletionField()
{
Input = data,
Weight = 1
};
}
}
For get suggest:
Query Making:
var sugContainer = new SuggestContainer
{
{ "completion-suggest", new SuggestBucket
{
Prefix ="word",//text for search
Completion = new CompletionSuggester
{
//Fuzzy = new FuzzySuggester
//{
// Fuzziness = Fuzziness.Auto,
// MinLength = 1,
// PrefixLength = 2,
// Transpositions = true,
// UnicodeAware = false
//},
Analyzer = "simple",
Field =new Field("FieldNameSuggest"),
Size =10, //SuggestionCount
SkipDuplicates=true,
}
}
}
};
Finaly for search:
var Result = Client.Search<T>(new SearchRequest<T>
{
Suggest = sugContainer ,
...
});

NEST elasticsearch DateRange query

I am trying to get results from elasticsearch on a predefined range of dates using NEST api but i dont manage. This example code i am trying to retrieve a week of data but it is ignoring my date Filter and returning older data than my initial date.
Thanks in advance for the help!
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);
settings.DefaultIndex("wholelog-2017.04*");
client.CreateIndex("wholelog-2017.04*",
create => create.Mappings(
mappings => mappings.Map<LogLine>(type =>
type.AutoMap()
)
));
DateTime InitDate = DateTime.Now.AddDays(-7);
var filterClauses = new List<QueryContainer>();
filterClauses.Add(new DateRangeQuery
{
Field = new Field("logTime"),
LessThanOrEqualTo = DateTime.Now,
GreaterThanOrEqualTo = InitDate
});
var searchRequest = new SearchRequest<LogLine>()
{
Size = 10000,
From = 0,
Scroll = "1m",
Query = new BoolQuery
{
Filter = filterClauses
}
};
var searchResult = client.Search<LogLine>(searchRequest);
[ElasticsearchType(Name="logLine")]
public class LogLine
{
public string Message {get;set;}
public DateTime logTime {get;set;}
public string type {get;set;}
public string logServer {get;set;}
}
logstash config file
output {
elasticsearch{
hosts => "localhost"
#user => ####
#password => ####
index => "wholelog-%{+YYYY.MM.dd}"
}
}
I assume that you didn't explicitly put your mapping and just started adding documents to your index. Try to create a new index like this
client.CreateIndex(indexName,
create => create.Mappings(
mappings => mappings.Map<LogLine>(
type => type.AutoMap()
)
)
);
And use DateTime for your field. DateMath is intended for building search queries
[ElasticsearchType(Name = "logLine")]
public class LogLine
{
public string Message { get; set; }
public DateTime logTime { get; set; }
public string type { get; set; }
public string logServer { get; set; }
}
After explicitly putting your mapping try to add docs and search against new index.

Elasticsearch - Trying to index MS Word attachment & making a full text search within

As the title is already indicating, I am trying to index MS Word documents and making a full text search within.
I have seen several examples, but I am not able to figure out what I am doing incorrectly.
Relevant Code:
[ElasticsearchType(Name = "AttachmentDocuments")]
public class Attachment
{
[String(Name = "_content")]
public string Content { get; set; }
[String(Name = "_content_type")]
public string ContentType { get; set; }
[String(Name = "_name")]
public string Name { get; set; }
public Attachment(Task<File> file)
{
Content = file.Result.FileContent;
ContentType = file.Result.FileType;
Name = file.Result.FileName;
}
}
The "Content" property above is set to "file.Result.FileContent" in the constructor. The "Content" property is a base64 string.
public class Document
{
[Number(Name = "Id")]
public int Id { get; set; }
[Attachment]
public Attachment File { get; set; }
public String Title { get; set; }
}
Below is the method for indexing documents to elasticsearch database.
public void IndexDocument(Attachment attachmentDocument)
{
// Create the index if it does not already exist
var indexExists = _client.IndexExists(new IndexExistsRequest(ElasticsearchIndexName));
if (!indexExists.Exists)
{
var indexDescriptor =
new CreateIndexDescriptor(new IndexName {Name = ElasticsearchIndexName}).Mappings(
ms => ms.Map<Document>(m => m.AutoMap()));
_client.CreateIndex(indexDescriptor);
}
var doc = new Document()
{
Id = 1,
Title = "Test",
File = attachmentDocument
};
_client.Index(doc);
}
Based on the code above, the document get indexed into the correct index(Screenshot from Elasticsearch host - Searchly):
Searchly Screenshot
The content in the file is : "VCXCVXCVXCVXCVXVXCVXCV" and with the following query I get zero hits in return:
QueryContainer queryContainer = null;
queryContainer |= new MatchQuery()
{
Field = "file",
Query = "VCXCVXCVXCVXCVXVXCVXCV"
};
var searchResult =
await _client.LowLevel.SearchAsync<string>(ApplicationsIndexName, "document", new SearchRequest()
{
From = 0,
Size = 10,
Query = queryContainer,
Aggregations = GetAggregations()
});
I would appericiate if someone could hint me what I am doing incorrectly or should look into?
Providing screenshot of mapping in my Elasticsearch database:
Elasticsearch - Mapping
Because you refer to wrong field. Field should be file.content
queryContainer |= new MatchQuery()
{
Field = "file.content",
Query = "VCXCVXCVXCVXCVXVXCVXCV"
};

How to disable source field and use store with Nest?

I want to disable source as it is done here initially. I am not sure how to achieve similar like in the sample
PUT tweets
{
"mappings": {
"tweet": {
"_source": {
"enabled": false
}
}
}
}
I tried below but when I enter http://localhost:9200/_plugin/head/; I can see that all the properties are stored. I expect to store and index only id and name properties.
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node, defaultIndex: "mydatabase");
var client = new ElasticClient(settings);
var createIndexResult = client.CreateIndex("mydatabase");
var mapResult = client.Map<Product>(c => c.MapFromAttributes().SourceField(s=>s.Enabled(false)).IgnoreConflicts().Type("product").Indices("mydatabase"));
client.Index(sampleproduct);
[ElasticType(Name ="product", IdProperty = "ProductId" )]
[Table("Product")]
public partial class Product
{
[ElasticProperty(Name = "id",Index = FieldIndexOption.NotAnalyzed, Store = true)]
public int ProductId { get; set; }
[ElasticProperty(Index = FieldIndexOption.Analyzed, Store = true)]
public string Name { get; set; }
[ElasticProperty(Index = FieldIndexOption.No, Store = false)]
public int? ProductTypeId { get; set; }
[ElasticProperty(Index = FieldIndexOption.No, Store = false)]
public int? ManufacturerId { get; set; }
}
EDIT: without adding any documents after creating index, index metadata looks like in the image. i dont see any source enabled false.
EDIT2: after changing creating index first then mapping. Name field is displaying store=true as in the image but there is no value stored. I debugged and i am surely passing value where i index the sampleproduct
You need to add the mapping first and then index the document. Only then will you see the get the expected mapping. By indexing first without the mapping, a mapping gets dynamically created with default options (source being enabled).

Elastic search Nest index query always returning false

var local = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(local, null);
var elastic = new ElasticClient(settings);
var res = elastic.CreateIndex(ci => ci
.Index("my_first_index_final2")
.AddMapping<BlogPost>(m => m.MapFromAttributes()));
Console.WriteLine(res.RequestInformation.Success);
var blogPost = new BlogPost
{
Id = Guid.NewGuid(),
Title = "First blog post",
Body = "This is very long blog post!"
};
var firstId = blogPost.Id;
var result = elastic.Index(blogPost, p => p
.Index("my_first_index_final2")
.Id(blogPost.Id.ToString())
.Refresh());
Console.WriteLine(result.RequestInformation.Success);
Blogpost class:
[ElasticType(IdProperty = "Id", Name = "blog_post")]
public class BlogPost
{
[ElasticProperty(Name = "_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public Guid? Id { get; set; }
[ElasticProperty(Name = "title", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Title { get; set; }
[ElasticProperty(Name = "body", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Body { get; set; }
public override string ToString()
{
return string.Format("Id: '{0}', Title: '{1}', Body: '{2}'", Id, Title, Body);
}
}
This is my code. Everytime it returns:
true
false
Meaning, it creates index but unable to insert document into the index. I don't understand the reason.
Also, I have to rename my index name everytime i run this demo console application as i think we cannot insert index with same name. how can i avoid doing this?
I am following this tutorial:
https://www.devbridge.com/articles/getting-started-with-elastic-using-net-nest-library-part-two/
Any other resource for learning nest and elastic search, please feel free to suggest.
My guess is you are using Elasticsearch 2.x. Your code won't break in Elasticsearch 1.x. The problem is, you are trying to add a field _id inside a document. It being one of the metadata fields, Elasticsearch 2.x prohibits you from indexing it inside the document. To make your code work, simply change the name of the Id field from _id to something different, say, id.

Resources