全文检索
准备
使用全文检索前先安装elasticsearch
插件。
配置
进入 后台——模块管理——CMS配置
,搜索
选项卡引擎选项切换到Elasticsearch
,填写索引名称,比如cms
。
进入后台——模块管理
,打开Elasticsearch配置页,填写Hosts
(如:127.0.0.1:9200)。
进入后台——Elasticsearch全文检索
,添加一项配置,如(索引必须于配置中的索引一致)
Mapping示例:
{
"properties": {
"id": { "type": "integer" },
"title": { "type": "text", "analyzer": "ik_max_word" },
"description": { "type": "text", "analyzer": "ik_max_word" },
"url": { "type": "keyword" },
"type": { "type": "keyword" },
"group": { "type": "keyword" },
"group_name": { "type": "keyword" },
"keywords": { "type": "text", "analyzer": "ik_max_word" },
"content": { "type": "text", "analyzer": "ik_max_word" },
"views": { "type": "long" },
"image": { "type": "keyword" },
"author": { "type": "keyword" },
"author_avatar": { "type": "keyword" },
"created_at": { "type": "integer" }
}
}
Query示例
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "{$payload->params['keyword']}",
"fields": [
"title",
"description",
"content"
]
}
}
{if !empty($payload->params['type'])},{
"match": {
"type": "{$payload->params['type']}"
}
}{/if}
]
}
},
"highlight": {
"fields": {
"title": {
"pre_tags": "<span class='text-danger text-bold'>",
"post_tags": "</span>"
},
"description": {
"pre_tags": "<span class='text-danger text-bold'>",
"post_tags": "</span>"
}
}
}
}
最后初始化数据即可。
<h3 id="h3-u51C6u5907"><a name="准备" class="reference-link"></a><span class="header-link octicon octicon-link"></span>准备</h3><p>使用全文检索前先安装<a href="https://www.yiadmin.net/appstore/show/es.html" title="elasticsearch"><code>elasticsearch</code></a>插件。</p>
<h3 id="h3-u914Du7F6E"><a name="配置" class="reference-link"></a><span class="header-link octicon octicon-link"></span>配置</h3><p>进入 <code>后台——模块管理——CMS配置</code> ,<code>搜索</code>选项卡引擎选项切换到<code>Elasticsearch</code>,填写索引名称,比如<code>cms</code>。<br>进入<code>后台——模块管理</code>,打开Elasticsearch配置页,填写<code>Hosts</code>(如:127.0.0.1:9200)。<br>进入<code>后台——Elasticsearch全文检索</code>,添加一项配置,如(索引必须于配置中的索引一致)<br><img src="/storage/202209/23/4d06f863e0e7682ad66974834ae1574f7e2b13ef.png" alt="图片标注" title="图片说明"><br>Mapping示例:
</p><pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> </span><span class="str">"properties"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"id"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"integer"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"title"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"analyzer"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"ik_max_word"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L4"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"analyzer"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"ik_max_word"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"url"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L7"><code><span class="pln"> </span><span class="str">"group"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">"group_name"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L9"><code><span class="pln"> </span><span class="str">"keywords"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"analyzer"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"ik_max_word"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L0"><code><span class="pln"> </span><span class="str">"content"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"analyzer"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"ik_max_word"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L1"><code><span class="pln"> </span><span class="str">"views"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"long"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"image"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"author"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L4"><code><span class="pln"> </span><span class="str">"author_avatar"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"keyword"</span><span class="pln"> </span><span class="pun">},</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"created_at"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"integer"</span><span class="pln"> </span><span class="pun">}</span></code></li><li class="L6"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol></pre><p>Query示例</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="pun">{</span></code></li><li class="L1"><code><span class="pln"> </span><span class="str">"query"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"bool"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"must"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"multi_match"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"query"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"{$payload->params['keyword']}"</span><span class="pun">,</span></code></li><li class="L7"><code><span class="pln"> </span><span class="str">"fields"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">"title"</span><span class="pun">,</span></code></li><li class="L9"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">,</span></code></li><li class="L0"><code><span class="pln"> </span><span class="str">"content"</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L2"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">{</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">!</span><span class="pln">empty</span><span class="pun">(</span><span class="pln">$payload</span><span class="pun">-></span><span class="kwd">params</span><span class="pun">[</span><span class="str">'type'</span><span class="pun">])},{</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"match"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"{$payload->params['type']}"</span></code></li><li class="L7"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L8"><code><span class="pln"> </span><span class="pun">}{/</span><span class="kwd">if</span><span class="pun">}</span></code></li><li class="L9"><code><span class="pln"> </span><span class="pun">]</span></code></li><li class="L0"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">},</span></code></li><li class="L2"><code><span class="pln"> </span><span class="str">"highlight"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">"fields"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L4"><code><span class="pln"> </span><span class="str">"title"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln"> </span><span class="str">"pre_tags"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"<span class='text-danger text-bold'>"</span><span class="pun">,</span></code></li><li class="L6"><code><span class="pln"> </span><span class="str">"post_tags"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"</span>"</span></code></li><li class="L7"><code><span class="pln"> </span><span class="pun">},</span></code></li><li class="L8"><code><span class="pln"> </span><span class="str">"description"</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L9"><code><span class="pln"> </span><span class="str">"pre_tags"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"<span class='text-danger text-bold'>"</span><span class="pun">,</span></code></li><li class="L0"><code><span class="pln"> </span><span class="str">"post_tags"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"</span>"</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L2"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L4"><code><span class="pun">}</span></code></li></ol></pre><p>最后初始化数据即可。</p>