簡體   English   中英

播放框架:由於無法使用@Index而使用ebean orm實現全文搜索的最佳方法

[英]Play framework: Best way to implement a fulltext search using ebean orm because of not working @Index

目前,我正在為一個正在使用Play的大學做項目! 框架2.4和Ebean。

目前,我正在嘗試實現實時用戶搜索,其中用戶可以使用全文輸入輸入來搜索其他用戶。 可能的搜索字符串可以是“ Michael Lee”,也可以只是一個電子郵件地址。 我將搜索字符串傳遞給此方法,在此方法中,我嘗試以一種智能的方式訪問數據庫:

public Result searchUser(String sstring) {
    String query = "WHERE MATCH (firstname,lastname,email) AGAINST ('" + sstring + "' IN BOOLEAN MODE)";
    List<User> resultUsers = User.find
            .setQuery(query)
            .findList();

    String resultString = "";
    for (User user: resultUsers) {
      resultString += user.getFirstname() + user.getLastname() + "<br>";
    }

    return ok(resultString);
}

我正在嘗試在firstnamelastnameemail列中搜索關鍵字,但是play給我這個錯誤:

[PersistenceException:查詢引發SQLException:使用的表類型不支持FULLTEXT索引綁定值:[]查詢為:選擇t0.id c0,t0.firstname c1,t0.lastname c2,t0.birthday c3,t0.email c4 ,t0.password c5,t0.author c6,t0.points c7,t0.locked c8,t0.last_online c9,t0.date_created c10,t0.date_updated c11來自用戶t0,其中MATCH(t0.firstname,t0.lastname,t0 .email)反對(布爾模式下的'erik')]

是否可以告訴ebean使用所需的全文索引創建表users 還是有其他聰明的方法可以在MySQL表中搜索許多列中的搜索字符串? 搜索字符串可以包含所有列中的關鍵字。

謝謝。

更新:

我試圖添加一個索引,就像ebean api文檔中提到的那樣,但不幸的是,似乎播放ebean版本不支持columnNames關鍵字(以及其他所有關鍵字……)。

@Index(name = "livesearch_index", columnNames = {"firstname","lastname","email"})

那我還能做什么? 可以在外部自動設置的第二個演進文件中設置此索引嗎?

我找到了使用play框架演變的解決方案。 Ebean自動創建一個名為1.sql的第一個演化文件,其中包含數據庫模式的完整創建。

對於我的users表UP部分如下所示:

create table users (
id                            bigint auto_increment not null,
firstname                     varchar(45),
lastname                      varchar(45),
birthday                      datetime,
email                         varchar(60),
password                      varchar(32),
author                        tinyint(1) default 0,
points                        integer,
locked                        tinyint(1) default 0,
last_online                   datetime,
date_created                  datetime not null,
date_updated                  datetime not null,
constraint uq_users_email unique (email),
constraint pk_users primary key (id)
);

如您所見,沒有創建FULLTEXT索引。

我所做的是創建第二個演進文件2.sql。 這會將FULLTEXT索引添加到users表。

# --- !Ups
create fulltext index livesearch_index on users (firstname,lastname,email);

# --- !Downs
drop index livesearch_index on users;

我知道這不是最好的解決方案,但它是一個可行的解決方案。 我仍然對其他人如何解決此問題或您對全文搜索使用哪種解決方案感興趣。

您的解決方案實際上是好的。 這是處理架構演變的正確方法。

我通常要做的是在開發過程中啟用Evolutions插件,然后在應用程序准備就緒時將其禁用-這樣,您最終將只有1.sql至少直到收到另一個更改請求並決定更新架構為止。 此處提供更多信息: https : //playframework.com/documentation/2.4.x/Evolutions

除此之外,您還應該考慮另一件事-數據庫在搜索方面不利,尤其是在文本搜索方面。 幾個月前,我擔任類似職務,因為我需要實現這種功能(請參閱https://softwareengineering.stackexchange.com/questions/301843/elasticsearch-and-relational-database-combination )。 為了節省您的時間,我最終使用ElasticSearch來處理我所有與搜索相關的功能(全文,聚合等),我並不后悔! 我知道,這可能是矯枉過正,為您的大學項目,但在現實世界的情況下,您不想使用數據庫的全文搜索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM