您有多个要求,所有这些都可以使用
- 创建一个自定义分析器,根据我们的要求对数据进行标记。
- 使用结合前缀(用于自动完成)和匹配的布尔查询进行数字搜索。
下面是使用 OP 数据和查询的分步示例。
索引定义
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "autotoken" -->used your analyzer to extract numbers
}
},
"tokenizer": {
"autotoken": {
"type": "simple_pattern",
"pattern": "[0-9]+",
"preserve_original": true
}
}
}
},
"mappings": {
"properties": {
"code": {
"type": "keyword",
"fields": {
"number": {
"type": "text",
"analyzer" : "my_analyzer"
}
}
}
}
}
}
索引几个文档
{
"code" : "hcc420"
}
{
"code" : "HCC23"
}
{
"code" : "I23"
}
{
"code" : "I420"
}
{
"code" : "I421"
}
{
"code" : "hcc420"
}
搜索查询(问题 1,搜索I420
,应该在样本数据中带来 2 个文档,I420
但hcc420
必须I420
有更多的分数作为完全匹配)
{
"query": {
"bool": {
"should": [
{
"prefix": {
"code": {
"value": "I420"
}
}
},
{
"match": {
"code.number": "I420"
}
}
]
}
}
}
结果
"hits": [
{
"_index": "so_number",
"_type": "_doc",
"_id": "4",
"_score": 2.0296195, --> note exact match having high score
"_source": {
"code": "I420"
}
},
{
"_index": "so_number",
"_type": "_doc",
"_id": "7",
"_score": 1.0296195,
"_source": {
"code": "hcc420"
}
}
]
第 2 部分:相同的搜索查询可以使用自动完成功能
所以搜索I42
必须带来I420
和I421
来自示例文档
{
"query": {
"bool": {
"should": [
{
"prefix": {
"code": {
"value": "I42"
}
}
},
{
"match": {
"code.number": "I42"
}
}
]
}
}
}
结果
"hits": [
{
"_index": "so_number",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"code": "I420"
}
},
{
"_index": "so_number",
"_type": "_doc",
"_id": "5",
"_score": 1.0,
"_source": {
"code": "I421"
}
}
]
让我们再举一个号码搜索的例子,搜索420
必须带hcc420
和I420
搜索查询
{
"query": {
"bool": {
"should": [
{
"prefix": {
"code": {
"value": "420"
}
}
},
{
"match": {
"code.number": "420"
}
}
]
}
}
}
And whoa, again it gave expected results
Result
------
"hits": [
{
"_index": "so_number",
"_type": "_doc",
"_id": "4",
"_score": 1.0296195,
"_source": {
"code": "I420"
}
},
{
"_index": "so_number",
"_type": "_doc",
"_id": "7",
"_score": 1.0296195,
"_source": {
"code": "hcc420"
}
}
]