2016-07-18 3 views
0

최대 가격이 100 달러 인 신발을 찾고 있다고 가정 해 봅시다. 신발에는 '가격'필드가 있지만 'discounted_price'필드가있을 수도 있습니다.ElasticSearch의 두 필드에서 필드 중 하나만 표시되는 경우가 있습니다.

{ 
    "_index": "shoes", 
    "_type": "shoe", 
    "_id": "1", 
    "_score": 1, 
    "_source": { 
     "price": 150, 
     "discounted_price": 90 
    } 
} 
{ 
    "_index": "shoes", 
    "_type": "shoe", 
    "_id": "2", 
    "_score": 2, 
    "_source": { 
     "price": 100, 
     "discounted_price": null 
    } 
} 

가 어떻게이 경우에는 두 문서를 반환 $ 100 최대 가격에 대한 쿼리를 만들 것입니다 : 예를 들어

?

답변

1

이 뭔가를 시도 할 수 있습니다 :

POST /shoes/shoe/_search 
{ 
    "query": { 
    "bool": { 
     "minimum_should_match": 1, 
     "should": [ 
     { 
      "bool": { 
      "filter": [ 
       { 
       "range": { 
        "discounted_price": { 
        "lte": 100 
        } 
       } 
       }, 
       { 
       "exists": { 
        "field": "discounted_price" 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "filter": [ 
       { 
       "range": { 
        "price": { 
        "lte": 100 
        } 
       } 
       }, 
       { 
       "missing": { 
        "field": "discounted_price" 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

UPDATE : 대신 다음과 같은 쿼리를 사용할 수 있습니다 2.0 이전의 ES의 버전

:

POST /shoes/shoe/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "minimum_should_match": 1, 
      "should": [ 
      { 
       "bool": { 
       "must": [ 
        { 
        "range": { 
         "discounted_price": { 
         "lte": 100 
         } 
        } 
        }, 
        { 
        "exists": { 
         "field": "discounted_price" 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "bool": { 
       "must": [ 
        { 
        "range": { 
         "price": { 
         "lte": 100 
         } 
        } 
        }, 
        { 
        "missing": { 
         "field": "discounted_price" 
        } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 
+0

가 400 오류가 발생을 붙여 넣기에는 너무 길어 ... QueryParsingException [[shoes] [missing]에 대한 쿼리가 등록되지 않았습니다.]; – 2083

+0

... QueryParsingException [[shoes] bool 쿼리가 [filter]]를 지원하지 않습니다. – 2083

+0

어떤 ES 버전이 있습니까? – Val

관련 문제