2016-09-03 2 views
0

I '은행'Elasticsearch는 문서 내부에 배열 필드에 대해 정확히 쿼리 실패

[ 
    { 
     "_id": "1", 
     "name": "ICIC", 
     "balance": "$2,574.27", 
     "friends": [ 
      { 
       "roleid": 0, 
       "name": "Alana Shepard", 
       "isactive": true 
      }, 
      { 
       "roleid": 1, 
       "name": "Katheryn Hatfield", 
       "isactive": false 
      } 
     ] 
    }, 
    { 
     "_id": "2", 
     "name": "SBK", 
     "balance": "$2,346.44", 
     "friends": [ 
      { 
       "roleid": 0, 
       "name": "Hinton Kaufman", 
       "isactive": true 
      }, 
      { 
       "roleid": 1, 
       "name": "Miles Alford", 
       "isactive": true 
      } 
     ] 
    } 
] 

라는 elasticsearch 유형에서 다음과 같은 데이터가 지금은 문서 friends.roleid = 1과 친구를 가져 오기 위해 시도해야 .isactive = true. 다음과 같이 요청 된 DSL 쿼리는

{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "term": { 
         "friends.roleid": { 
          "value": 1 
         } 
        } 
       }, 
       { 
        "term": { 
         "friends.isactive": { 
          "value": true 
         } 
        } 
       } 
      ] 
     } 
    } 
} 

예상 결과가 _id = 2와 대상이되는 것이다. 그러나 실제 결과는 _id = 1과 _id = 2입니다. DSL 질의에서 문제가 무엇인지 찾아내는 데 도움이된다면 크게 환영 할 것입니다. 고맙습니다.

+0

'friends' 필드를'nested' 타입으로 선언해야합니다. 이유에 대해 알아 보려면 다음을 참조하십시오. https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-objects.html – Val

+0

Val 동일한 요청에 중첩 쿼리와 일반 dsl 쿼리를 작성하는 방법은 무엇입니까? –

+0

'bool/must'를 사용하여 중첩 된 쿼리 내에서'term' 쿼리를 결합했던 것과 같은 방법으로 그들을 결합 할 수 있습니다. – Val

답변

1

문제는 "친구"필드를 중첩 된 개체로 만드는 문제가 수정되었습니다. DSL 쿼리는 다음과 같이 재구성됩니다.

{ 
    "query": { 
     "nested": { 
      "path": "friends", 
      "query": { 
       "bool": { 
        "must": [ 
         { 
          "term": { 
           "friends.roleid": { 
            "value": 1 
           } 
          } 
         }, 
         { 
          "term": { 
           "friends.isactive": { 
            "value": "true" 
           } 
          } 
         } 
        ] 
       } 
      } 
     } 
    } 
} 

DSL 쿼리의 경로는 중첩 된 개체를 지정합니다. elasticsearch 문서 참조 https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html 많은 도움이 될 것입니다.