How to use filter in Ubuntu scope to improve the quality of your search more accurately

Source: Internet
Author: User

At the scope of Ubuntu, a new filter function is currently being developed. We can find more information about the filter on our developer website. There you can see some introduction to filter, but it's still a bit difficult to really get started with it. Today in our routines, we'll show you how to use filter for a better search. In practical applications, for example, we can use the filter to achieve the following Ctrip scope:




In the middle of the above diagram, we can see that " Chongqing " is selected, in the second column we can use our department to search from one city to another city.


First, we use our familiar review scope as an exercise. We can download our code in my previous article "Creating a department review on the Ubuntu OS scope (Qt XML)":


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


First, we are in our SDK to make sure that it can actually run. In today's exercise, we want to achieve the following scope:


`


We have added a filter to our design. It can be used to select the order in which we display the search results. This will allow us to show the results of our search in our department according to our selection.


First, we can take a look at our reviews API interface:


We can see a "sort" item. In our API, we can use it to check how the results are displayed.


To be able to design our filter, we need to make the following modifications:

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::P tr Config);    ~query () = default;    .... Private:    ...        Qmap<qstring, qstring> m_sortpref;    QString M_pref;};


Query.cpp
This function was 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 high priority";    m_sortpref["4" = "Environmental assessment high priority";    m_sortpref["5" = "Service evaluation high priority";    m_sortpref["6"] = "Number of reviews priority";    m_sortpref["7"] = "distance from incoming latitude and longitude coordinates near priority";    m_sortpref["8" = "low per capita price priority";    m_sortpref["9" = "per capita price high priority";}

This is the data that a helper method uses 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 create a filter from the code above, and we can get the value of the filter that the user chooses, and the member variable M_prefIn
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 code above, we take advantage of m_pref and make it part of the API request.
This allows us to complete our filter design work. The displayed picture is shown above.
All source code can be downloaded at address: git clone https://gitcafe.com/ubuntu/dianping_filter.git


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to use filter in Ubuntu scope to improve the quality of your search more accurately

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.