【问题标题】:What is the best way to index documents which contain mathematical expression in elastic search?在弹性搜索中索引包含数学表达式的文档的最佳方法是什么?
【发布时间】:2020-07-12 13:49:19
【问题描述】:

我要解决的问题是我有一堆文档,其中包含数学表达式/公式。我想通过公式或表达式搜索文档。

到目前为止,根据我的研究,我正在考虑将数学表达式转换为乳胶格式并作为字符串存储在数据库中(弹性搜索)。

通过这种方法,我可以使用乳胶字符串搜索文档吗?

a2 + b2 = c2 的示例乳胶转换是 a^{2} + b^{2} = c^{2} 。这个字符串可以在弹性搜索中搜索到吗?

【问题讨论】:

  • 你能举出这些表达的例子吗?以及你想如何搜索它们
  • 例如,我的文档包含以下字符串:我最喜欢的公式是 a2 + b2 = c2。我需要有一种机制,我需要在其中搜索所有文档公式 a2 + b2 = c2。注意:虽然我插入可以将公式转换为乳胶格式。
  • 是的,您将能够做到。但是您必须为此字符串配置分析器。查看文档:elastic.co/guide/en/elasticsearch/reference/7.x/analysis.html
  • 嗨 Gillespie59,我的字符串将是 a2 + b2 = c2 的乳胶转换,即 a^{2} + b^{2} = c^{2} 。这个字符串也可以搜索吗?
  • 如果您使用关键字类型索引字段,则不会分析您的字符串,您将能够搜索确切的字符串 "a^{2} + b^{2} = c^{ 2}”。如果您想搜索两种精确格式,您必须创建一个子字段(或另一个字段),这将是另一种格式,这两个字段应该是关键字。请注意,解决方案是搜索确切的字符串而不是表达式的一部分,它也区分大小写。

标签: elasticsearch indexing lucene latex mathml


【解决方案1】:

我同意用户@Lue E 的一些修改,并尝试使用简单的关键字方法,但给了我一些问题,因此我修改了在我自己的custom analyzer 中使用keyword 标记器的方法,这应该可以解决大部分问题您的用例。

使用自定义分析器索引 def

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_custom_analyzer": {
                    "type": "custom",
                    "tokenizer": "keyword", --> to make it searchable
                    "filter": [
                        "lowercase", --> case insensitive search
                        "trim" --> remove extra spaces
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "mathformula": {
                "type": "text",
                "analyzer": "my_custom_analyzer"
            }
        }
    }
}

索引示例文档

 {
        "mathformula" : "(a+b)^2 = a^2 + b^2 + 2ab"
    }

{
    "mathformula" : "a2+b2 = c2"
}

搜索查询(匹配查询,使用同一个索引时间分析器)

{
    "query": {
        "match" : {
            "mathformula" : {
                "query" : "a2+b2 = c2"
            }
        }
    }
}

搜索结果只包含第一个索引的文档

 "hits": [
            {
                "_index": "so_math",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.6931471,
                "_source": {
                    "mathformula": "a2+b2 = c2"
                }
            }
        ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2015-12-18
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多