mercredi 6 mai 2015

Lucene retrieve Values based on conditional of two fields

I've been trying to get Lucene.NET to respect a given condition which requires an evaluation of the index document fields.

I have a database table which stores "Experiences". For each experience there are two field/columns which tell is the row/ExperienceId is current (IsCurrent flag) as well as a StartDate and an EndDate.

Now, when implementing a "search engine" on the platform I need to have an option which, once checked (it's a checkbox) should retrieve only the those where IsCurrent is 1 or, otherwise, the one where the EndDate is the closest to the current date, i.e, EndDate is the highest. All this should be done for each PersonId (a foreing key in my Experiences table)

The thing is, I don't know how to tell Lucene that it should repect these conditions, that is, give me the ones with IsCurrent = 1 or the ones where IsCurrent = 0 but the EndDate is the maximum for each PersonId.

I looked into MultiTermQueries but couldn't make any sense of it.

I was trying the code bellow but I don't think this is working because the results make no sense (and I know the problem lies between the chair and the keyboard).

if (isCurrent)
{
    BooleanQuery queryIsCurrent = new BooleanQuery();
    queryIsCurrent.Add(new TermQuery(new Term("IsCurrent", "1")), BooleanClause.Occur.SHOULD);
    booleanQuery.Add(queryIsCurrent, BooleanClause.Occur.SHOULD);
    //Should I be using SHOULD here?? ^^


    //Should I be using this code for the date at all? I don't think so..
    TermRangeQuery termRangeQuery = null;
    termRangeQuery = new TermRangeQuery("EndDate", null, DateTime.Now.ToString(), true, true);
    booleanQuery.Add(termRangeQuery, BooleanClause.Occur.MUST);
}

Aucun commentaire:

Enregistrer un commentaire