How to Use Filter in Ubuntu Scope to improve the search quality more accurately?

Source: Internet
Author: User

How to Use Filter in Ubuntu Scope to improve the search quality more accurately?

In Ubuntu Scope, a new Filter function is being developed. We can find more information about filter on our developer website. Here you can see some introduction to filter, but it is still difficult to use it. In our routine today, we will show how to use filter to achieve better search. In practical applications, for example, we can use filter to implement the Scope of ctrip as follows:




In the middle figure above, we can see"Chongqing"Selected. In the second column, we can use our department to search from one city to another.


First, we will use the Scope we are familiar with as an exercise. We can download our code in my previous article "create a department comment Scope (Qt XML) on Ubuntu OS:


Git clone https://gitcafe.com/ubuntu/dianpingdept.git


First, make sure that the SDK runs correctly. In today's exercise, we want to implement the following Scope:


'


We added a filter to our design. It can be used to select the order in which the search results are displayed. In this way, the search results can be displayed in our department according to our selection.


First, let's look at our comments API:


We can see a"Sort. In our API, we can use it to select the display result.


To design our filter, we need to make the following changes:

Query. h
class Query: public unity::scopes::SearchQueryBase {public:    Query(const unity::scopes::CannedQuery &query,          const unity::scopes::SearchMetadata &metadata, QString const& scopeDir,          QString const& cacheDir, api::Config::Ptr config);    ~Query() = default;    ...private:    ...        QMap<QString, QString> m_sortPref;    QString m_pref;};


Query. cpp
// This function is used to add filter support for my scopevoid Query: initFilerData () {m_sortPref ["1"] = "default "; m_sortPref ["3"] = "product evaluation high priority"; m_sortPref ["2"] = "star level high priority "; m_sortPref ["4"] = "high environmental evaluation Priority"; m_sortPref ["5"] = "high service evaluation Priority "; m_sortPref ["6"] = "many comments have priority"; m_sortPref ["7"] = "the distance from the input coordinates of longitude and latitude is near priority "; m_sortPref ["8"] = "per capita low price first"; m_sortPref ["9"] = "per capita high price first ";}

This is the data required by the helper method to initialize our filter.

void Query::run(sc::SearchReplyProxy const& reply) {    qDebug() <<  "Run is started .............................!";    // Initialize the scopes    initScope();        ...        initFilerData();    // set up the filter    sc::Filters filters;    const sc::CannedQuery &myquery(sc::SearchQueryBase::query());    sc::OptionSelectorFilter::SPtr prefFilter = sc::OptionSelectorFilter::create("category",                                             m_sortPref[DEFAULT_PREF].toStdString());    prefFilter->set_display_hints(1);    QMapIterator<QString, QString> i(m_sortPref);    while (i.hasNext()) {        i.next();        qDebug() << i.key() << ": " << i.value();        prefFilter->add_option(i.key().toStdString(), i.value().toStdString());        prefFilter->active_options(myquery.filter_state());        filters.push_back(prefFilter);    }    reply->push(filters, myquery.filter_state());    if (prefFilter->has_active_option(myquery.filter_state())){        auto o = *(prefFilter->active_options(myquery.filter_state()).begin());        m_pref = QString::fromStdString(o->id());        cerr << "pref id: " << o->id()  << endl;        cerr << "label: " << o->label() << endl;    } else {        cerr << "no active option!" << endl;        m_pref = DEFAULT_PREF;    }    qDebug() << "m_pref: " << m_pref;        ...}


We use the above code to create a filter. At the same time, we can get the value of the filter selected by the user, coexist in the member variable M_pref.
QString Query::getDeptUrl(QString dept){    QMap<QString, QString> map;    map["category"] = dept;    map["sort"] = m_pref;    map["limit"] = QString::number(m_limit);    map["platform"] = "2";    map["format"] = "xml";    qDebug() << "m_latitude: " << m_latitude;    qDebug() << "m_longitude: " << m_longitude;    map["latitude"] = m_latitude;    map["longitude"] = m_longitude;    return getUrl( BASE_URI, map );}


In the above Code, we use m_pref and make it part of the API request.
In this way, we have completed our filter design. The displayed image is displayed on it.
All source code can be downloaded at the address: git clone https://gitcafe.com/ubuntu/dianping_filter.git


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.