Create index with completion suggest using Nest elasticsearch - c#

I am using elasticsearc 2.3.3 and Nest 2.3.2, I need to create index. Need to map the properties with attributes added in the class file.
public class IndexDocument
{
[Number(Store = true)]
public long Id { get; set; }
[String(Store = true, Index = FieldIndexOption.Analyzed, TermVector = TermVectorOption.WithPositionsOffsets)]
public string Title { get; set; }
public Attachment File { get; set; }
[String(Store = true, Index = FieldIndexOption.Analyzed)]
public string DocumentType { get; set; }
[String(Store = true, Index = FieldIndexOption.NotAnalyzed)]
public string DocLocation { get; set; }
[String(Store = true, Index = FieldIndexOption.Analyzed)]
public DateTime LastModifiedDate { get; set; }
}
public class Attachment
{
public Attachment()
{
}
[String(Name = "_content_length", Store = true, Index = FieldIndexOption.Analyzed)]
public long ContentLength { get; set; }
[String(Store = true, Index = FieldIndexOption.Analyzed, TermVector = TermVectorOption.WithPositionsOffsets, Name = "_content")]
public string Content { get; set; }
}
Also I would like to add a completion suggest to the File field.
I am new in this elastic search. Can anyone please help?
I have created my attachment index as below. How to append completion suggest and stemmer code with this?
this.client.CreateIndex("mydocs", c => c.Mappings(mp => mp.Map<IndexDocument>
(m => m.Properties(ps => ps.Attachment
(a => a.Name(o => o.File)
.TitleField(t => t.Name(x => x.Title).TermVector(TermVectorOption.WithPositionsOffsets))
)))));

Related

Read CSV files without Header using CSVHelper

I have a lot of CSV files without header and need to read it in C#. I manually added header to one of these files and with the following code using CSVHelper I can read the files and show them in a GridView.
Now my question is, how can I read these files without a header? Or how can I add a header (a new record) using CSVHelper in the first line?
public Form1()
{
InitializeComponent();
List<Festival> records;
var config = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";" };
using (var reader = new StreamReader(#"File8.csv"))
using(var csv = new CsvReader(reader, config))
{
records = csv.GetRecords<Festival>().ToList();
}
dataGridView1.DataSource = records;
}
Class
public class Festival
{
public string Day { get; set; }
public string Start { get; set; }
public int Lenght { get; set; }
public string FilmName { get; set; }
public float Rating { get; set; }
}
csv sample
Mi;22:15;110;A;8
Mi;19:00;106;B;8
Mi;19:15;97;C;8.2
Add column-index mapping attributes to the target members:
public class Festival
{
[Index(0)]
public string Day { get; set; }
[Index(1)]
public string Start { get; set; }
[Index(2)]
public int Lenght { get; set; }
[Index(3)]
public string FilmName { get; set; }
[Index(4)]
public float Rating { get; set; }
}
And specify HasHeaderRecord = false in the config:
var config = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";", HasHeaderRecord = false };
If modifying the target model isn't desirable, implement a ClassMap instead:
public sealed class FestivalMap : ClassMap<Festival>
{
public FestivalMap()
{
Map(f => f.Day).Index(0);
Map(f => f.Start).Index(1);
Map(f => f.Lenght).Index(2);
Map(f => f.FilmName).Index(3);
Map(f => f.Rating).Index(4);
}
}
And register it like this before fetching the records (you still need to specify HasHeaderRecord = false in the config):
csv.Context.RegisterClassMap<FestivalMap>();
records = csv.GetRecords<Festival>().ToList();

Adding data from one list to another using linq

