[英]Conditional Sorting on 2 fields in Elastic Search Nest
I have type in my data, if type is A then i need to sort by price1 and if type is B then i need to sort by price2, 我在数据中输入了类型,如果类型为A,则需要按price1排序,如果类型为B,则需要按price2排序,
so in c# NEST how we can write query for this? 所以在C#NEST中我们如何为此编写查询?
{
"test": {
"mappings": {
"listings": {
"properties": {
"productname": {
"type": "string"
},
"ptype": {
"type": "string"
},
"price1": {
"type": "float"
},
"price2": {
"type": "float"
}
}
}
}
}
}
Is something like this what you're looking for? 您正在寻找类似这样的东西吗?
_elasticClient.SearchAsync<ListingsModel>(s => s
.Type(type)
.Sort(o =>
{
if(type == "A")
return o.OnField("price1");
else
return o.OnField("price2");
})
//rest of query
The best I was able to do was something like this 我能做的最好的事情就是这样
class Program
{
static void Main(string[] args)
{
var random = new Random();
var uri = new Uri("http://localhost.fiddler:9200");
var indexName = "test_index";
ElasticClient db = new ElasticClient(uri);
db.DeleteIndex(indexName);
foreach (var item in Enumerable.Range(0, 10).Select(i => new A { Price1 = random.NextDouble() * 1000 }))
{
db.Index(item, inx => inx.Index(indexName));
}
foreach (var item in Enumerable.Range(0, 10).Select(i => new B { Price2 = random.NextDouble() * 1000 }))
{
db.Index(item, inx => inx.Index(indexName));
}
//db.IndexMany(Enumerable.Range(0, 10).Select(i => new A { Price1 = random.NextDouble() * 1000 }), indexName); When using this got nothing back since the query was too fast after index
//db.IndexMany(Enumerable.Range(0, 10).Select(i => new B { Price2 = random.NextDouble() * 1000 }), indexName);
var data = db.Search<JObject>(q =>
q.Index(indexName)
.Size(20)
.Type("")
.Sort(s => s.Script(scd => scd
.Type("number")
.Script(sc => sc
//.Inline(@" doc['price1']? doc['price1'] : doc['price2']") if no price1 field in object B then you can use this and no need for TypeIndex
.Inline(@"doc['typeIndex'] == 0? doc['price1'] : doc['price2']")// typeIndex must be a number lucene has no string literal support
.Lang("expression")
).Order(SortOrder.Descending))));
Console.WriteLine("DONE");
Console.ReadLine();
}
}
[ElasticsearchType(Name="A")]
public class A
{
[Number]
public int TypeIndex { get { return 0; } }
[Number]
public double Price1 { get; set; }
}
[ElasticsearchType(Name = "B")]
public class B
{
[Number]
public int TypeIndex { get { return 1; } }
[Number]
public double Price2 { get; set; }
}
You could do it with groovy but I don't know how good will it performe. 您可以通过常规操作来做到这一点,但我不知道它的性能如何。 expression are on by default and they use lucene .
默认情况下,expression处于启用状态,并且使用lucene 。 I do think it is a bit "hacky" but hope helps a bit.
我确实认为这有点“棘手”,但希望会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.