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

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

我要解决的问题是,我有一堆有关数学表达式/公式的文档。我想按公式或表达式搜索文档。

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

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

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

elasticsearch indexing lucene latex mathml
1个回答
0
投票

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

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

{
    "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"
                }
            }
        ]
© www.soinside.com 2019 - 2024. All rights reserved.