Elastic Search Nest中2个字段的条件排序

时间:2016-10-26 07:39:16

标签: c# elasticsearch nest

我输入了我的数据,如果 类型是A然后我需要按price1排序 如果type是B,那么我需要按price2排序,

所以在c#NEST中我们如何为此编写查询?

{
  "test": {
    "mappings": {
      "listings": {
        "properties": {
          "productname": {
            "type": "string"
          },
          "ptype": {
            "type": "string"
          },
          "price1": {
            "type": "float"
          },
          "price2": {
            "type": "float"
          }
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

这就是你想要的东西吗?

_elasticClient.SearchAsync<ListingsModel>(s => s
    .Type(type)
    .Sort(o => 
    {
        if(type == "A")
            return o.OnField("price1");
        else
            return o.OnField("price2");
    })
    //rest of query

答案 1 :(得分:0)

我能做的最好的事情是这样的

 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; }
    }

你可以用groovy来做,但我不知道它有多好。表达式默认打开,它们使用lucene。我认为这有点&#34; hacky&#34;但希望有所帮助。