2015-08-25 59 views
1

我有一個來自gui的json查詢。它已經過濾了查詢部分。我想用日期範圍過濾器擴展過濾的查詢如何使用嵌套來實現。我嘗試了一種方法(QueryTest方法),如下所示,但失敗了。如何更新.net中現有的彈性搜索查詢嵌套

public SearchDescriptor<dynamic> GetSearchDescriptor(string queryJson = null) 
    { 
     var searchDesc = new SearchDescriptor<dynamic>(); 

     if (string.IsNullOrEmpty(queryJson)) 
     { 
      return searchDesc; 
     } 

     searchDesc = _nestClient.Serializer.Deserialize<SearchDescriptor<dynamic>>(new MemoryStream(Encoding.UTF8.GetBytes(queryJson))); 

     return searchDesc; 
    } 

public string ConvertToJsonQuery(SearchDescriptor<dynamic> searchDescriptor) 
    { 
     return Encoding.UTF8.GetString(_nestClient.Serializer.Serialize(searchDescriptor, SerializationFormatting.Indented)); 
    } 

public void QueryTest() 
    { 
     var query = @"{  
         ""query"": { 
         ""filtered"": { 
          ""filter"": { 
          ""bool"": { 
           ""must"": [ 
           { 
            ""term"": { 
            ""display_name"": ""NBLEYLEK$"" 
            } 
           } 
           ] 
          } 
          } 
         } 
         } 
        }"; 

     var searchDescriptor = GetSearchDescriptor(query).AllTypes(); 

     var queryLastA = ConvertToJsonQuery(searchDescriptor); 

     searchDescriptor.Query(x => x.Filtered(y => y.Filter(z => z.Range(t => t.OnField("sign_time").Greater(DateTime.Now))))); 

     var queryLastB = ConvertToJsonQuery(searchDescriptor); 
    } 

如果我看queryLastA和queryLastB的值,我看爲:

queryLastA =>

{  
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "display_name": "NBLEYLEK$" 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

queryLastB =>

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "range": { 
      "sign_time": { 
      "gt": "2015-08-25T16:13:56.694" 
      } 
     } 
     } 
    } 
    } 
} 

所需的查詢是:

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "display_name": "NBLEYLEK$" 
       } 
      }, 
      { 
       "range": { 
       "sign_time": { 
        "gt": "2015-08-25T16:13:56.694" 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

我期望從巢之下追加舊新的過濾器,但它忽略了舊的過濾器(帶queryLastA比較queryLastB)。有什麼建議?

回答

0

目前嵌套,如果我們給新的過濾器,它覆蓋舊的過濾器。我有同樣的問題。我對查詢進行了字符串操作,並將其轉換爲c#類並使用nest從頭開始應用過濾器。這爲我解決了。

+0

您可以詳細說明您的工作解決方案嗎? – serefbilge