I need to populate a dropdown in my UI and hence added List object to the view model in my c# application. I am fetching the data in my controller code for the dropdown. What's the best way to assign data to the viewmodel object. Is linq an option?
I basically need to assign fundclasses to fundTrackRecord.FundClass
The main Viewmodel:
public class FundPerformanceVM
{
public FundPerformanceVM()
{
TrackRecord = new List<TrackRecordVM>();
}
public int FundId { get; set; }
public string FundName { get; set; }
public List<FundClassVM> FundClass { get; set; }
public string BenchmarkName1 { get; set; }
public string BenchmarkName2 { get; set; }
public List<TrackRecordVM> TrackRecord { get; set; }
public List<Tuple<string, string, string>> FundStatistics { get; set; }
}
public class FundClassVM
{
public int FundClassId { get; set; }
public string FundClass { get; set; }
}
Controller code:
var service = GetViewService<V_LEGAL_FUND_CLASS_SUMMARY>();
foreach (KeyValuePair<int, IEnumerable<FUND_PERFORMANCE>> entry in allPerformance)
{
var fundClasses = service.GetAll().Where(x => x.FUND_ID == entry.Key).Select(x => new { x.LEGAL_FUND_CLASS_ID, x.LEGAL_FUND_CLASS}).ToList();
var fundTrackRecord = new FundPerformanceVM();
fundTrackRecord.FundClass = ??;
If I understood correctly the structure of your model, you can try this:
fundTrackRecord.FundClass = fundClasses.Select(fc => new FundClassVM
{
FundClassId = fc.LEGAL_FUND_CLASS_ID,
FundClass = fc.LEGAL_FUND_CLASS
}).ToList();
You can also do this directly, replacing the code:
var fundClasses = service.GetAll().Where(x => x.FUND_ID == entry.Key).Select(x => new { x.LEGAL_FUND_CLASS_ID, x.LEGAL_FUND_CLASS}).ToList();
var fundTrackRecord = new FundPerformanceVM();
With:
var fundTrackRecord = new FundPerformanceVM();
fundTrackRecord.FundClass = service.GetAll().
Where(x => x.FUND_ID == entry.Key).
Select(fc => new FundClassVM
{
FundClassId = fc.LEGAL_FUND_CLASS_ID,
FundClass = fc.LEGAL_FUND_CLASS
}).ToList();

How to use LINQ to assign while selecting to new object

I have a data contract called GameImage and GameTone. I am trying to join the two entities, and assign a unique random position between 0-11 to an Image/Tone association. I am able to join the tables but am unsure if there is a way to assign the position while creating the object in a LINQ lambda expression.
// Need random positions from 0-11 to to be associated to an image/tone
var positions = Enumerable.Range(0, 11).Shuffle().ToList();
// Associate image/tones
imageToneData = game.GameImages.Shuffle()
.Join(game.GameTones, gi => gi.GameId, gt => gt.GameId, (gi, gt) => new ImageToneData
{
Image = new ImageData()
{
ImageFileName = gi.Image.ImageFileName,
ImageId = gi.ImageId
},
Tone = new ToneData()
{
ToneFileName = gt.Tone.ToneFileName,
ToneId = gt.ToneId
},
Position = // What goes here?
});
These are my data contracts
[DataContract]
public class ImageToneData
{
[DataMember]
public ImageData Image { get; set; }
[DataMember]
public ToneData Tone { get; set; }
[DataMember]
public int Position { get; set; }
}
[DataContract]
public class ImageData
{
[DataMember]
public int ImageId { get; set; }
[DataMember]
public string ImageFileName { get; set; }
}
}
[DataContract]
public class ToneData
{
[DataMember]
public int ToneId { get; set; }
[DataMember]
public string ToneFileName { get; set; }
}
var positions = Enumerable.Range(0, 11).OrderBy(a => Guid.NewGuid()).ToList();
// Associate image/tones
imageToneData = game.GameImages.Shuffle()
.Join(game.GameTones, gi => gi.GameId, gt => gt.GameId, (gi, gt) => new ImageToneData
{
Image = new ImageData()
{
ImageFileName = gi.Image.ImageFileName,
ImageId = gi.ImageId
},
Tone = new ToneData()
{
ToneFileName = gt.Tone.ToneFileName,
ToneId = gt.ToneId
},
Position = positions.First()
});

How to map objects

