2017-10-12 54 views

回答

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 
      } 
     } 
     } 
    ] 
    } 
} 

希望這會給你任何具體的得分邏輯你需要的要點