C# 弹性搜索嵌套中两个字段的条件排序
如果需要,我已输入数据 类型是A,然后我需要按价格排序1 如果类型是B,那么我需要按价格2排序 那么在c#NEST中,我们如何为它编写查询呢C# 弹性搜索嵌套中两个字段的条件排序,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,如果需要,我已输入数据 类型是A,然后我需要按价格排序1 如果类型是B,那么我需要按价格2排序 那么在c#NEST中,我们如何为它编写查询呢 { "test": { "mappings": { "listings": { "properties": { "productname": { "type": "string" }, "ptype": { "
{
"test": {
"mappings": {
"listings": {
"properties": {
"productname": {
"type": "string"
},
"ptype": {
"type": "string"
},
"price1": {
"type": "float"
},
"price2": {
"type": "float"
}
}
}
}
}
}
你在找这样的东西吗
_elasticClient.SearchAsync<ListingsModel>(s => s
.Type(type)
.Sort(o =>
{
if(type == "A")
return o.OnField("price1");
else
return o.OnField("price2");
})
//rest of query
\u elasticClient.SearchAsync(s=>s)
.类型(类型)
.Sort(o=>
{
如果(类型=“A”)
返回o.OnField(“价格1”);
其他的
返回o.OnField(“价格2”);
})
//剩下的问题
我能做的最好的事情就是这样
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; }
}
类程序
{
静态void Main(字符串[]参数)
{
var random=新的random();
var uri=新的uri(“http://localhost.fiddler:9200");
var indexName=“测试指数”;
ElasticClient db=新的ElasticClient(uri);
db.DeleteIndex(indexName);
foreach(Enumerable.Range(0,10)中的var项。选择(i=>newa{Price1=random.NextDouble()*1000}))
{
db.Index(item,inx=>inx.Index(indexName));
}
foreach(Enumerable.Range(0,10)中的var项。选择(i=>newb{Price2=random.NextDouble()*1000}))
{
db.Index(item,inx=>inx.Index(indexName));
}
//db.IndexMany(Enumerable.Range(0,10).Select(i=>newa{Price1=random.NextDouble()*1000}),indexName);使用此选项时,由于索引后查询速度过快,因此没有返回任何内容
//IndexMany(Enumerable.Range(0,10).Select(i=>newb{Price2=random.NextDouble()*1000}),indexName);
var data=db.Search(q=>
q、 索引(indexName)
.尺寸(20)
.Type(“”)
.Sort(s=>s.Script(scd=>scd
.类型(“编号”)
.Script(sc=>sc
//.Inline(@“doc['price1']?doc['price1']:doc['price2'])如果对象B中没有price1字段,则可以使用此字段,并且不需要类型索引
.Inline(@“doc['typeIndex']==0?doc['price1']:doc['price2'])//typeIndex必须是一个数字lucene不支持字符串文字
朗先生(“表达”)
).顺序(排序降序));
控制台。写入线(“完成”);
Console.ReadLine();
}
}
[ElasticsearchType(Name=“A”)]
公共A类
{
[数字]
公共int类型索引{get{return 0;}}
[数字]
公共双价1{get;set;}
}
[ElasticsearchType(Name=“B”)]
公共B级
{
[数字]
公共int类型索引{get{return 1;}}
[数字]
公共双价2{get;set;}
}
您可以使用groovy,但我不知道它的性能有多好。默认情况下,expression是打开的,他们使用。我确实认为它有点“hacky”,但希望有点帮助。elasticsearch index中有两种类型?我有一个字段“list type”,可以是a或B。您可以共享您的索引映射和示例文档吗?