在 MongoDB Atlas 中搜索包含特殊字符的

问题描述 投票:0回答:2

当我尝试搜索包括特殊字符(部分符号“§”)在内的多个单词时,我遇到了这个问题。 示例:AB § 32。 我希望所有单词“AB”、“32”和符号“§”都包含在找到的文档中。 在某些情况下可以找到文档,在某些情况下则找不到。 如果我的文档包含以下文本,则搜索会找到它:

Lagrum: 32 § 1 mom. första stycket a) kommunalskattelagen (1928:370) AB

但是如果文档包含此文本,则搜索找不到:

Lagrum: 32 § 1 mom. första stycket AB

对于符号“§”,我使用 UT8 编码“\xc2\xa7”。

索引使用“lucene.swedish”分析器。

      "Content": [
        {
          "analyzer": "lucene.swedish",
          "minGrams": 4,
          "tokenization": "nGram",
          "type": "autocomplete"
        },
        {
          "analyzer": "lucene.swedish",
          "type": "string"
        }
      ]

查询如下:

{
    "index": "test_index",
    "compound": {
        "filter": [
            {
                "text": {
                    "query": [
                        "111111111111"
                    ],
                    "path": "ProductId"
                }
            },
        ],
        "must": [
            {
                "autocomplete": {
                    "query": [
                        "AB"
                    ],
                    "path": "Content"
                }
            },
            {
                "autocomplete": {
                    "query": [
                        "\xc2\xa7",
                    ],
                    "path": "Content"
                }
            },
            {
                "autocomplete": {
                    "query": [
                        "32"
                    ],
                    "path": "Content"
                }
            }
        ],
    },
    "count": {
        "type": "lowerBound",
        "threshold": 500
    }
}

问题是搜索出了什么问题以及如何获得正确的结果(返回上述两个文档)?

mongodb-query mongodb-compass mongodb-atlas-search
2个回答
0
投票

仅关注

content
字段,这里是一个应该满足您的要求的索引定义。文档位于here。让我知道这是否适合您。

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "content": [
        {
          "type": "autocomplete",
          "tokenization": "nGram",
          "minGrams": 4,
          "maxGrams": 7,
          "foldDiacritics": false,
          "analyzer": "lucene.whitespace"
        },
        {
          "analyzer": "lucene.swedish",
          "type": "string"
        }
      ]
    }
  }
}

0
投票

将两个选项的分析器更改为关键字以搜索特殊字符

© www.soinside.com 2019 - 2024. All rights reserved.