2017-10-12 1 views
0

주어진 필드에 값의 집계에 정렬 내 인덱스elasticsearch

field1:{key:value} 

에서 다음 필드를 가지고하는 것은 가능 필드 1의 값의 합에 내 쿼리를 정렬하는 것입니다. 감사합니다.

답변

0

미리 필드를 알고 있다고 가정 할 때 한 가지 방법이 있습니다. 필드를 와일드 카드해야하는 경우 약간의 세부 조정이 가능해야합니다. 이것은 중첩 된 유형의 형제 필드가 숫자라고 가정합니다.

예 매핑 :

"test": { 
    "mappings": { 
     "type1": { 
     "properties": { 
      "field1": { 
      "properties": { 
       "key1": { 
       "type": "integer" 
       }, 
       "key2": { 
       "type": "integer" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 

기본 결과 : 스크립트

"hits": { 
    "total": 2, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "type1", 
     "_id": "AV8O7956gIcGI2d5A_5g", 
     "_score": 1, 
     "_source": { 
      "field1": { 
      "key1": 11, 
      "key2": 17 
      } 
     } 
     }, 
     { 
     "_index": "test", 
     "_type": "type1", 
     "_id": "AV8O78FqgIcGI2d5A_5f", 
     "_score": 1, 
     "_source": { 
      "field1": { 
      "key1": 5, 
      "key2": 6 
      } 
     } 
     } 
    ] 
    } 

쿼리 :이 경우 적어도 부정적인 최고 점수 (로 가장 낮은 점수를 복용

GET /test/_search 
{ 
    "query": { 
    "function_score": { 
     "query": { 
     "match_all": {} 
     }, 
     "functions": [ 
     { 
      "script_score": { 
      "script": "return (doc['field1.key1'].value + doc['field1.key2'].value) * -1" 
      } 
     } 
     ] 
    } 
    } 
} 

논리) :

{ 
    "took": 18, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "skipped": 0, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": -11, 
    "hits": [ 
     { 
     "_index": "test", 
     "_type": "type1", 
     "_id": "AV8O78FqgIcGI2d5A_5f", 
     "_score": -11, 
     "_source": { 
      "field1": { 
      "key1": 5, 
      "key2": 6 
      } 
     } 
     }, 
     { 
     "_index": "test", 
     "_type": "type1", 
     "_id": "AV8O7956gIcGI2d5A_5g", 
     "_score": -28, 
     "_source": { 
      "field1": { 
      "key1": 11, 
      "key2": 17 
      } 
     } 
     } 
    ] 
    } 
} 

희망 사항에 따라 원하는 득점 논리의 요지가 표시되기를 바랍니다.