Solr使用PatternTokenizerFactory查询TextField

时间:2014-08-05 16:41:42

标签: solr textfield tokenize

我在查询使用PatternTokenizerFactory标记化的TextField时遇到问题。

我使用的是Solr版本4.7.2。

我的架构如下:

<field name="filter_amenity_codes" type="caretDelimited" indexed="true" stored="true" />
...
<fieldType class="solr.TextField" name="caretDelimited">
    <analyzer>
        <tokenizer class="solr.PatternTokenizerFactory" pattern="\^"/>
    </analyzer>
</fieldType>

字段值如下所示:     &#34; 2 ^ 9 ^ 11 ^ 12 ^ 15 ^ 93&#34;

对该领域的分析看起来也不错。它似乎标记了我想要它的方式。 http://i.stack.imgur.com/WiraY.png

所以,现在我尝试查询此字段以用作过滤器。例如,我想查找包含代码93的所有文档。当我使用以下查询时,我总是得到0个文档作为响应。我已尝试使用值93,&#34; 93&#34;,/ 93 /并且这些都没有返回结果。

我使用的查询看起来像:     filter_amenity_codes:93

这就是响应的样子:

{
    "response": {
        "docs": [],
        "numFound": 0,
        "start": 0
    },
    "responseHeader": {
        "QTime": 2,
        "params": {
            "_": "1407261941064",
            "indent": "true",
            "q": "filter_amenity_codes:93",
            "wt": "json"
        },
        "status": 0
    }
}

在查询中是否存在我无法正常执行的操作?或者我的架构中的字段未正确设置?

1 个答案:

答案 0 :(得分:1)

您的示例架构和查询一样有效 - 至少使用Solr 4.9.0。

<强> data.json

[
    {"id": 123, "filter_amenity_codes":"1^6^7^8^12^13^14"},
    {"id": 321, "filter_amenity_codes":"11^16^17^18^112^113^114"}
]

schema.xml(摘录)

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="filter_amenity_codes" type="caretDelimited" indexed="true" stored="true" />
<fieldType class="solr.TextField" name="caretDelimited">
    <analyzer>
        <tokenizer class="solr.PatternTokenizerFactory" pattern="\^"/>
    </analyzer>
</fieldType>

<uniqueKey>id</uniqueKey>

使用curl提交给索引的数据:

curl http://localhost:8983/solr/collection1/update?commit=true --data-binary @data.json -H 'Content-Type: application/json'

然后查询

http://localhost:8983/solr/collection1/select?
q=filter_amenity_codes:12&wt=json&indent=true

结果

"response":{"numFound":1,"start":0,"docs":[
  {
    "id":"123",
    "filter_amenity_codes":"1^6^7^8^12^13^14",
    "_version_":1475631637529100288}]
  }]}

如果您无法获得相同的输出,则导入过程可能会失败。查询*:*以查看您是否已实际索引任何值,尝试提交JSON文件以查看是否可搜索(这意味着导入失败),并确保正确设置SolrLogging以便记录任何错误。如果您对评论的查询接近正确,那么您缺少GROUP BY和任何名为FA等的表,因此我无法对此发表任何看法。