SOLR查询用逗号分隔的字段,无顺序

时间:2018-07-24 16:05:06

标签: solr lucene

我有一个字段,其中包含逗号分隔的值,例如JSON,AngularJS,另一个字段为AngularJS,JSON,而其他字段仅包含JSON,HTML。

现在我一直在尝试使用fq = field:( JSON AngularJS *)查询SOLR,但它只返回AngularJS之前带有JSON的记录。

如何查询SOLR,以便它返回具有JSON和AngularJS但不考虑顺序的记录。

为该字段附加SOLR分析: Analysis for the field

查询形式: http://localhost:8983/solr/my_core/select?fq=field:(JSON%20AND%20AngularJS)&q=

1 个答案:

答案 0 :(得分:1)

使用基于,标记的字段类型(即,列表中的每个条目都会产生一个单独的标记)。您可以使用SimplifiedRegularExpressionPatternTokenizer

<fieldType name="text" class="solr.TextField">
  <analyzer>
    <tokenizer class="solr.SimplePatternTokenizerFactory" pattern="[^,]+"/>
  </analyzer>
</fieldType>

通过索取两个令牌都fq=field:(JSON AND AngularJS)的文档来查询索引。

(问题更新后)

首先-您的字段似乎是 string 字段,而不是TextField。

Example definition from UI

要通过API添加具有正确定义的字段:

curl -X POST -H 'Content-type:application/json' --data-binary '{
  "add-field-type" : {
     "name":"comma-separated-list",
     "class":"solr.TextField",
     "positionIncrementGap":"100",
     "analyzer" : {
        "tokenizer":{
           "class":"solr.SimplePatternTokenizerFactory", "pattern": "[^,]+" },
        }
     }
  }
}' http://localhost:8983/solr/collectionname/schema

添加一组示例文档后:

[
      {
        "langs":"JSON,AngularJS,Microsoft Visual Basic",
        "id":"foo",
        "address":"None",
        "_version_":1606953238273196032},
      {
        "langs":"JSON,AngularJS",
        "id":"foo2",
        "address":"None",
        "_version_":1606953238277390336},
      {
        "langs":"JSON,Microsoft Visual Basic",
        "id":"foo3",
        "address":"None",
        "_version_":1606953238278438912},
      {
        "langs":"AngularJS,JSON",
        "id":"foo4",
        "address":"None",
        "_version_":1606953238278438913}]

然后使用fq=langs:(JSON AND AngularJS)&q=*:*)进行查询:

  {
    "langs":"JSON,AngularJS,Microsoft Visual Basic",
    "id":"foo",
    "address":"None",
    "_version_":1606953238273196032},
  {
    "langs":"JSON,AngularJS",
    "id":"foo2",
    "address":"None",
    "_version_":1606953238277390336},
  {
    "langs":"AngularJS,JSON",
    "id":"foo4",
    "address":"None",
    "_version_":1606953238278438913}]

未定义AngularJS的文档已被忽略。