首页 文章

在facet字段中使用特殊字符会导致错误的facet结果

提问于
浏览
6

我已经为电子商务商店实施了Solr Search and Faceting,并面临与facet过滤器分面结果相关的问题 . 只有当我们在facet字段中有特殊字符(即括号)时才会发生这种情况,否则一切正常 .

我使用SolrNet实现了这一点 . 我检查了直接对Solr进行原始查询,发现这个问题可能在Solr本身,而与SolrNet无关 .

例:

我有很多产品和过滤器如下:

RAM (GB)
  2 GB
  4 GB
  8 GB

Memory (GB)
  4 GB
  8 GB
  16 GB

每个方面选项都有一些产品,所以问题不在于facet.min计数 . 我也正确地应用了标记 .

现在,其中一个方面工作正常,而另一个方面似乎不能在方面字段中使用括号 .

这是我定义facet字段的架构 .

<dynamicField name="f_*"  type="string" indexed="true" stored="true" multiValued="true" required="false"  />
<dynamicField name="pa_*" type="string" indexed="true" stored="true" multiValued="true" required="false" />

当我以pa_查询字段时,Facet工作正常,但不是f_ .

查询我正在进入Solr:

../select?indent=on&wt=json&facet.field={!ex%3Dpa_RAM(GB)}pa_RAM(GB)&fq={!tag%3Dpa_RAM\(GB\)}pa_RAM\(GB\):2%2BGB&q=CategoryID:(1+OR+2+OR+3+OR+4)&start=0&rows=10&defType=edismax&facet.mincount=1&facet=true&spellcheck.collate=true

Image1
enter image description here

这可以正常工作 .

另一个疑问:

../select?indent=on&wt=json&facet.field={!ex%3Df_Memory(GB)}f_Memory(GB)&fq={!tag%3Df_Memory\(GB\)}f_Memory\(GB\):4%2BGB&q=CategoryID:(1+OR+2+OR+3+OR+4)&start=0&rows=10&defType=edismax&facet.mincount=1&facet=true&spellcheck.collate=true

给出以下结果:

图片2
enter image description here

这不起作用 . 但是,如果我从查询和索引数据中删除特殊字符,这可以正常工作 .

此外,返回的facet选项是我添加过滤器标签的选定选项 . Solr不会返回所有其他方面选项 .

我无法弄清楚为什么会发生这种情况以及如何解决这个问题 .

任何线索\想法都会很棒!

请参考此查询和图像 . (这不是正确的方法或完美的解决方案)

../select?indent=on&wt=json&facet.field={!ex%3Df_Memory(GB)}f_Memory(GB)&fq={!tag%3Df_Memory(GB)}f_Memory\(GB\):4%2BGB&q=CategoryID:(1+OR+2+OR+3+OR+4)&start=0&rows=10&defType=edismax&facet.mincount=1&facet=true&spellcheck.collate=true&fl=Id,Name,f_Memory(GB)

enter image description here

参考链接:Local Parameters for Faceting

请帮我!

1 回答

  • 1
    • 如果您需要按字面搜索它们,则必须转义SOLR查询中的特殊字符( qfq 参数),否则queryParser将具有其特殊含义 . (见"Escaping special characters" in SOLR Documentation

    在示例中 + 字符未在 fq 中转义:

    {!tag=f_Memory\(GB\)}f_Memory\(GB\):4+GB
    
    • 这些转义规则不适用于Local parameters,即全部在 {!} 之间 .

    在示例中,您在标记标签中转义了 () . 这样,在过滤器中定义为 {!tag=f_Memory\(GB\)} 的标签与在facet字段中 {!ex=f_Memory+(GB)} 中引用的标签不同,因此在分面期间不排除过滤器,并且仅使用匹配文档来构建构面 .

    您应该将过滤器编写为:

    {!tag=f_Memory(GB)}f_Memory\(GB\):4\+GB
    

    和方面一样

    {!ex=f_Memory+(GB)}f_Memory+(GB)
    

    获得你想要的东西 .

    完整正确请求的示例:

    ../select?indent=on&wt=json&facet.field={!ex%3Df_Memory(GB)}f_Memory(GB)&fq={!tag%3Df_Memory(GB)}f_Memory\(GB\):4\%2BGB&q=CategoryID:(1+OR+2+OR+3+OR+4)&start=0&rows=10&defType=edismax&facet.mincount=1&facet=true&spellcheck.collate=true
    

    我在本地测试的简单实例:

    这是核心数据:

    请求:

    http://localhost:8983/solr/test/select?q=*%3A*&fl=id%2Cf_*%2Cpa_*&wt=json&indent=true
    

    响应:

    {
      "responseHeader": {
        "status": 0,
        "QTime": 1,
        "params": {
          "q": "*:*",
          "indent": "true",
          "fl": "id,f_*,pa_*",
          "wt": "json",
          "_": "1474529614808"
        }
      },
      "response": {
        "numFound": 2,
        "start": 0,
        "docs": [
          {
            "id": "1",
            "f_Memory(GB)": [
              "4+GB"
            ],
            "pa_RAM(GB)": [
              "2+GB",
              "4GB",
              "8GB"
            ]
          },
          {
            "id": "2",
            "f_Memory(GB)": [
              "8+GB"
            ],
            "pa_RAM(GB)": [
              "4GB"
            ]
          }
        ]
      }
    }
    

    工作分面:

    请求:

    http://localhost:8983/solr/test/select?q=*%3A*&fq=%7B!tag%3Df_Memory(GB)%7Df_Memory%5C(GB%5C)%3A4%5C%2BGB&fl=id%2Cf_*%2Cpa_*&wt=json&indent=true&facet=true&facet.field=%7B!ex%3Df_Memory(GB)%7Df_Memory(GB)
    

    响应:

    {
      "responseHeader": {
        "status": 0,
        "QTime": 2,
        "params": {
          "q": "*:*",
          "facet.field": "{!ex=f_Memory(GB)}f_Memory(GB)",
          "indent": "true",
          "fl": "id,f_*,pa_*",
          "fq": "{!tag=f_Memory(GB)}f_Memory\\(GB\\):4\\+GB",
          "wt": "json",
          "facet": "true",
          "_": "1474530054207"
        }
      },
      "response": {
        "numFound": 1,
        "start": 0,
        "docs": [
          {
            "id": "1",
            "f_Memory(GB)": [
              "4+GB"
            ],
            "pa_RAM(GB)": [
              "2+GB",
              "4GB",
              "8GB"
            ]
          }
        ]
      },
      "facet_counts": {
        "facet_queries": {},
        "facet_fields": {
          "f_Memory(GB)": [
            "4+GB",
            1,
            "8+GB",
            1
          ]
        },
        "facet_dates": {},
        "facet_ranges": {},
        "facet_intervals": {},
        "facet_heatmaps": {}
      }
    }
    

相关问题