2016-01-11 58 views
1

我的查詢:如何Elasticsearch聚集和源只返回匹配的文本太

POST /testqueryidx/testQuery/_search 
{ 
    "size" : 10, 
    "query" : { 
    "bool" : { 
     "must" : [ { 
     "multi_match": { 
      "query": "sales*", 
      "fields": ["skills"] 
     } 
    }, { 
      "query_string" : { 
      "query" : "jay12", 
      "fields" : [ "idNum" ] 
     } 
     } ] 
    } 
    }, 
"aggregations" : { 
    "aggs" : { 
     "terms" : { 
      "field" : "skills_sort", 
      "size" : 0, 
      "order" : { 
       "_term" : "asc" 
       } 
      } 
     } 
    } 
} 

查詢結果:

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
}, 
    "hits": { 
    "total": 1, 
    "max_score": 0.9734945, 
    "hits": [ 
    { 
    "_index": "testqueryidx", 
    "_type": "testQuery", 
    "_id": "56909fbdaecb813e8c64e1e8", 
    "_score": 0.9734945, 
    "_source": { 
     "skills": [ 
      "Account Management", 
      "Sales force", 
      "Adobe Creative Suite" 
     ], 
     "_id": "56909fbdaecb813e8c64e1e8", 
     "idNum": "jay12" 
    } 
    } 
] 
}, 
    "aggregations": { 
    "aggs": { 
    "doc_count_error_upper_bound": 0, 
    "sum_other_doc_count": 0, 
    "buckets": [ 
    { 
     "key": "Account Management", 
     "doc_count": 1 
    }, 
    { 
     "key": "Adobe Creative Suite", 
     "doc_count": 1 
    }, 
    { 
     "key": "Sales force", 
     "doc_count": 1 
    } 
    ] 
    } 
} 
} 

在這裏,我搜索了場技能關鍵字銷售和我匹配的文檔。您可以在下面看到一個匹配的樣品:

"skills": [ 
      "Account Management", 
      "Sales force", 
      "Adobe Creative Suite" 
      ], 

但我不希望在查詢結果中「賬戶管理」和「Adobe Creative Suite的」,以及查詢聚合。見下面聚集的結果:

"buckets": [ 
     { 
      "key": "Account Management", 
      "doc_count": 1 
     }, 
     { 
      "key": "Adobe Creative Suite", 
      "doc_count": 1 
     }, 
     { 
      "key": "Sales force", 
      "doc_count": 1 
     } 
    ] 

同樣我也不想上述「關鍵」:作爲我搜索只對聚集結果「Adobe Creative Suite的」:「帳戶管理」和「鍵」銷售*。

我上面有突出的文本,因爲我的文檔中的技能領域擁有所有這三個技能,但我感興趣的只是搜索的關鍵字。請幫助我,如果有人有這個解決方案

回答

1

我認爲這是可以實現的。您可以使用include進行條款彙總,只會給您sales*。至於查詢而言,你必須使用highlight得到只有特定價值的任何領域的,您可以用source filtering檢索等。這是我的設置

POST only_index 
{ 
    "mappings": { 
    "my_type": { 
     "properties": { 
     "skills": { 
      "type": "string", 
      "fields": { 
      "raw": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     }, 
     "idNum" : { 
      "type" : "string" 
     } 
     } 
    } 
    } 
} 

索引您的文檔後,我運行以下查詢

GET only_index/_search 
{ 
    "size": 10, 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "multi_match": { 
      "query": "sales*", 
      "fields": [ 
       "skills" 
      ] 
      } 
     }, 
     { 
      "query_string": { 
      "query": "jay12", 
      "fields": [ 
       "idNum" 
      ] 
      } 
     } 
     ] 
    } 
    }, 
    "aggregations": { 
    "aggs": { 
     "terms": { 
     "field": "skills.raw", 
     "size": 0, 
     "include": "(?i)sales.*", 
     "order": { 
      "_term": "asc" 
     } 
     } 
    } 
    }, 
    "highlight": { 
    "fields": { 
     "skills": {} 
    } 
    }, 
    "_source": [ 
    "idNum" 
    ] 
} 

我已經使用(?i)標誌case insensitive match。這是我得到

"hits": { 
     "total": 1, 
     "max_score": 0.29834434, 
     "hits": [ 
     { 
      "_index": "only_index", 
      "_type": "my_type", 
      "_id": "1", 
      "_score": 0.29834434, 
      "_source": { 
       "idNum": "jay12" 
      }, 
      "highlight": { 
       "skills": [ 
        "<em>Sales</em> force" 
       ] 
      } 
     } 
     ] 
    }, 
    "aggregations": { 
     "aggs": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": "Sales force", 
       "doc_count": 1 
      } 
     ] 
     } 
    } 

希望這可以幫助!