使用特殊字符的ElasticSearch

ilmyapht  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(95)

我将PDF文件编入Elasticsearch索引。当我搜索类似“§ 123”的内容时,“§”被忽略了。

我需要做什么才能使“§”也包含在搜索中?

这里是索引。创建:

CreateIndexResponse createIndexResponse = elasticClient.Indices.Create(sep.DefaultIndex, c => c
    .Settings(s => s
        .Analysis(a => a
            .Analyzers(ad => ad
                .Custom("windows_path_hierarchy_analyzer", ca => ca
                    .Tokenizer("windows_path_hierarchy_tokenizer")
                )
            )
            .Tokenizers(t => t
                .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
                    .Delimiter('\\')
                )
                .NGram("ngram_tokenizer", td => td
                .MinGram(2)
                .MaxGram(20)
                .TokenChars(
                    TokenChar.Letter,
                    TokenChar.Digit,
                    TokenChar.Symbol)
                )
            )
        )
    )
    .Map<ElasticDocument>(mp => mp
        .AutoMap()
        .Properties(ps => ps
            .Text(s => s
                .Name(n => n.Path)
                .Analyzer("windows_path_hierarchy_analyzer")
            )
            .Object<Attachment>(a => a
                .Name(n => n.Attachment)
                .AutoMap()
            )
        )
    )
);

下面是Map:

{
  "attachments": {
    "mappings": {
      "properties": {
        "attachment": {
          "properties": {
            "author": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "content": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "content_length": {
              "type": "long"
            },
            "content_type": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "creator_tool": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "date": {
              "type": "date"
            },
            "description": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "format": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "keywords": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "language": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "metadata_date": {
              "type": "date"
            },
            "modified": {
              "type": "date"
            },
            "title": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "id": {
          "type": "long"
        },
        "instance": {
          "type": "long"
        },
        "path": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}
yebdmbv4

yebdmbv41#

Tldr;

正如@llermaly在Map中所指出的,您似乎没有使用您用代码创建的分析器。
默认情况下,text字段将使用标准分析器进行分析。

溶液

您需要指定要用于文本字段的分析器

{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "windows_path_hierarchy_analyzer"
      }
    }
  }
}

否则使用X1 E0 F1 X。
这将删除特殊字符。

POST _analyze
{
  "analyzer": "standard",
  "text": "§ 123"
}

为您提供:

{
  "tokens": [
    {
      "token": "123",
      "start_offset": 2,
      "end_offset": 5,
      "type": "<NUM>",
      "position": 0
    }
  ]
}

**§**已被删除。

相关问题