如何在solr json

时间:2019-02-06 09:06:03

标签: solr solrj solrcloud

我在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()));
            }

日期转换实用程序如下:

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;
    }

在为内容编入索引之后,我想查询日期比较,例如:仅显示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
}
}

我在做什么错了?

1 个答案:

答案 0 :(得分: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)