elasticsearch中的mapping映射配置示例
比如要搭建個中文新聞信息的搜索引擎,新聞有"標題"、"內容"、"作者"、"類型"、"發布時間"這五個字段;
我們要提供"標題和內容的檢索"、"排序"、"高亮"、"統計"、"過濾"等一些基本功能。
ES提供了smartcn的中文分詞插件,測試的話建議使用IK分詞插件。
內容中properties對應mapping里的內容,里面5個字段。
type指出字段類型、內容、標題字段要進行分詞和高亮因此要設置分詞器和開啟term_vector。
{
? "news": {
??? "properties": {
????? "content": {#內容
??????? "type": "string",? #字段類型
??????? "store": "no", #是否存儲
??????? "term_vector": "with_positions_offsets",#開啟向量,用于高亮
??????? "index_analyzer": "ik",#索引時分詞器
??????? "search_analyzer": "ik"#搜索時分詞器
????? },
????? "title": {
??????? "type": "string",
??????? "store": "no",
??????? "term_vector": "with_positions_offsets",
??????? "index_analyzer": "ik",
??????? "search_analyzer": "ik",
??????? "boost": 5
????? },
????? "author": {
??????? "type": "string",
??????? "index": "not_analyzed"#該字段不分詞
????? },
????? "publish_date": {
??????? "type": "date",
??????? "format": "yyyy/MM/dd",
??????? "index": "not_analyzed"#該字段不分詞
????? },
????? "category": {
??????? "type": "string",
??????? "index": "not_analyzed"#該字段不分詞
????? }
??? }
? }
}
查詢示例:內容包括幾個部分:
分頁:from/size、字段:fields、排序sort、查詢:query、過濾:filter、高亮:highlight、統計:facet
{
? "from": 0,
? "size": 10,
? "fields": [
??? "title",
??? "content",
??? "publish_date",
??? "category",
??? "author"
? ],
? "sort": [
??? {
????? "publish_date": {
??????? "order": "asc"
????? }
??? },
??? "_score"
? ],
? "query": {
??? "bool": {
????? "should": [
??????? {
????????? "term": {
??????????? "title": "中國"
????????? }
??????? },
??????? {
????????? "term": {
??????????? "content": "中國"
????????? }
??????? }
????? ]
??? }
? },
? "filter": {
??? "range": {
????? "publish_date": {
??????? "from": "2010/07/01",
??????? "to": "2010/07/21",
??????? "include_lower": true,
??????? "include_upper": false
????? }
??? }
? },
? "highlight": {
??? "pre_tags": [
????? "<tag1>",
????? "<tag2>"
??? ],
??? "post_tags": [
????? "</tag1>",
????? "</tag2>"
??? ],
??? "fields": {
????? "title": {},
????? "content": {}
??? }
? },
? "facets": {
??? "cate": {
????? "terms": {
??????? "field": "category"
????? }
??? }
? }
}
結果包含需要的幾個部分。
值得注意的是,facet的統計是命中的結果進行統計,filter是對結果進行過濾,filter不會影響facet,如果要統計filter掉的的就要使用filter facet。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
