elasticsearch中的mapping映射配置示例
比如要搭建個(gè)中文新聞信息的搜索引擎,新聞?dòng)?標(biāo)題"、"內(nèi)容"、"作者"、"類(lèi)型"、"發(fā)布時(shí)間"這五個(gè)字段;
我們要提供"標(biāo)題和內(nèi)容的檢索"、"排序"、"高亮"、"統(tǒng)計(jì)"、"過(guò)濾"等一些基本功能。
ES提供了smartcn的中文分詞插件,測(cè)試的話建議使用IK分詞插件。
內(nèi)容中properties對(duì)應(yīng)mapping里的內(nèi)容,里面5個(gè)字段。
type指出字段類(lèi)型、內(nèi)容、標(biāo)題字段要進(jìn)行分詞和高亮因此要設(shè)置分詞器和開(kāi)啟term_vector。
{
? "news": {
??? "properties": {
????? "content": {#內(nèi)容
??????? "type": "string",? #字段類(lèi)型
??????? "store": "no", #是否存儲(chǔ)
??????? "term_vector": "with_positions_offsets",#開(kāi)啟向量,用于高亮
??????? "index_analyzer": "ik",#索引時(shí)分詞器
??????? "search_analyzer": "ik"#搜索時(shí)分詞器
????? },
????? "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"#該字段不分詞
????? }
??? }
? }
}
查詢(xún)示例:內(nèi)容包括幾個(gè)部分:
分頁(yè):from/size、字段:fields、排序sort、查詢(xún):query、過(guò)濾:filter、高亮:highlight、統(tǒng)計(jì):facet
{
? "from": 0,
? "size": 10,
? "fields": [
??? "title",
??? "content",
??? "publish_date",
??? "category",
??? "author"
? ],
? "sort": [
??? {
????? "publish_date": {
??????? "order": "asc"
????? }
??? },
??? "_score"
? ],
? "query": {
??? "bool": {
????? "should": [
??????? {
????????? "term": {
??????????? "title": "中國(guó)"
????????? }
??????? },
??????? {
????????? "term": {
??????????? "content": "中國(guó)"
????????? }
??????? }
????? ]
??? }
? },
? "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"
????? }
??? }
? }
}
結(jié)果包含需要的幾個(gè)部分。
值得注意的是,facet的統(tǒng)計(jì)是命中的結(jié)果進(jìn)行統(tǒng)計(jì),filter是對(duì)結(jié)果進(jìn)行過(guò)濾,filter不會(huì)影響facet,如果要統(tǒng)計(jì)filter掉的的就要使用filter facet。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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