2

我有一个 Solr 模式,其中我的对象可以有这两个字段:

  • 序列号开始
  • 序列号结束

例如:

"SerialNumberStart": "0000067098910",
"SerialNumberEnd": "0000067098920",

查询时,我希望能够SerialNumberStart在我的查询中传入 a 。如果该序列号与文档的序列号开始匹配,我想获取该文档。但是,当它不匹配时,我想查找我传入的序列号在 SerialNumberStart 和 SerialNumberEnd 之间的所有文档。在 or 之后我的查询的第二部分应该是这样的:get the documents where someSerialStart is between documentSerialStart and documentSerialEnd.

我从 JavaScript 构建查询。我试图将范围组合如下,但它似乎不正确,但我不知道如何修复它。

var value = req.query['matchedPattern.serialNumber'];
return 'SerialSNumberStart:' + value +  ' OR (SerialNumberStart:[* TO ' + value + '] AND SerialNumberEnd:[' + value + ' TO *])';

谢谢。

4

2 回答 2

1

我根据您的说明更新了答案。有 4 种情况需要排查: 1 SN 存在 2 SN 不存在 2.1 获取 SN 之前的范围 2.2 获取 SN 之后的范围

直观地说,我肯定会将它分成两个块:存在 1 个 SN,因此将其返回给客户端。这是它的样子:

http://solrserver.us-west-2.compute.amazonaws.com:8983/solr/hellosolr/select?indent=on&q=id:S9V7464-APL-KIT3&wt=json

响应如下:

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"id:S9V7464-APL-KIT3",
"indent":"on",
"wt":"json"}},
"response":{"numFound":1,"start":0,"docs":[
{
"id":"S9V7464-APL-KIT3",
...
  1. 如果您的 SN 不存在,则响应返回“numFound”:1,此时您需要运行搜索。因此,如果没有文档,查询将如下所示:

    http://solrserver.us-west-2.compute.amazonaws.com:8983/solr/hellosolr/select?indent=on&q=id:S9V7464-APL-KIT5&rows=1&sort=id%20asc&start=0&wt=json

响应如下所示:

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"id:S9V7464-APL-KIT5",
"indent":"on",
"start":"0",
"sort":"id asc",
"rows":"1",
"wt":"json"}},
"response":{"numFound":0,"start":0,"docs":[]
}}

2.1 所以你需要SN之前的下一个邻居。我要在降序范围搜索中添加两件事,排序和对答案数量的限制。这是查询的内容:

http://solrserver.us-west-2.compute.amazonaws.com:8983/solr/hellosolr/select?indent=on&q=id:[*%20TO%20S9V7464-APL-KIT5]&rows=1&sort=id%20desc&start=0&wt=json

响应如下所示:

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"id:[* TO S9V7464-APL-KIT5]",
"indent":"on",
"start":"0",
"sort":"id desc",
"rows":"1",
"wt":"json"}},
"response":{"numFound":25,"start":0,"docs":[
{
"id":"S9V7464-APL-KIT3",
"name":["Belkin Mobile Power Cord for iPod w/ Dock"],
"manu":["Belkin"],
"manu_id_s":"belkin",
"cat":["electronics",
"connector"],
"features":["car power adapter, white"],
"weight":[6.7],
"price":[19.95],
"popularity":[1],
"inStock":[false],
"store":["45.18014,-93.87741"],
"manufacturedate_dt":"2005-08-01T16:30:25Z",
"_version_":1547654166135963648}]
}}

2.2 你需要一个升序范围,按返回文档的数量排序和限制。就像是:

http://solrserver.us-west-2.compute.amazonaws.com:8983/solr/hellosolr/select?indent=on&q=id:[S9V7464-APL-KIT5%20TO%20*]&rows=1&sort=id%20asc&start=0&wt=json

响应将与此类似:

{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"id:[S9V7464-APL-KIT5 TO *]",
"indent":"on",
"start":"0",
"sort":"id asc",
"rows":"1",
"wt":"json"}},
"response":{"numFound":8,"start":0,"docs":[
{
"id":"S9V7464-APL-KIT7",
"name":["Belkin Mobile Power Cord for iPod w/ Dock"],
"manu":["Belkin"],
"manu_id_s":"belkin",
"cat":["electronics","connector"],
"features":["car power adapter, white"],
"weight":[6.7],
"price":[19.95],
"popularity":[1],
"inStock":[false],
"store":["45.18014,-93.87741"],
"manufacturedate_dt":"2005-08-01T16:30:25Z",
"_version_":1547654166137012224}]
}}

增加行数返回更多文档,修改 start 可用作获取第 N 个邻居的偏移量。

于 2016-10-08T04:55:36.180 回答
0

您查询的内容似乎与您的内容相反。您在 SerialNumberStart 之前和 SerialNumberEnd 之后查找记录。首先在 Solr 中获取正确的查询,然后将其映射到 Javascript。

此外,您似乎没有引用第一个子句中的值。您可能想这样做以防万一。

于 2016-10-08T03:34:38.817 回答