2016-04-13 28 views
1

我的感覺查詢:我想計算elasticsearch中特定字段的文檔數量。我使用感

GET testindexnew1/_search 
{ 
    "fields": ["T","U", "UD", "UE", "gsaentity_executives","extension.viewport","extension.google-site-verification","extension.og:description"], 
    "query": { 
    "bool": { 
     "should": [ 
     { "match": { "gsaentity_executives": "firstname lastname" }}, 
     { "match": { "gsaentity_executives": "firstname2 lastname2" }} 
     ] 
    } 
    }, 
"size": 10, 

    "aggs": { 
    "tags": { 
     "significant_terms": { 
     "field": "newContent" 
     } 
    } 
    }, 

    "highlight" : { 
     "fields" : { 
      "newContent" : {"fragment_size" : 150, "number_of_fragments" : 1} 
     } 
    } 
} 

我使用匹配查詢匹配高管的名字「姓」和「FIRSTNAME2 lastname2」。另外,我使用術語聚合來返回文檔數量。結果我得到的樣子:

{ 
    "took": 31, 
    "timed_out": false, 
    "_shards": { 
    "total": 1, 
    "successful": 1, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 6, 
    "max_score": 4.021257, 
    "hits": [ 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/blog", 
     "_score": 4.021257, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/blog" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Search and Big Data Insights - Search Technologies Blog" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/blog" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname lastname, firstname2 lastname2" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/blog" 
      ], 
      "extension.og:description": [ 
      "Search Technologies is the largest IT services company dedicated to enterprise search and big data implementation, consulting and managed Services" 
      ] 
     } 
     }, 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/executive-team", 
     "_score": 2.0106285, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/executive-team" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Search Technologies Executive Team" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/executive-team" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname lastname, firstname2 lastname2" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/executive-team" 
      ], 
      "extension.og:description": [ 
      "Our executive team is the most experienced in the search and analytics business with an average of more than 18 years experience" 
      ] 
     } 
     }, 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/solr-hadoop-integration", 
     "_score": 1.3831896, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/solr-hadoop-integration" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Solr | Hadoop Integration" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/solr-hadoop-integration" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname2 lastname2" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/solr-hadoop-integration" 
      ], 
      "extension.og:description": [ 
      "Solr is the natural choice for searching over Hadoop data. Search Technologies is the leading IT services company dedicated to implementing enterprise search and unstructured big data applications." 
      ] 
     } 
     }, 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/news-adecco-improves-recruiter-efficiency-with-search", 
     "_score": 1.1300961, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/news-adecco-improves-recruiter-efficiency-with-search" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Search Technologies Helps Adecco Group Significantly Improve Recruiter Efficiency" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/news-adecco-improves-recruiter-efficiency-with-search" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname lastname" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/news-adecco-improves-recruiter-efficiency-with-search" 
      ], 
      "extension.og:description": [ 
      "Custom Search and Match application based on Cloudera and Solr improves Adecco's recruiters' response times and fill rates." 
      ] 
     } 
     }, 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/search-big-data-videos", 
     "_score": 1.1300961, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/search-big-data-videos" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Enterprise Search & Big Data Videos" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/search-big-data-videos" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname lastname" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/search-big-data-videos" 
      ], 
      "extension.og:description": [ 
      "A collection of videos discussing enterprise search and big data concepts, business applications, and real-world case stories." 
      ] 
     } 
     }, 
     { 
     "_index": "testindexnew1", 
     "_type": "SearchTech", 
     "_id": "http://www.searchtechnologies.com/faq", 
     "_score": 1.1300961, 
     "fields": { 
      "U": [ 
      "http://www.searchtechnologies.com/faq" 
      ], 
      "extension.google-site-verification": [ 
      "jP1bIfjuuyZUYfTkYc_O6ZlTHxCm07voTDcMk72Z8oQ" 
      ], 
      "T": [ 
      "Search Technologies: Frequently Asked Questions (FAQ)" 
      ], 
      "UD": [ 
      "http://www.searchtechnologies.com/faq" 
      ], 
      "extension.viewport": [ 
      "width=device-width, initial-scale=1, maximum-scale=1" 
      ], 
      "gsaentity_executives": [ 
      "firstname lastname" 
      ], 
      "UE": [ 
      "http://www.searchtechnologies.com/faq" 
      ], 
      "extension.og:description": [ 
      "Search Technologies FAQ: Where did Search Technologies come from?   What are our key differentiators?  How do our customers view us?" 
      ] 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "tags": { 
     "doc_count": 6, 
     "buckets": [ 
     { 
      "key": "lastname", 
      "doc_count": 5, 
      "score": 13.055555555555557, 
      "bg_count": 5 
     }, 
     { 
      "key": "firstname", 
      "doc_count": 5, 
      "score": 13.055555555555557, 
      "bg_count": 5 
     }, 
     { 
      "key": "ceo", 
      "doc_count": 5, 
      "score": 13.055555555555557, 
      "bg_count": 5 
     }, 
     { 
      "key": "lastname2", 
      "doc_count": 3, 
      "score": 7.833333333333333, 
      "bg_count": 3 
     }, 
     { 
      "key": "candidates", 
      "doc_count": 4, 
      "score": 6.7407407407407405, 
      "bg_count": 6 
     }, 
     { 
      "key": "retention", 
      "doc_count": 3, 
      "score": 5.75, 
      "bg_count": 4 
     }, 
     { 
      "key": "firstname2", 
      "doc_count": 3, 
      "score": 5.75, 
      "bg_count": 4 
     }, 
     { 
      "key": "known", 
      "doc_count": 3, 
      "score": 5.75, 
      "bg_count": 4 
     }, 
     { 
      "key": "it’s", 
      "doc_count": 3, 
      "score": 5.75, 
      "bg_count": 4 
     }, 
     { 
      "key": "said", 
      "doc_count": 3, 
      "score": 5.75, 
      "bg_count": 4 
     } 
     ] 
    } 
    } 
} 

你可以從我得到的文檔計數姓氏,FIRSTNAME2,lastname2等單獨的結果看。

"aggregations": { 
    "tags": { 
     "doc_count": 6, 
     "buckets": [ 
     { 
      "key": "lastname", 
      "doc_count": 5, 
      "score": 13.055555555555557, 
      "bg_count": 5 
     }, 
     { 
      "key": "firstname", 
      "doc_count": 5, 
      "score": 13.055555555555557, 
      "bg_count": 5 
     }, 

我想我的查詢數整個高管的名字「姓」,「FIRSTNAME2 lastname2」等文件,但我得到的文件爲行政姓和名分開計算。

我是ES新手,所以我可能錯過了一些東西。謝謝你的幫助!

回答

0

您需要類似於以下的領域進行彙總的自定義分析和映射:

{ 
    "settings": { 
    "analysis": { 
     "tokenizer": { 
     "comma": { 
      "type": "pattern", 
      "pattern": "," 
     } 
     }, 
     "analyzer": { 
     "comma": { 
      "type": "custom", 
      "tokenizer": "comma", 
      "filter": [ 
      "trim" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "SearchTech": { 
     "properties": { 
     "gsaentity_executives": { 
      "type": "string", 
      "fields": { 
      "comma_separated": { 
       "type": "string", 
       "analyzer": "comma" 
      } 
      } 
     } 

和聚合:

"aggs": { 
    "tags": { 
     "terms": { 
     "field": "gsaentity_executives.comma_separated", 
     "size": 10 
     } 
    } 
    } 
+0

它的工作。非常感謝。 – Rose

相關問題