I'm using Nest 2.2.0 and am trying to build a multimatch query as follows:
var searchQuery = new MultiMatchQuery()
{
Fields = Field<Product>(p=>p.SKUName, 2),
Query = "hello world"
};
When I run it however, it returns:
The non-generic type 'Nest.Field' cannot be used with type arguments.
I don't understand why I'm getting the error, since I've more or less taken this query straight from the documentation found at https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/multi-match-usage.html#_object_initializer_syntax_example_35.
In case it matters, I've defined the Product as follows:
[ElasticsearchType(Name="product", IdProperty="Id")]
public class Product
{
[Nest.Number(Store = true)]
public int Id {get;set;}
[String(Name="name", Store = true, Index=FieldIndexOption.Analyzed)]
public string SKUName { get; set; }
}
Is anyone able to help?
The Field type you're looking for is Nest.Infer.Field
var searchQuery = new MultiMatchQuery()
{
Fields = Nest.Infer.Field<Product>(p => p.SKUName, 2),
Query = "hello world"
};
client.Search<Product>(new SearchRequest { Query = searchQuery });
Related
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.
I have a message structure that contains the following
DateTime dateIn;
long? messageId;
string messageContent;
I am trying to group my message list based on messageId and ordered by the dateIn field.
My LINQ currently looks like this
var groups = from c in MessageList
let name = c.messageId
orderby name ascending, c.dateIn ascending
group c by name into g
select g;
When I try and feed it back into a new List< Messages>, the compiler comes back with
"Cannot implicitly convert type System.Collections.Generic.List<
System.Linq.IGrouping < long?, Messages> > to
System.Collections.Generic.List< Messages>"
Is the problem down to the long? more than anything? I have tried to cast messageId to long, but that doesn't seem to work either.
I suppose you can use SelectMany to orbitaine your Message collection from the grouped one (if I understood you correct):
List<Messages> back = groups.SelectMany(m=>m.ToList()).ToList();
UPDATED
According to your comments. When you use GroupBy - Linq creates the Enumerable of new generic type combining your collection type and key type, which you use for grouping. In your case it is Messages type and long? (the type of messageId - key, you are grouping by). So this should work for you:
List<long?,Messages> grouped = groups.ToList();
Or you can use var and this should work also:
var grouped = groups.ToList();
If I understand what are you trying to do, there is simpler way. Look at my demo:
public class Program
{
static void Main(string[] args)
{
List<Message> MessageList = new List<Message>()
{
new Message(){ dateIn = new DateTime(2000,1,11)},
new Message(){ dateIn = new DateTime(2000,1,9)},
new Message(){ dateIn = new DateTime(2000,1,8), messageId = 5},
new Message(){ dateIn = new DateTime(2000,1,12)},
new Message(){ dateIn = new DateTime(2000,1,2), messageId = 7}
};
foreach (var item in MessageList)
{
Console.WriteLine(item);
}
Console.WriteLine("===");
// MUCH SIMPLER
var result = MessageList
.GroupBy(m => m.messageId)
.SelectMany(m => m.ToList())
.OrderBy(m => m.dateIn);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
struct Message
{
public DateTime dateIn { get; set; }
public long? messageId { get; set; }
public string messageContent { get; set; }
public override string ToString()
{
return messageId + "\t" + dateIn;
}
}
I want to retrieve a specific record using IQueryable. But i get error 'No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.'. I got the selected row id, but I cannot display it out. Here is my code.
internal static IQueryable GetRecordsFromPrimaryKeys(this IQueryable datasource, List<FilterDescriptor> primaryKeys)
{
IQueryable data = datasource;
ParameterExpression paramExp = null;
bool firstLoop = false;
System.Linq.Expressions.Expression predicate = null;
var RecordType = datasource.GetObjectType();
paramExp = RecordType.Parameter();
foreach (FilterDescriptor primaryKey in primaryKeys)
{
if (!(firstLoop))
{
predicate = data.Predicate(paramExp, primaryKey.ColumnName, primaryKey.Value, FilterType.Equals, false, RecordType);
firstLoop = true;
}
else
{
predicate = predicate.AndPredicate(data.Predicate(paramExp, primaryKey.ColumnName, primaryKey.Value, FilterType.Equals, false, RecordType));
}
}
if (paramExp != null && predicate != null)
{
var lambda = Expression.Lambda(predicate, paramExp);
data = data.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"Where",
new Type[] { data.ElementType },
data.Expression,
lambda
)
);
}
return data;
}
My Code works well for IEnumerable/IQueryable/ICollection . But it throws the exception when i specify the class with the keyword virtual and type as ICollection. My code is
public class RoomType
{
public int ID { get; set; }
[MaxLength(10, ErrorMessage = "Room code cannot be longer than 10 characters.")]
public string Code { get; set; }
[MaxLength(50, ErrorMessage = "Room name cannot be longer than 50 characters.")]
public string Name { get; set; }
public virtual ICollection<RoomCategory> RoomCategories { get; set; }
}
Some random values gets appended to 'RecordType' while using the keyword 'virtual'. I think this leads to the exception. Still searching for the solution.
I don't know what is going wrong . Any suggestions welcome.
Thanks.
I just ran into a similar situation. The problem stems from the fact that in some cases you're dealing with the "proxy" not the actual entity. So, you want to make sure that RecordType matches data.ElementType.
try:
var recordType = datasource.GetObjectType();
// make sure we have the correct type (not the proxy)
if (recordType.BaseType.Name != "Object")
recordType = recordType.BaseType;
Or better yet, try:
var recordType = data.ElementType
Try to use typeof(Enumerable) instead of typeof(Queryable)
This question is a follow up to this question:
How to create a list from two values
Consider this code:
class MainClass()
{
string MainKey {get;set;}
string MainName {get;set;}
IEnumerable<SmallObject> MainList {get;set}
}
class SmallObject()
{
string SmallKey {get;set}
}
and:
var mainQuery = (from v from DataContext.myTable
select v);
var myQuery = (from v in mainQuery
select new MainClass()
{
MainKey = v.Field1,
MainName = v.Field2,
MainList = new []
{
new SmallObject { SmallKey = v.Field3 },
new SmallObject { SmallKey = v.Field4 },
}
});
var result1 = myQuery.ToList();
//Changing datatypes for optimization reasons in SQLServer2000
var cmd = DataContext.GetCommand(myQuery);
foreach (System.Data.Common.DbParameter param in cmd.Parameters)
{
// nvarchar -> varchar
// decimal -> numeric
}
var result2 = DataContext.Translate<MainClass>(cmd.ExecuteReader()).ToList();
result1.MainList is OK
result2.MainList is null
The original query was very slow running on SQLServer2000, and I got it fixed when changing datatypes (Linq uses nvarchar and decimal, as my database use varchar and numeric)
So I want result2 to be the same as result1, but that doesn't happen when doing a DataContext.Translate like this.
Any thoughts of getting the same result here?
I've also tryed anonymous types, like this:
IEnumerable<object> MainList {get;set;}
...
MainList = new []
{
new { SmallKey = v.Field3},
new { SmallKey = v.Field4},
}
but the result is the same:
I think you are asking too much from Translate.
If I understand you correctly, it is the first query (mainQuery) that is too slow, so I would look to replace it.
I would create a simpler temporary class like
public class TmpClass
{
public string Field1 {get;set;}
public string Field2 {get;set;}
public string Field3 {get;set;}
public string Field4 {get;set;}
}
Once the list is in this format, you can use the second query to change it to a list of MainClass.
Just a matter of interest, what is the difference between the sql outputted by Linq and your customized version? Unless it is does some casting, I would not expect this type of query to need optimizing.
I would use the AsEnumerable extension method which basically converts the IQueryable to an IEnumerable which forces the enumerator to be processed. You could achieve the same thing by calling ToArray() or ToList() but AsEnumerable() magically lets you return it back to an IQueryable by calling AsQueryable()
So probably doing the following will work for you:
var result1 = DataContext.myTable.AsEnumerable()
.Select(v=> new MainClass {
MainKey = v.Field1,
MainName = v.Field2,
MainList = new []
{
new SmallObject { SmallKey = v.Field3 },
new SmallObject { SmallKey = v.Field4 },
}
});
I'm quite new to csharp and linq, so am having some trouble with a simple enough casting issue.
I have a simple object -
public class AskQuestionFormView
{
public string QuestionText { get; set; }
public Dictionary<string,int> Location { get; set; }
}
and a linq query
AskQuestionFormView aqfv = new AskQuestionFormView();
var result = from c in db.Countries
.ToDictionary(c => c.LocationName.ToString(),
c=>c.ID)
select c;
aqfv.Location = (Dictionary<string,int>)result;
but I'm getting the following error -
System.InvalidCastException: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Collections.Generic.KeyValuePair`2[System.String,System.Int32],System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]]' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]'.
What am I doing wrong?
Thanks in advance.
Your query is attempting to query from the dictionary, which is returning a KeyValuePair when you use select c. You just need to generate a Dictionary from the table, so try this instead:
var result = db.Countries.ToDictionary(c => c.LocationName, c => c.ID);
aqfv.Location = result;
Try it like this:
var result = (from c in db.Countries
select c).ToDictionary(c => c.LocationName.ToString(),
c=>c.ID);
aqfv.Location = result;