Here is my how my bindings currently look:
MerchantAccountRequest request = new MerchantAccountRequest
{
Individual = new IndividualRequest
{
FirstName = merchant.MerchantIndividual.FirstName,
LastName = merchant.MerchantIndividual.LastName,
Email = merchant.MerchantIndividual.Email,
Phone = merchant.MerchantIndividual.Phone,
DateOfBirth = merchant.MerchantIndividual.DateOfBirth,
Ssn = merchant.MerchantIndividual.Ssn,
Address = new AddressRequest
{
StreetAddress = merchant.MerchantIndividual.StreetAddress,
Locality = merchant.MerchantIndividual.Locality,
Region = merchant.MerchantIndividual.Region,
PostalCode = merchant.MerchantIndividual.PostalCode
}
},
Business = new BusinessRequest
{
LegalName = merchant.MerchantBusiness.LegalName,
DbaName = merchant.MerchantBusiness.DbaName,
TaxId = merchant.MerchantBusiness.TaxId,
Address = new AddressRequest
{
StreetAddress = merchant.MerchantBusiness.StreetAddress,
Locality = merchant.MerchantBusiness.Locality,
Region = merchant.MerchantBusiness.Region,
PostalCode = merchant.MerchantBusiness.PostalCode
}
},
Funding = new FundingRequest
{
Descriptor = merchant.MerchantFunding.Descriptor,
Destination = FundingDestination.BANK,
Email = merchant.MerchantFunding.Email,
MobilePhone = merchant.MerchantFunding.MobilePhone,
AccountNumber = merchant.MerchantFunding.AccountNumber,
RoutingNumber = merchant.MerchantFunding.RoutingNumber
},
TosAccepted = merchant.TosAccepted,
MasterMerchantAccountId = merchant.MasterMerchantAccountId,
Id = merchant.MerchantId
};
And I need to use AutoMapper to achieve the above.
This is what I've tried:
CreateMapperProfile
EDIT:
CreateMap<Classes.Merchant, MerchantAccountRequest>()
.ForMember(dest => dest.Individual, source => source.MapFrom(s => s.MerchantIndividual))
.ForMember(dest => dest.Business, source => source.MapFrom(s => s.MerchantBusiness))
.ForMember(dest => dest.Funding, source => source.MapFrom(s => s.MerchantFunding))
.ForMember(dest => dest.Id, source => source.MapFrom(s => s.MerchantId));
Here is the Merchant class:
public partial class Merchant :
{
public int Id { get; set; }
public string MerchantId { get; set; }
public virtual MerchantIndividual MerchantIndividual { get; set; }
public virtual MerchantBusiness MerchantBusiness { get; set; }
public virtual MerchantFunding MerchantFunding { get; set; }
public bool TosAccepted { get; set; }
public string MasterMerchantAccountId { get; set; }
public bool isSubMerchant { get; set; }
}
And the mappings;
EDIT:
MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant);
request.Individual = _mapper.Map<IndividualRequest>(merchant.MerchantIndividual);
request.Business = _mapper.Map<BusinessRequest>(merchant.MerchantBusiness);
request.Funding = _mapper.Map<FundingRequest>(merchant.MerchantFunding);
Can the first line of code MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant); above do all the mapings?
..
..
..
..
How can I create the correct mappings?
You don't need to call _mapper.Map on the properties of the request.
Just call MerchantAccountRequest request = _mapper.Map<MerchantAccountRequest>(merchant); and assuming you have a map for each type you should be fine.
I think something along the lines of the following should get you going down the right path.
class Program
{
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Merchant, MerchantAccountRequest>()
.ForMember(dest => dest.Individual, c => c.MapFrom(source => source.MerchantIndividual));
cfg.CreateMap<MerchantIndividual, IndividualRequest>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var merchant = new Merchant
{
Id = 1,
MerchantIndividual = new MerchantIndividual { FirstName = "John Doe" }
};
var merchantAccountRequest = mapper.Map<Merchant, MerchantAccountRequest>(merchant);
}
}
public class Merchant
{
public int Id { get; set; }
public MerchantIndividual MerchantIndividual { get; set; }
}
public class MerchantIndividual
{
public string FirstName { get; set; }
}
public class MerchantAccountRequest
{
public int Id { get; set; }
public IndividualRequest Individual { get; set; }
}
public class IndividualRequest
{
public string FirstName { get; set; }
}

How to pass a cookie value string array to the controller to get looped as PayPal Items

I'm trying to pass my CookieCarts string array (containing shopping cart items) into my controller to get looped for my Paypal api.
My View
var cookiecart = Server.UrlDecode(Request.Cookies["cookieCart"].Value);
#Html.HiddenFor(m => m.CookieCart, new { Value = cookiecart })
Response.Write(cookiecart);
cookiecart:*[{"datetime":"2016-02-25 02:51:49","id":"749","typeid":"13","qty":1,"fullname":"The Matrix","image":"/Content/images/products/online-video.png","price":"69","sku":"MATRIX"}]*
My Model
public string CookieCart { get; set; }
My Controller
var cartArray = model.CookieCart;
var cartArray = model.CookieCart;
var itemArray = cartArray.Split(',');
foreach (var t in itemArray)
{item.name = itemArray[0]; }
when i quickwatch the data sent to the controller it looks like this:
cartArray displays: "[{\"datetime\":\"2016-02-25 02:51:49\",\"id\":\"749\",\"typeid\":\"13\",\"qty\":1,\"fullname\":\"The Matrix\",\"image\":\"/Content/images/products/online- video.png\",\"price\":\"69\",\"sku\":\"MATRIX\"}]"
item.name displays: *"[{\"datetime\":\"2016-02-25 02:51:49\""*
None of this is right. its so frustrating! How to convert a cookie array value into a C# array.
itemArray[0] should be:
itemArray[0][0] = datetime:"2016-02-25 02:51:49",
itemArray[0][1] = id:"749",
itemArray[0][2] = typeid:"13",
itemArray[0][3] = qty:1,
itemArray[0][4] = fullname:"The Matrix",
itemArray[0][5] = image:"/Content/images/products/online-video.png",
itemArray[0][6] = price:"69"
itemArray[0][7] = sku:"MATRIX"
:(
ok i figured it out. using JSON .Net:
My Controller
var cookie = Request.Cookies["cookieCart"];
cookieArray = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(cookie.Value));
My Model
public class CookieCart
{
public DateTime Datetime { get; set; }
public int Id { get; set; }
public int Typeid { get; set; }
public string Qty { get; set; }
public string Fullname { get; set; }
public string Image { get; set; }
public string Price { get; set; }
public string Sku { get; set; }
}
then i iterated the array items for PayPal:
foreach (var cartitem in cookiecart)
{
item.name = cartitem.Fullname;
item.currency = "USD";
item.price = cartitem.Price;
item.quantity = cartitem.Qty;
item.sku = cartitem.Sku;
var intPrice = Int32.Parse(cartitem.Price);
subtotal = subtotal + intPrice;
}

Categories

Resources