Solr C# client that convert query result to object - solr

I am really new to solr.
I am wondering if there is C# client that allow me to convert solr query result to C# object?
I also have the solr schema xml but not sure how to convert it to C# class? Do i need to parse the xml?

There are many different Solr client implmentations for C#.
I have tried couple different ones, but only SolrNet worked with the current version.
(SolrNet version: SolrNet-0.4.0.2002.zip (843.53Kb), Solr version: 4.7.1)
First I have tried to use the NuGet package, but it turned out that it is outdated (was using deprecated API), get the newest version from here:
Solr Net - Master
Build the dlls and use them in your project.
You can find a nice tutorial here:
SolrNet tutorial
The only problem here is that these calls are now deprecated. Instead of Add method you should be calling the actually supported Commands. Add and the other derprecated methods already worked well but I don't think that you should rely on deprecetad calls.
Instead of these you can use the different Command classes representing the different Solr operations.
Example:
using Microsoft.Practices.ServiceLocation;
using SolrNet;
using SolrNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
class Product
{
[SolrUniqueKey("id")]
public string Id { get; set; }
[SolrField("manu")]
public string Manufacturer { get; set; }
[SolrField("cat")]
public ICollection<string> Categories { get; set; }
[SolrField("price")]
public decimal Price { get; set; }
[SolrField("inStock")]
public bool InStock { get; set; }
}
static void Main(string[] args)
{
Startup.Init<Product>("http://localhost:8983/solr");
var p = new Product
{
Id = "SP2514M",
Manufacturer = "Samsung Electronics Co. Ltd.",
Categories = new[] {
"electronics",
"hard drive",
},
Price = 92,
InStock = true,
};
var solr = ServiceLocator.Current.GetInstance<ISolrConnection>();
Dictionary<Product, double?> dict = new Dictionary<Product,double?>();
dict.Add(p, 0);
Console.WriteLine("-- Add products --");
AddProducts(solr, dict);
Console.WriteLine("-- Commit changes --");
CommitChanges(solr);
Console.WriteLine("--Query all documents --");
QueryAll();
Console.WriteLine("-- Delete all documents --");
DeleteAll(solr);
Console.WriteLine("-- Commit changes --");
CommitChanges(solr);
Console.WriteLine("--Query all documents --");
QueryAll();
Console.ReadKey();
}
private static void DeleteAll(ISolrConnection solr)
{
SolrNet.DeleteParameters del = new DeleteParameters();
SolrNet.Impl.FieldSerializers.DefaultFieldSerializer deffield = new SolrNet.Impl.FieldSerializers.DefaultFieldSerializer();
SolrNet.Impl.QuerySerializers.DefaultQuerySerializer defquery = new SolrNet.Impl.QuerySerializers.DefaultQuerySerializer(deffield);
SolrNet.Commands.Parameters.DeleteByIdAndOrQueryParam delpar = new SolrNet.Commands.Parameters.DeleteByIdAndOrQueryParam(Enumerable.Empty<string>(), SolrNet.SolrQuery.All, defquery);
var delete = new SolrNet.Commands.DeleteCommand(delpar, del);
string res = delete.Execute(solr);
System.Diagnostics.Trace.WriteLine(res);
}
private static void CommitChanges(ISolrConnection solr)
{
SolrNet.Commands.CommitCommand commit = new SolrNet.Commands.CommitCommand();
string res = commit.Execute(solr);
System.Diagnostics.Trace.WriteLine(res);
}
private static void AddProducts(ISolrConnection solr, Dictionary<Product, double?> dict)
{
AddParameters par = new AddParameters();
ISolrDocumentSerializer<Product> ser = ServiceLocator.Current.GetInstance<ISolrDocumentSerializer<Product>>();
SolrNet.Commands.AddCommand<Product> addProduct = new SolrNet.Commands.AddCommand<Product>(dict, ser, par);
string res = addProduct.Execute(solr);
System.Diagnostics.Trace.WriteLine(res);
}
private static void QueryAll()
{
// Query all documents
var query = SolrNet.SolrQuery.All;
var operations = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
var Products = operations.Query(query);
int i = 0;
foreach (var product in Products)
{
i++;
Console.WriteLine("{0}:\t {1} \t{2} \t{3}", i, product.Id, product.Manufacturer, product.Price);
}
if(i == 0)
{
Console.WriteLine(" = no documents =");
}
}
}
}
I am very new to Solr too so my code may contain some flaws. Please help me spot them. Thank you.
See QueryAll method for querying and using the resulting objects.

