简体   繁体   English

如何使用锈柴油做全文查询

[英]how to using rust diesel to do the full text query

I am trying to using diesel diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] } to do a full text query, this is the sql command look like:我正在尝试使用柴油diesel = { version = "1.4.8", features = ["postgres","64-column-tables","chrono","serde_json"] }进行全文查询,这是sql 命令如下所示:

SELECT * FROM article a  WHERE to_tsvector('english', title) @@ to_tsquery('english','Rate|Limiting')

how to using rust diesel to do this query?如何使用锈柴油做这个查询? I am using the like right now and want to switch to full text search, this is the like query code main.rs look like:我现在正在使用like并想切换到全文搜索,这是like查询代码main.rs的样子:

#[macro_use]
extern crate diesel;

use diesel::{TextExpressionMethods, QueryDsl, RunQueryDsl};
use rust_wheel::config::db::config;
use crate::model::diesel::dict::dict_models::Article;

mod model;

fn main() {
    use model::diesel::dict::dict_schema::article as article_table;
    let connection = config::establish_connection();
    let mut query = article_table::table.into_boxed::<diesel::pg::Pg>();
    query = query.filter(article_table::title.like(format!("{}{}{}","%","demo","%")));
    let query_result = query.load::<Article>(&connection);
}

and this is the schema files dict_schema.rs look like:这是模式文件dict_schema.rs的样子:

table! {
    article (id) {
        id -> Int8,
        user_id -> Int8,
        title -> Varchar,
        author -> Varchar,
        guid -> Varchar,
        created_time -> Int8,
        updated_time -> Int8,
        link -> Nullable<Varchar>,
        sub_source_id -> Int8,
        cover_image -> Nullable<Varchar>,
        channel_reputation -> Int4,
        editor_pick -> Nullable<Int4>,
    }
}

and this is the model files dict_models.rs look like:这是模型文件dict_models.rs的样子:

// Generated by diesel_ext

#![allow(unused)]
#![allow(clippy::all)]

use std::io::Write;
use diesel::deserialize::FromSql;
use diesel::pg::Pg;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Jsonb;
use rocket::serde::Serialize;
use serde::Deserialize;
use chrono::DateTime;
use chrono::Utc;
use crate::model::diesel::dict::dict_schema::*;

#[derive(Queryable,QueryableByName,Debug,Serialize,Deserialize,Default,Clone)]
#[table_name = "article"]
pub struct Article {
    pub id: i64,
    pub user_id: i64,
    pub title: String,
    pub author: String,
    pub guid: String,
    pub created_time: i64,
    pub updated_time: i64,
    pub link: Option<String>,
    pub sub_source_id: i64,
    pub cover_image: Option<String>,
    pub channel_reputation: i32,
    pub editor_pick: Option<i32>,
}

and this is the dependencies Cargo.toml look like:这是Cargo.toml的依赖项:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
rocket = { version = "=0.5.0-rc.2", features = ["json"] }

serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
serde_derive = "1.0"
# database
diesel = { version = "1.4.7", features = ["postgres","serde_json"] }
dotenv = "0.15.0"
jsonwebtoken = "7"
chrono = "0.4"
config = "0.11"
ring = "0.16.20"
md5 = "0.7.0"
data-encoding = "2.3.2"
diesel_full_text_search = "1.0.1"
bigdecimal = "0.3.0"
# reddwarf public component
rust_wheel = { git = "https://github.com/jiangxiaoqiang/rust_wheel.git" }

What shuld I do to change to like query to full text search query?我应该怎么做才能将喜欢查询更改为全文搜索查询? I am searching from internet but no one talk about this issue.我正在从互联网上搜索,但没有人谈论这个问题。 BTW: this is the cargo version:顺便说一句:这是货物版本:

➜  rust-learn git:(group-by) ✗ cargo version
cargo 1.62.0 (a748cf5a3 2022-06-08)

and this is the rust version:这是生锈版本:

➜  rust-learn git:(group-by) ✗ rustc --version
rustc 1.62.0 (a8314ef7d 2022-06-27)

any idea about this issue?关于这个问题的任何想法? what should i do to using the full text search in diesel?我应该怎么做才能在柴油中使用全文搜索?

I have tried to add the dependencies diesel_full_text_search = "1.0.1" and tweak the main.rs code like this:我试图添加依赖diesel_full_text_search = "1.0.1"并调整main.rs代码,如下所示:

#[macro_use]
extern crate diesel;

use diesel::{TextExpressionMethods, QueryDsl, RunQueryDsl};
use diesel_full_text_search::{to_tsquery, to_tsvector, TsQueryExtensions};
use rust_wheel::config::db::config;
use diesel_full_text_search::TsVectorExtensions;
use crate::model::diesel::dict::dict_models::Article;

mod model;

fn main() {
    use model::diesel::dict::dict_schema::article as article_table;
    let connection = config::establish_connection();
    let mut query = article_table::table.into_boxed::<diesel::pg::Pg>();
    let filter_title = "经济 文化";
    let query_items: Vec<&str> = filter_title.trim().split_whitespace().collect();
    let query_array = query_items.join(" & ");
    let tsquery = to_tsquery(query_array);
    let tsvector = to_tsvector("'dolphinzhcfg', title");
    query = query.filter(&tsvector.matches(&tsquery));
    let query_result = query.load::<Article>(&connection);
}

shows error:显示错误:

mismatched types [E0308] expected `char`, found `&to_tsquery<String>`

what should I do to fixed this problem?我应该怎么做才能解决这个问题?

try to do a full text search like this:尝试像这样进行全文搜索:

            let query_items: Vec<&str> = filter_title.trim().split_whitespace().collect();
            let query_array = query_items.join("|");
            let ts_query = plainto_tsquery(format! {"{}{}{}", "'dolphinzhcfg','", query_array, "'"});
            let ts_vector = to_tsvector("'dolphinzhcfg', title");
            query = query.filter(ts_vector.matches(ts_query));

filter_title is the parameter passed from frontend that split with whitespace. filter_title是从前端传递的参数,用空格分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM