1. I use the xunsearch search function to build a document index for two tables, one is the article table and the other is the user table
2. My configuration file has two user.ini article.ini contents as follows
project.name = user
project.default_charset = utf-8
server.index = xxx:8383
server.search = xxx:8384
[id]
type = id
[nickname]
index = self
phrase = yes
[avatar]
[article_numbers]
type = numeric
[follow_numbers]
type = numeric
[fans_numbers]
type = numeric
[createtime]
type = numeric
project.name = article
project.default_charset = utf-8
server.index = xxx:8383
server.search = xxx:8384
[id]
type = id
[author]
type = string
[user_id]
[category_id]
index = self
tokenizer = full
[category]
type = both
[title]
type = title
[cover_url]
type = both
[content]
type = body
[reply_numbers]
type = numeric
[star_numbers]
type = numeric
[read_numbers]
type = numeric
[createtime]
type = numeric
3. Create an indexed document
/**
*
* @param $type string (update)(add)
* @param $subject string (article)(user)
* @return bool
* @throws \XSException
* @throws \think\exception\DbException
*/
public function makedoc()
{
$request = Request::instance();
$type = $request->param("type");
$subject = $request->param("subject");
switch ($subject){
case "article":
$map = [
"status" => ExtraConst::NORMAL
];
$order = "collect_numbers desc,reply_numbers desc,star_numbers desc,read_numbers desc,id desc";
$articles = Article::all(function ($query) use ($map, $order) {
$query->where($map)->order($order);
});
if (!$articles) {
return false;
}
$xs = new \XS(file_get_contents(ROOT_PATH . "docs" . DIRECTORY_SEPARATOR . "article.ini"));
$doc = new \XSDocument();
foreach ($articles as $article) {
$doc->setFields([
"id" => $article->id,
"author" => $article->author,
"user_id" => $article->user_id,
"category_id" => $article->category_id,
"category" => $article->category,
"title" => $article->title,
"content" => $article->content,
"cover_url" => $article->cover_url,
"reply_numbers" => $article->reply_numbers,
"star_numbers" => $article->star_numbers,
"read_numbers" => $article->read_numbers,
"createtime" => $article->createtime,
]);
if ($type === "add") {
$result = $xs->index->add($doc);
} elseif ($type === "update") {
$result = $xs->index->update($doc);
}
}
break;
case "user":
$map = [
"status" => "normal"
];
$order = "id desc";
$users = User::all(function ($query) use ($map, $order){
$query->where($map)->order($order);
});
if (!$users) {
return false;
}
$xs = new \XS(file_get_contents(ROOT_PATH . "docs" . DIRECTORY_SEPARATOR . "user.ini"));
$doc = new \XSDocument();
foreach ($users as $user){
$doc->setFields([
"id" => $user->id,
"nickname" => $user->nickname,
"avatar" => $user->avatar,
"article_numbers" => $user->article_numbers,
"follow_numbers" => $user->follow_numbers,
"fans_numbers" => $user->fans_numbers,
"createtime" => $user->createtime,
]);
if ($type === "add") {
$result = $xs->index->add($doc);
} elseif ($type === "update") {
$result = $xs->index->update($doc);
}
}
break;
}
if ($result) {
echo "--------success ----------";
}
}
4. Search for content based on keywords
/**
*
* @return array
* @throws \XSException
*/
public function search()
{
$request = Request::instance();
$query = $request->post("key");
$article_ini = file_get_contents(ROOT_PATH . "docs" . DIRECTORY_SEPARATOR . "article.ini");
$user_ini = file_get_contents(ROOT_PATH . "docs" . DIRECTORY_SEPARATOR . "user.ini");
//
$a_xs = new \XS($article_ini);
$a_search = $a_xs->search;
$a_search->setQuery($query)->setSort("createtime", true)->setLimit(20, 0);
$a_docs = $a_search->search();
$a_count = $a_search->count();
//
$u_xs = new \XS($user_ini);
$u_search = $u_xs->search;
$u_search->setQuery($query)->setSort("createtime", true)->setLimit(20, 0);
$u_docs = $u_search->search();
$u_count = $u_search->count();
dump($a_docs);
dump($u_docs);die;
//
5. Use postman to test the interface
array(3) {
["code"] => int(1024)
["users"] => array(0) {
}
["articles"] => array(4) {
[1] => array(10) {
["id"] => string(10) "jo1gn7l9b8"
["title"] => string(36) ""
["author"] => string(22) "-"
["user_id"] => string(10) "xenp87gyvk"
["read_numbers"] => string(3) "123"
["reply_numbers"] => string(4) "3445"
["star_numbers"] => string(4) "1322"
["cover_url"] => string(41) "/uploads/cover/20180423/5add3c3664db9.jpg"
["content"] => string(57) ""
["createtime"] => string(11) "04-11 18:52"
}
[2] => array(10) {
["id"] => string(10) "jo1gn7l9b8"
["title"] => string(36) ""
["author"] => string(22) "-"
["user_id"] => string(10) "xenp87gyvk"
["read_numbers"] => string(3) "123"
["reply_numbers"] => string(4) "3445"
["star_numbers"] => string(4) "1322"
["cover_url"] => string(41) "/uploads/cover/20180423/5add3c3664db9.jpg"
["content"] => string(57) ""
["createtime"] => string(11) "04-11 18:52"
}
6. I tested many times
article keywords can be easily searched out, and the user table of the nickname data, how can not be found,
I try to linux / usr/local/xunsearch/data/db to find index documents, there are user and article documents, what is wrong with my code? I have debugged breakpoints in my UserModel, there is data, and the nickname keyword also exists.