在弹性搜索中索引包含数学表达式的文档的最佳方法是什么?

What is the best way to index documents which contain mathematical expression in elastic search?

这里我要解决的问题是我有一堆上下文数学的文档expressions/formulas。我想通过公式或表达式搜索文档。

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

通过这种方法我可以搜索带有 latex 字符串的文档吗?

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

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

使用自定义分析器的索引定义

{
    "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"
                }
            }
        ]