Related

WCF Service and Windows Application Client. why client receives null

I have mapped the service with a linq to Sql classes and I am using wcf library for vs 2019 and in client a win form app.
I am trying sending the class created for linq to sql the next way
public List<Trades> GetAllTradings()
{
context = new DCStockTradingDataContext();
List<Trades> tradings = (from t in context.Trades
select t).ToList();
return tradings;
}
and the client
private void btnRun_Click(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
var trades = client.GetAllTradings();
dgViewStocks.DataSource = trades;
//string ret = client.GetData("Hello");
//Console.WriteLine(ret);
}
I din´t know what is happening and I don´t know what is wrong
The service
and the client receives all null
I would appreciate any help about this and thank you in advance
If you get null response in WCF client, some advices:
try to call the service in SoapUi with Validation of request and response turned on. It may be useful in detecting problems.
debug Reference.cs file to see more closely what is going on
use MessageInspector to see what is received in AfterReceiveReply method
examine namespaces attentively, response often cannot be deserialized because of the difference in namespaces that are in Reference.cs and in real service
Thanks for your response and everything if this I did.
I found how I have to work with linq to sql classes and wcf. We need to take into account that you need to convert the List of linq classes to List<string. What I did
enter code here
public List<string[]> GetAllStocks()
{
context = new DCStockTradingDataContext();
System.Data.Linq.Table<Stocks> stocks = context.GetTable<Stocks>();
var allStock = (from st in stocks
select new
{
stockId = (string) st.CodeID,
currency = (char) st.Currency,
stName = st.Name,
price = (decimal) st.Price,
volument = (decimal) st.Volumen
}).ToList();
List<string[]> listRetruned = new List<string[]>();
foreach (var tr in allStock)
{
string[] t = new string[5];
t[0] = tr.stockId;
t[1] = tr.currency.ToString();
t[2] = tr.stName;
t[3] = tr.price.ToString();
t[4] = tr.volument.ToString();
listRetruned.Add(t);
}
return listRetruned;
}
and the client
I have created a model class with the expected data
public class TradingModel
{
public string TradeID { get; set; }
public string StockId { get; set; }
public decimal Bid { get; set; }
public int BidQty { get; set; }
public decimal Ask{get;set;}
public int AskQty { get; set; }
public decimal Last { get; set; }
}
and finally the method
List<TradingModel> models = new List<TradingModel>();
for(int i = 0; i < trades.Length; i++)
{
TradingModel model = new TradingModel()
{
Ask = Convert.ToDecimal(trades[i][0]),
Bid = Convert.ToDecimal(trades[i][1]),
BidQty = Convert.ToInt32(trades[i][2]),
AskQty = Convert.ToInt32(trades[i][3]),
Last = Convert.ToDecimal(trades[i][4]),
StockId = trades[i][5],
TradeID = trades[i][6],
};
models.Add(model);
}
dgViewStocks.DataSource = models;
I am not sure if this is the best way to jump to next step, but it worked for me. If someone is looking for this info as I did, wasting several days chasing the solution, I leave what I did.
There is off course changing the service and generate the proxy again

Dapper control dates

