2016-07-22 2 views
0

이 elasticsearch 쿼리는 원시 형식으로 완벽하게 작동하며 C# NEST 절로 바꾸는 데 문제가 있습니다.여러 개의 must 절이있는 Elasticsearch Nest 쿼리 bool 필터

이 원시 쿼리입니다 :

{ 
"query":{ 
     "constant_score":{ 
     "filter":{ 
      "bool":{ 
       "must":{ 
        "term":{ 
        "ingredients":"baking" 
        } 
       }, 
       "must":{ 
        "term":{ 
        "ingredients":"soda" 
        } 
       } 
      } 
     } 
     } 
    } 
} 

그리고 이것이 내가 C#을 NEST에서 일 것이라고 생각 것입니다 : 사용자는 X 값의 배열을 보낼 수 있습니다

public List<Recipe> FindByMultipleValues(string field, string[] values) { 
     List<string> vals = values.ToList(); 
     return client.Search<Recipe>(s => s 
      .Query(q => q 
       .Bool(fq => fq 
        .Filter(f => f 
         .Term(rec => rec.Ingredients, vals) 
        ) 
       ) 
      ) 
     ).Documents.ToList(); 
    } 

에 대한 것을 의미한다 각각의 값은이 있어야합니다 :

이 같은
"must":{ 
    "term":{ 
     "ingredients":"soda" 
     } 
    } 
+0

'bool' 쿼리의'must' 절은 배열입니다. 나는 두 번째'must' 절이 첫 번째 항목을 덮어 쓰게 될 것이라고 의심 할 것이다. NEST의 어떤 버전을 사용하고 있습니까? –

+0

최신 버전을 사용하고 있습니다. 2.3.x 나는 그것이 있다고 생각한다. – McBoman

답변

1

뭔가 작동합니다

,
var terms = new[] { "baking", "soda" }; 

client.Search<Recipe>(s => s 
    .Query(q => q 
     .ConstantScore(cs => cs 
      .Filter(csf => 
      { 
       var firstTerm = csf.Term(f => f.Ingredients, terms.First());   
       return terms.Skip(1).Aggregate(firstTerm, (query, term) => query && csf.Term(f => f.Ingredients, term)); 
      }) 
     ) 
    ) 
); 

이 절 mustbool 질의를 형성하도록 함께 에드 '그들 && 할 수 operator overloading for QueryContainer 활용

{ 
    "query": { 
    "constant_score": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "ingredients": { 
        "value": "baking" 
       } 
       } 
      }, 
      { 
       "term": { 
       "ingredients": { 
        "value": "soda" 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

수득한다.

+0

코드가 실행되지 않습니다. 성분을 찾을 수 없기 때문에 f => f.I.라는 단어를 사용할 수없는 것 같습니다. – McBoman

+0

이 예제는 여러분이'Ingredients'라는 속성을 가진'Recipe'라는 POCO 타입을 가지고 있다고 가정합니다. 모델에 맞게 조정해야합니다. –

+0

당신이 뭘했는지 알아 냈습니다. 도와 주셔서 감사합니다. 정말 잘됐다! – McBoman

관련 문제