如何比较Solr的JSON日期

问题描述 投票:0回答:1

我有一个Solr的索引的文档:

{
        "id":"/content/dam/enron-cmi/fragments/headline-header/industry/industry commitment section header",
        "path":["/content/dam/enron-cmi/fragments/headline-header/industry/industry commitment section header"],
        "title":["Industry Commitment Section Header"],
        "previewText":["Industry commitment"],
        "publishedDate_dt":"2018-09-08T00:00:00Z",
        "lastModifiedDate_dt":"2018-09-08T00:00:00Z",
        "_version_":1624706590447763456},
      {
        "id":"/content/dam/enron-cmi/fragments/industry-overview/financial-services-overview---long",
        "path":["/content/dam/enron-cmi/fragments/industry-overview/financial-services-overview---long"],
        "title":["Financial Services Overview - Long"],
        "previewText":["Adaptation to the disruptive innovations that are driving necessary transformation and reform has me ..."],
        "publishedDate_dt":"2018-09-08T00:00:00Z",
        "lastModifiedDate_dt":"2018-09-08T00:00:00Z",
        "_version_":1624706590447763456}
        }

我SolrInputDocument代码如下所示(指数只有当它转化为具体的日期之后的有效日期):

if (tempObject.has("expirationDate") && tempObject.get("expirationDate") != null
                    && CommonUtils.isDateValid(tempObject.get("expirationDate").getAsString())) {
                nestedDoc.addField("expirationDate_dt",
                        CommonUtils.toUtcDate(tempObject.get("expirationDate").getAsString()));
            }

            if (tempObject.has("publishedDate") && tempObject.get("publishedDate") != null
                    && CommonUtils.isDateValid(tempObject.get("publishedDate").getAsString())) {
                nestedDoc.addField("publishedDate_dt",
                        CommonUtils.toUtcDate(tempObject.get("publishedDate").getAsString()));
            }

            if (tempObject.has("lastModifiedDate") && tempObject.get("lastModifiedDate") != null
                    && CommonUtils.isDateValid(tempObject.get("lastModifiedDate").getAsString())) {
                nestedDoc.addField("lastModifiedDate_dt",
                        CommonUtils.toUtcDate(tempObject.get("lastModifiedDate").getAsString()));
            }

日期转换UTIL如下:

public static String toUtcDate(String dateStr) {
        SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        String outDateFormat = StringUtils.EMPTY;
        String dateFormat = "MM-dd-yyyy";

        try {
            outDateFormat = out.format(new SimpleDateFormat(dateFormat).parse(dateStr));
        } catch (Exception ignore) {
            LOG.info(ignore.getMessage(), null, ignore);
        }

        return outDateFormat;
    }

I指数我的内容后,我想查询例如日期比较:只显示publishedDate_dt> lastModifiedDate_dt我该怎么办呢?对于Solr的这些文件是不是真的清楚。 Solr的版本是7.6。

我可以使用的Solr的Java API做的一样吗?

我曾尝试以下查询:

http://localhost:8983/solr/collection/select?fl=publishedDate_dt,lastModifiedDate_dt&defType=func&q=ms(publishedDate_dt,%20lastModifiedDate_dt)

并且输出是:

“文档”:[{ “publishedDate_dt”: “2018-09-20T00:00:00Z”},{ “publishedDate_dt”: “2018-09-01T00:00:00Z”},{ “publishedDate_dt”:“2018-09 -01T00:00:00Z”},{ “publishedDate_dt”: “2018-09-01T00:00:00Z”},

尝试了这一点:

http://localhost:8983/solr/collection/select?&q=:FL = expirationDate_dt,publishedDate_dt,MS(expirationDate_dt,publishedDate_dt)DEFTYPE = FUNC输出是:

{
"responseHeader": {
"zkConnected": true,
"status": 400,
"QTime": 2,
"params": {
"q": "*:*",
"defType": "func",
"fl": "expirationDate_dt,publishedDate_dt,ms(expirationDate_dt,publishedDate_dt)"
}
},
"error": {
"metadata": [
"error-class",
"org.apache.solr.common.SolrException",
"root-error-class",
"org.apache.solr.search.SyntaxError"
],
"msg": "org.apache.solr.search.SyntaxError: Expected identifier at pos 0 str='*:*'",
"code": 400
}
}

我究竟做错了什么 ?

solr solrj solrcloud
1个回答
0
投票

在这种情况下,你可以使用函数查询。

这里是链接它Function QueriesAvailable Functions

您可以使用ms功能:它返回它的参数之间的差异毫秒

语法实例

ms(NOW/DAY)
ms(2000-01-01T00:00:00Z)
ms(mydatefield)
ms(NOW,mydatefield)
ms(mydatefield, 2000-01-01T00:00:00Z)
ms(datefield1, datefield2)
© www.soinside.com 2019 - 2024. All rights reserved.