如何做搜索功能 [英] how to do search functionality

查看:103
本文介绍了如何做搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我。
我正在进行搜索功能。以下是我的代码和用户故事。我只能通过第一个用户故事,但不能与其他故事。有人可以帮助我。



用户故事:


  1. 作为用户,如果我搜索技能和位置,我应该得到与查询匹配的结果。


  2. 作为用户,如果我只搜索技能,我仍然应该得到与查询匹配的结果。


  3. 作为用户,如果我只搜索位置,我应该得到与查询匹配的结果。


  4. 作为用户,如果我用无效技能(J,A,B)或位置进行搜索,我不应该得到与查询匹配的结果。

      var search = {
    get:function(req,res){
    var locationQuery = req。 params.locationQuery,
    skillsQuery = req.params.skillsQuery;

    Jobs.find({state:{$ regex:locationQuery,$ options:i},技能:{$ regex:skillQuery,$ options:i }},function(err,result){
    if(err){
    console.log('Not a Valid Search');
    res.status(500).send(err ,不是有效的搜索);
    } else {
    res.json(result);
    }
    });
    }
    };



解决方案

对于这种可选支持,最好在GET中使用搜索参数作为查询。您可以参考以下代码片段。



  var search = {get :function(req,res){var query = {}; if(req.query.locationQuery){query.state = {$ regex:req.query.locationQuery,$ options:i}; } if(req.query.skillsQuery){query.Skills = {$ regex:req.query.skillsQuery$ options:i}} Jobs.find(query,function(err,result){if (err){console.log('not a Valid Search'); res.status(500).send(err,'not a Valid Search');} else {res.json(result);}});  



Upadte: / p>

将路由定义为:
searchRoute.get('/',search.get); / p>

您可以通过POSTMAN发送您的查询参数:



localhost: 3000 / api / search /?skillsQuery =java,javascript& loyangQuery =孟买


Please help me out. I'm making a search functionality. Below is My Code and User stories. I'm able to pass only the first User Story but not with the other stories. Can someone please help me out.

User Stories:

  1. As an User if I search with Skills and location I should get the results which matched with the query.

  2. As an User if I search with only Skills still I should get the results which matched with the query.

  3. As an User if I search with only Location still I should get the results which matched with the query.

  4. As an User if I search with invalid skills (J, A, B) or location I shouldn't get the results which does matched the query..

    var search = {
     get: function (req, res) {
        var locationQuery = req.params.locationQuery,
        skillsQuery = req.params.skillsQuery;
    
        Jobs.find({state: { "$regex": locationQuery, "$options": "i" }, Skills: { "$regex": skillsQuery, "$options": "i" }}, function(err, result) {
            if (err) {
                console.log('Not a Valid Search');
                res.status(500).send(err, 'Not a Valid Search');
            }else {
                res.json(result);
            }            
        });
     }
    };
    

解决方案

For this kind of optional support, It would be better to take search parameters as query in GET. You can take reference of the following code snippet.

var search = {
 get: function (req, res) {
    var query = {};
    if(req.query.locationQuery){
      query.state = { "$regex": req.query.locationQuery, "$options": "i" };
    }
    if(req.query.skillsQuery){
      query.Skills = { "$regex": req.query.skillsQuery, "$options": "i" }
    }

    Jobs.find(query, function(err, result) {
        if (err) {
            console.log('Not a Valid Search');
            res.status(500).send(err, 'Not a Valid Search');
        }else {
            res.json(result);
        }            
    });
 }
};

Upadte:

Define your route as: searchRoute.get('/', search.get);

And you can send your query parameters as(through POSTMAN):

localhost:3000/api/search/?skillsQuery="java,javascript"&lo‌​cationQuery="Mumbai

这篇关于如何做搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