matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
436 stars 68 forks source link

Sort doesnt work with elasticsearch #595

Closed AdrianAbba closed 7 years ago

AdrianAbba commented 7 years ago
export const ProjectsIndex = new Index({
  collection: Projects,
  fields: ["title", "subtitle", "description", "jobs.joblabel", "tags", "occasions", "owner.wholeName", "team.userName"],
  engine: new ElasticSearchEngine({
    query: (searchObject) => {
      if (searchObject.title){
        var finalQuery = { "bool": { "must": [],  } }
        var words = searchObject.title,
        wordArray = words.split(' ');
        console.log(wordArray);
        _.each(wordArray, function(word){
          const queryInput = {
            "bool": {
              "should":[{
                "multi_match" :{
                  "query": word,
                  "fields": [ "title", "subtitle", "description", "jobs.joblabel", "tags", "occasions", "owner.wholeName", "team.userName"]
                }
              },{
                "regexp":{
                  "title":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }},{
                "regexp":{
                  "subtitle":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "description":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "jobs.joblabel":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "tags":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "occasions":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "owner.wholeName":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }
              },{
                "regexp":{
                  "team.userName":{
                    "value":".*"+word+".*",
                    "boost":1.2
                  }
                }}
              ], "minimum_number_should_match": 1
            }
          }
          finalQuery.bool.must.push(queryInput);
        });
        console.log(finalQuery);
        return finalQuery;
      }
    },
    sort: function (searchObject, options) {
      var sortBy = options.search.props.sortBy
      if (!sortBy){
        sortBy = 'relevance';
      }
      console.log(sortBy);
      // return a mongo sort specifier
      if ('relevance' === sortBy) {
        return ["_score"];

      } else if ('newest' === sortBy) {
        return  [{"deadline" : "desc"}];

      } else if ('bestScore' === sortBy) {
        return [{"deadline" : "asc"}];

      } else {
        throw new Meteor.Error('Invalid sort by prop passed')
      }
    },
     body:(body) => {
       body._source = ["_id"];
       delete body.fields;
       console.log("SearchBody", body);
       return body;
     },
    /*getElasticSearchDoc:(doc, fields) =>{
      console.log("getDoc", doc);
      console.log("getFields", fields);
      return doc;

    }*/
  }),
});
SearchBody { query: { bool: { must: [Object] } },
 sort: [ { deadline: 'asc' } ],
 _source: [ '_id' ] }

The body should be right, the query works, but if im pressing the sort button, he is loading the new body, with another sort like: sort[{deadline:"desc"}] but he didnt changed the sort, he is loading the same documents on the same places

matteodem commented 7 years ago

Did you find out where the problem lies in the meantime?

matteodem commented 7 years ago

I'll re-open this as soon as I got an answer.