FTSearch involving date fields is confusing me

痴心易碎 提交于 2019-12-05 19:52:24

The line if(requestScope.edtFrom != & requestScope.edtFrom != "") { is not complete. You miss the part to test for. I assume it lacks the null check and therefore should be:

if(requestScope.edtFrom != null & requestScope.edtFrom != "") {

Furthermore, you need to format the date to return what you expect for the query (e.g. MM/dd/yyyy). The formatting in the inputText control only applies to the visual formatting and not the format of the actual content.

Finally, you need to remove the quotes around the date.

The following code example based on your code will return the date without formatting and then return the date with the correct formatting:

<xp:button value="Label" id="button1">
    <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="computedField1"></xp:eventHandler>
</xp:button>

<xp:inputText id="edtDateRangeFrom" value="#{requestScope.edtDateRangeFrom}">
    <xp:this.converter>
        <xp:convertDateTime type="date"></xp:convertDateTime>
    </xp:this.converter>
    <xp:dateTimeHelper></xp:dateTimeHelper>
</xp:inputText>

<xp:text escape="true" id="computedField1">
    <xp:this.value><![CDATA[#{javascript:var tmpArray = new Array("");
    var cTerms = 0;
    if(requestScope.edtDateRangeFrom != null & requestScope.edtDateRangeFrom != "") {
        tmpArray[cTerms++] = "(FIELD DeliveredDate >= \"" + requestScope.edtDateRangeFrom + "\")";
        var dateFormatter = new java.text.SimpleDateFormat( "MM/dd/yyyy" );
        var formattedDate = dateFormatter.format( requestScope.edtDateRangeFrom );
        tmpArray[cTerms++] = "(FIELD DeliveredDate >= " + formattedDate + ")";
    }

    qstring = tmpArray.join(" AND ").trim();
    requestScope.queryString = qstring;

    return qstring}]]>
    </xp:this.value>
</xp:text>

It will return the following where the 2nd part is the format you are looking for:

(FIELD DeliveredDate >= "Fri Apr 27 12:00:00 CEST 2012")
AND (FIELD DeliveredDate >= 04/27/2012)

Here is your code with all these updates:

var tmpArray = new Array("");
var cTerms = 0;
var dateFormatter = new java.text.SimpleDateFormat( "MM/dd/yyyy" );
var formattedDate = "";
if(requestScope.cmbSendTo != null & requestScope.cmbSendTo != "") {
    a = @Right(requestScope.cmbSendTo, "(");
    b = @Left(a,3);
tmpArray[cTerms++] = "(FIELD Mnemonic = \"" + b + "\")";
}
if(requestScope.edtFrom != null & requestScope.edtFrom != "") {
tmpArray[cTerms++] = "(FIELD From = \"" + requestScope.edtFrom + "\")";
}

if(requestScope.edtDateRangeFrom != null & requestScope.edtDateRangeFrom != "") {
    formattedDate = dateFormatter.format( requestScope.edtDateRangeFrom );
    tmpArray[cTerms++] = "(FIELD DeliveredDate >= " + formattedDate + ")";
} 
if(requestScope.edtDateRangeTo != null & requestScope.edtDateRangeTo != "") {
    formattedDate = dateFormatter.format( requestScope.edtDateRangeTo );
    tmpArray[cTerms++] = "(FIELD DeliveredDate <= " + formattedDate + ")";
}


if(requestScope.edtOriginal != null & requestScope.edtOriginal != "") {
tmpArray[cTerms++] = "(FIELD SourceFilename = \"" + requestScope.edtOriginal + "\")";
}
if(requestScope.edtCaptiva != null & requestScope.edtCaptiva != "") {
tmpArray[cTerms++] = "(FIELD Filename = \"" + requestScope.edtCaptiva + "\")";
}
qstring = tmpArray.join(" AND ").trim();
requestScope.queryString = qstring; // this just displays the query
return qstring // this is what sets the search property

If I'm reading it right, your query is resolving to

FIELD DeliveredDate >= "xx/yy/zz"

My first instinct was that you needed this instead:

FIELD DeliveredDate >= [xx/yy/zz]

But documentation indicates that you don't need brackets or quotes, so this:

FIELD DeliveredDate >= xx/yy/zz

Double check the query being created here. Perhaps print it to the screen or grab it from the debugger. You may see a problem there within the query and I believe you should be able to take that exact query and paste it in the search window of the database that has been full text indexed.

Also, have a look at this doc which covers notes query syntax, it may help you troubleshoot. I didn't see anything wrong in your code though.

http://www.loganmachinists.com/help/help8_client.nsf/f4b82fbb75e942a6852566ac0037f284/0e044d2c0639c979852572fe00687f29?OpenDocument

dates should always ( in my experience ) be written in the format mm/dd/yyyy so for instance

[deliverdatemin] >= 1/1/2012 and [deliverdatemax] <= 1/30/2012

An easy way to find out which query you are generating is to use the following piece of code to throw an error with the query generated

//youre own code  
throw new java.lang.exception(queryvariable);

Or you could simply do a print() to display the query on the serverconsole

As of my concern The best way to handle the date field is that converting our date value for one specific format using NotesDateTime., Because this is the best date conversion for xpage.

Dim dateTime As New NotesDateTime( "date String" )

or

Dim dateTime As New NotesDateTime( NotedateTime.getDtaeOnly() )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!