Xapian學習筆記

來源:互聯網
上載者:User

接著上回的打分系統, 為了把該小程式加入到整個大系統中去,需要大系統提供介面,其實也就是提供http資料以及產生的SQL資料而已,由於資料是分開存放的,HTTP資訊存放在一個檔案夾中,SQL資料存放在一個檔案夾中,並不像小程式那樣是簡單的放置在一個檔案中,所以這樣要涉及到搜尋技術。

具體的設想如下,使用者通過輸入一個http請求,並不需要從http檔案夾下所有檔案中去尋找資料,只需要去SQL檔案夾下去尋找即可,在尋找過程中,由於知道了http的時間,故也只需要查詢特定時間內產生的SQL語句,這裡初步設定為http請求提交後的在十秒之內產生的SQL語句。通過對這些語句進行操作,得到一個得分情況。

關鍵是如何在如此眾多的SQL語句中進行尋找,這裡用到一個新的工具,那就是Xapian,據說google也是用的和xapian類似的搜尋技術,我們不需要它是如何?的,只需要會用即可。

在http://xapian.org/ 上下載最新版後,解壓有doc檔案夾,裡面有詳細的使用說明可以看,如果只用到尋找,那不需要看很多,也就是一個例子即可。

先給出代碼:

/* quickstartsearch.cc: Simplest possible searcher * * ----START-LICENCE---- * Copyright 1999,2000,2001 BrightStation PLC * Copyright 2003,2004 Olly Betts * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 * USA */#include <xapian.h>#include <iostream>using namespace std;int main(int argc, char **argv){    // Simplest possible options parsing: we just require two or more    // parameters.    if (argc < 3) {        cout << "usage: " << argv[0] <<                " <path to database> <search terms>" << endl;        exit(1);    }    // Catch any Xapian::Error exceptions thrown    try {        // Make the databaseXapian::Database db(argv[1]);        // Start an enquire session        Xapian::Enquire enquire(db);        // Build the query object        Xapian::Query query(Xapian::Query::OP_OR, argv + 2, argv + argc);        cout << "Performing query `" << query.get_description() << "'" << endl;        // Give the query object to the enquire session        enquire.set_query(query);        // Get the top 10 results of the query        Xapian::MSet matches = enquire.get_mset(0, 10);        // Display the results        cout << matches.size() << " results found" << endl;        for (Xapian::MSetIterator i = matches.begin();             i != matches.end();             ++i) {            Xapian::Document doc = i.get_document();            cout << "Document ID " << *i << "\t" <<                    i.get_percent() << "% [" <<                    doc.get_data() << "]" << endl;        }    } catch(const Xapian::Error &error) {        cout << "Exception: "  << error.get_msg() << endl;    }}

 

在上述代碼中,調用了xapian.h檔案,使用者傳入的參數是關鍵字和目錄 Xapian::Database db(argv[1])是開啟一個資料庫(如果之前沒有它會自動建立一個),Xapian::Enquire enquire(db)是開啟一個查詢事件,Xapian::Query query是構建查詢語句,enquire.set_query是進行尋找,在尋找完成是能給出匹配總數,結果所在的檔案編號,符合度,查詢資料結果。

當涉及到某一範圍內查詢時,xapian也能實現,說明文檔如下

This class allows you to implement numeric range searches. The numbers used may be any number which is representable as a double, but requires that the stored values which the range is being applied
have been converted to strings at index time using the
Xapian::sortable_serialise() method:

Xapian::Document doc;doc.add_value(0, Xapian::sortable_serialise(price));

This method produces strings which will sort in numeric order, so you can use it if you want to be able to sort based on the value in numeric order, too.

The class allows a prefix or suffix to be specified which must be present on the values, allowing multiple NumberValueRangeProcessors to be active in the same queryparser. For example, this specifies
that a prefix of "$" must be present on the first value (and may optionally be present on the second value):

Xapian::QueryParser qp;Xapian::NumberValueRangeProcessor numrange_proc(0, "$", true);qp.add_valuerangeprocessor(&numrange_proc);

}

 

只需要給一個範圍即可,具體情況仍然在學習中,以後繼續補充。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.