This question is meant to bring some light around control date times using Dapper.
These controls are used to audit the information in a data storage and figure out when a particular row has been created / updated. I couldn't manage to find any information on GitHub's project, either here in StackOverflow, so I would like this post to become a central source of truth to help others or even to turn into a future extension of the library.
Any answer, resource or best practice will be appreciated.
I've ran into a case where I was working with a database that was consumed by both Rails and Dapper. Rails was managing created_at and updated_at, not the database. So with the .net application I had to implement a solution that managed these and provided the ability to add additional business logic at these layers such as events.
I've included a basic example of how I handled this with a wrapper around Dapper Simple Crud for inserts and updates. This example does not include exposing the other critical methods from dapper and simplecrud such as Query, GET, Delete, etc. You will need to expose those at your discresion.
For safety ensure that you decorate your models created_at property with the attribute [Dapper.IgnoreUpdate]
[Table("examples")]
public partial class example
{
[Key]
public virtual int id { get; set; }
[Required(AllowEmptyStrings = false)]
[StringLength(36)]
public virtual string name { get; set; }
[Dapper.IgnoreUpdate]
public virtual DateTime created_at { get; set; }
public virtual DateTime updated_at { get; set; }
}
public class ExampleRepository : IExampleRepository
{
private readonly IYourDapperWrapper db;
public PartnerRepository(IYourDapperWrapper yourDapperWrapper){
if (yourDapperWrapper == null) throw new ArgumentNullException(nameof(yourDapperWrapper));
db = yourDapperWrapper;
}
public void Update(example exampleObj)
{
db.Update(exampleObj);
}
public example Create(example exampleObj)
{
var result = db.Insert(exampleObj);
if (result.HasValue) exampleObj.id = result.value;
return exampleObj;
}
}
public class YourDapperWrapper : IYourDapperWrapper
{
private IDbConnectionFactory db;
public YourDapperWrapper(IDbConnectionFactory dbConnectionFactory){
if (dbConnectionFactory == null) throw new ArgumentNullException(nameof(dbConnectionFactory));
db = dbConnectionFactory;
}
public int Insert(object model, IDbTransaction transaction = null, int? commandTimeout = null)
{
DateUpdate(model, true);
var results = Db.NewConnection().Insert(model, transaction, commandTimeout);
if (!results.HasValue || results == 0) throw new DataException("Failed to insert object.");
return results;
}
public int Update(object model, IDbTransaction transaction = null, int? commandTimeout = null)
{
DateUpdate(model, false);
var results = Db.NewConnection().Update(model, transaction, commandTimeout);
if (!results.HasValue || results == 0) throw new DataException("Failed to update object.");
return results;
}
private void DateUpdate(object model, bool isInsert)
{
model.GetType().GetProperty("updated_at")?.SetValue(model, DateTime.UtcNow, null);
if (isInsert) model.GetType().GetProperty("created_at")?.SetValue(model, DateTime.UtcNow, null);
}
}

Why is creating a MSSQL(Express) database with Entity Framework so slow?

We are developing a WPF application which can manage multiple clients. For each client we need a separate database (within the same SQL Server instance). So we need a „create database“ more than once. All individual client-databases will have all the same database structure.
We use Entity Framework Version 6.1.3, code-first and a local installation of SQL Server 2014 Express. In our „real“ business-application a new database with about 60 tables is created properly, but the performance is not so good.
Trying to isolate the problem I wrote a small sample program which creates 5 databases in a loop, 1 table per database. On my Dev-PC (Windows 10, i7-6700 3.40GHz, 16 GB RAM) the executiontime for those 5 databases is about 1 minute (11-12 secs for each database). When I debug the application, I see that there is a long wait on the line
dbContext.Database.CreateIfNotExists();
In SQL Server Profiler I see that it takes about 10 seconds per loop until the first entry arrives.
Any idea's where the time is lost? Or other suggestions for creating many databases (> 20) with the same DbContext in Entity Framework?
Here the source-code of my sample application:.
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System;
namespace TestEnityFramework01.Model
{
[Table("Table01")]
public class Table01 {
[Key]
[Column("ID", Order = 0, TypeName = "int")]
public int ID { get; set; }
[Column("Number01", TypeName = "int")]
public int Number01 { get; set; }
[Column("Date01")]
public DateTime? Date01 { get; set; }
[StringLength(50)]
[Column("Text01", TypeName = "nvarchar")]
public string Text01 { get; set; }
[Column("Amount01")]
public decimal Amount01 { get; set; }
[Column("Doule01")]
public double Doule01 { get; set; }
[Column("Bool01")]
public bool Bool01 { get; set; }
}
}
And the context:
using TestEnityFramework01.Model;
using System.Data.Entity;
namespace TestEnityFramework01.Context
{
class ContextA : DbContext
{
public DbSet<Table01> Tabel01 { get; set; }
public ContextA()
: base("ContextA")
{
// Don't create database automatically
Database.SetInitializer<ContextA>(null);
}
public ContextA(string pConnectionString)
: base(pConnectionString)
{
// Don't create database automatically
Database.SetInitializer<ContextA>(null);
this.Configuration.AutoDetectChangesEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ValidateOnSaveEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
The test program produces in a loop five databases:
private void button_Click(object sender, RoutedEventArgs e)
{
int _randomNumber = 0;
for (int i = 1; i <= 5; i++)
{
// create random number between 1 and 100'000
_randomNumber = new Random().Next(1, 100000);
// concat the random number to the database name
string _databaseName = "DatabaseCustomer" + _randomNumber.ToString();
// the connection-string look like this:
// "Data Source=MyPC\SQLEXPRESS;Initial Catalog=MyDatabaseName;User ID=MyUser;Password=MyPassword;Integrated Security=False;MultipleActiveResultSets=True;"
string _connectionString = GetConnectionString(_databaseName);
using (var dbContext = new ContextA(_connectionString))
{
// create the database
dbContext.Database.CreateIfNotExists();
}
}
}
**** UPDATE 16.06.2016****
When I switch the DB connection from SQL-Server 2014 Express to „SQL Server Compact Edition“, the creationtime for all 5 databases is about 1 or 2 seconds. Could it be that i use a wrong connectionstring to connect with the sql-server or my SQL-Server 2014 Express is not configured properly?
This is because the code first Approach creates a new database and establishes connection with it.
I Think you should use sql query for Creating Database.
If You Want I can help you how will you do it with better UI and Functionality design
Please reply for my answer.

Entity Framework : Create a model from Dictionary<TKey,TValue> to be mapped to a database table

Earlier I had a table named ApplicationConfiguration which simply had [Key],[Value] columns to store some config data. This was queried straight away using SQL queries.
Now I intend to make use of Entity Framework (EF) Code First approach to query this table. The specialty of this table is that the table will have only a fixed number of rows in its lifetime. Only the Value column can be updated.
So as per the code first approach, we have to first write our POCO classes with its properties that will be mapped to columns in the underlying table. However, I wish to have a Dictionary<> structure to represent these configuration KV pairs. My concern is, will EF be able to fire update queries against any updation to the the value of a particular pair.
Also since I am using Code First approach, I would want some seed data(i.e the fixed number of rows and its initial content) to the added after the table itself is created on the fly when the application is first executed.
If Dictionary<> cannot be used, please suggest some alternative. Thanks in advance.
Coded this way:
public class ApplicationConfiguration
{
public int Id { get; set; }
public string Key { get; set; }
public int Value { get; set; } // should be string, but I'm lazy
}
class Context : DbContext
{
internal class ContextInitializer : DropCreateDatabaseIfModelChanges<Context>
{
protected override void Seed(Context context)
{
var defaults = new List<ApplicationConfiguration>
{
new ApplicationConfiguration {Key = "Top", Value = 5},
new ApplicationConfiguration {Key = "Bottom", Value = 7},
new ApplicationConfiguration {Key = "Left", Value = 1},
new ApplicationConfiguration {Key = "Right", Value = 3}
};
// foreach (var c in defaults)
// context.ConfigurationMap.Add(c.Key, c); // by design, no IReadOnlyDictionary.Add
foreach (var c in defaults)
context.ApplicationConfigurations.Add(c);
base.Seed(context);
}
}
public Context()
{
Database.SetInitializer(new ContextInitializer());
}
private IDbSet<ApplicationConfiguration> ApplicationConfigurations
{
get { return Set<ApplicationConfiguration>(); }
}
public IReadOnlyDictionary<string, ApplicationConfiguration> ConfigurationMap
{
get { return ApplicationConfigurations.ToDictionary(kvp => kvp.Key, kvp => kvp); }
}
}
Used this way:
using (var context = new Context())
{
ReadConfigurationOnly(context.ConfigurationMap);
}
using (var context = new Context())
{
ModifyConfiguration(context.ConfigurationMap);
context.SaveChanges();
}
static void ReadConfigurationOnly(IReadOnlyDictionary<string, ApplicationConfiguration> configuration)
{
foreach (var k in configuration.Keys)
Console.WriteLine("{0} = {1}", k, configuration[k].Value);
}
static void ModifyConfiguration(IReadOnlyDictionary<string, ApplicationConfiguration> configuration)
{
foreach (var k in configuration.Keys)
configuration[k].Value++; // this is why I was lazy, using an int for a string
}
So, I wrote it up this way — using an int Value property rather than a string — just so I could run the "Used this way" code over and over, and see the database update each time, without having to come up with some other way to change Value in an interesting way.
It's not quite as nifty here to use a IReadOnlyDictionary<string, ApplicatonConfiguration> instead of a IReadOnlyDictionary<string, string>, the way we'd really like, but that's more than made up for by the fact that we can easily modify our collection values without resorting to a clumsier Set method taking a dictionary as input. The drawback, of course, is that we have to settle for configuration[key].Value = "new value" rather than configuration[key] = "new value", but — as I say — I think it's worth it.
EDIT
Dang! I wrote this code up specifically to answer this question, but I think I like it so much, I'm going to add it to my bag of tricks ... this would fit in really well when my company goes from local databases to Azure instances in the cloud, and the current app.config has to go into the database.
Now all I need is a ContextInitializer taking a System.Configuration.ConfigurationManager as a ctor parameter in order to seed a new database from an existing app.config ...
I don't think you can map a table directly to a Dictionary; you will probably have to write your own wrapper to fill a dictionary from the table and update it back to the DB. Entities are each a row of a given table... Something like this (untested):
public Dictionary<string, string> GetDictionary()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
using (var db = new Context())
{
var configs = db.ApplicationConfiguration.Select();
foreach (var entry in configs)
{
dic.Add(config.Key, config.Value);
}
}
return dic;
}
public void SaveConfig(Dictionary<string, string> dic)
{
using (var db = new Context())
{
foreach (KeyValuePair kvp in dic)
{
if (!db.ApplicationConfiguration.First(a => a.Key == kvp.Key).Value == kvp.Value)
{
var ac = new ApplicationConfiguration();
ac.Key = kvp.Key;
ac.Value = kvp.Value;
db.Entry(ac).State = EntityState.Modified;
}
}
db.SaveChanges();
}
}
For your second question, you want to use the Seed() method to add initial values to the database. See here for an example implementation.

How can I make Entity Framework only update object dependencies?

I'd like to know how can I make Entity Framework update an object instead of always inserting a new one for each new main object.
For example:
I have these objects:
Main Object:
public class ExtraArticleAttributes
{
[Key]
public int extraarticleattributes_id { get; set; }
virtual public WorldData world_data { get; set; }
}
Its dependencie:
public class WorldData
{
[Key]
public int worlddata_id { get; set; }
public string country { get; set; }
So, how can I make Entity Framework when inserting a new ExtraArticleAttributes verify if already exists a WorldData object and only update it?
I've been reading some articles about it and I notice that Entity Framework identify an existing object in DB with a HASH code, so when I get it from an API, and try to insert It in the DB, even though the object has the same data, the Entity Framework doesn't recognize like an existed object in DB. Does exist a way of make It, without spending request to the DB to verify if the object exists, if true get It.
Set the entity state to Modified:
using System.Data.Entity;
// Assuming that there is already an existing WorldData record in the database with id 1 and country 'foo', and you want to change the country to 'bar'
using (var context = new MyContext())
{
var extraArticleAttributes = new ExtraArticleAttributes
{
world_data = new WorldData
{
worlddata_id = 1,
country = "bar"
}
};
db.ExtraArticleAttributes.Add(extraArticleAttributes);
db.Entry<WorldData>(extraArticleAttributes.world_data).State = EntityState.Modified;
db.SaveChanges();
// world data 1 country is now 'bar'
}

Resources