elasticsearch 弹性按祖先搜索查询

j8yoct9x  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(153)

我有一个ElasticSearch索引,包含以下文档:

"hits": [
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90323",
                "_source": {
                    "codeId": "90323",
                    "code": "A01",
                    "code_raw": "A01",
                    "description": "Desc for A01",
                },
            },
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90324",
                "_source": {
                    "codeId": "90324",
                    "code": "A01.2",
                    "code_raw": "A012",
                    "description": "Desc for A01.2",
                },
            },
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90325",
                "_source": {
                    "codeId": "90325",
                    "code": "A01.3",
                    "code_raw": "A013",
                    "description": "Desc for A01.3",
                },
            },
            {
                "_index": "icd",
                "_type": "icd10",
                "_id": "90326",
                "_source": {
                    "codeId": "90326",
                    "code": "A01.34",
                    "code_raw": "A0134",
                    "description": "Desc for A01.34",
                },
            },
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90327",
                "_source": {
                    "codeId": "90327",
                    "code": "A01.35",
                    "code_raw": "A0135",
                    "description": "Desc fro A01.35",
                },
                "sort": [
                    "Z1321"
                ]
            },
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90328",
                "_score": null,
                "_source": {
                    "codeId": "90328",
                    "code": "A01.356",
                    "code_raw": "A01356",
                    "description": "Desc for A01.356",
                },
            },
            {
                "_index": "myindex",
                "_type": "mytype",
                "_id": "90329",
                "_source": {
                    "codeId": "90329",
                    "code": "A01.359",
                    "code_raw": "A01359",
                    "description": "Desc for A01.359",
                },
            },
...
]

我通常使用类似于GET http://[host:port]/myindex/mytype/_search的查询来查询子对象:

"query":{
            "match_phrase_prefix":{
               "code_raw":"A01"
            }
         }

现在我需要反向任务,例如code_raw A01359,我需要文档A01359、A0135、A013和A01,有没有简单的方法使用elasticsearch?,或者我只是单独预处理每个文档的代码和查询?

zbwhf8kr

zbwhf8kr1#

我的建议是创建一个新的分析器,这个分析器使用标记器edge_ngram。
但是这个分析器只在搜索时使用search_analyzer。
这允许创建令牌,如:

{
  "analyzer": "my_analyzer",
  "text": ["A01359"]
}

令牌:

{
  "tokens": [
    {
      "token": "a01",
      "start_offset": 0,
      "end_offset": 3,
      "type": "word",
      "position": 0
    },
    {
      "token": "a013",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 1
    },
    {
      "token": "a0135",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 2
    },
    {
      "token": "a01359",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 3
    }
  ]
}

这样,就可以匹配其他文档的code_raw字段中的值。
完整示例:

PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer",
          "filter": [
            "lowercase"
            ]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 3,
          "max_gram": 6,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
      "properties": {
        "code_raw": {
          "type": "text",
          "analyzer":"standard", 
          "search_analyzer": "my_analyzer"
        }
      }
  }
}

POST test/_bulk
{"index":{}}
{"code_raw": "A01"}
{"index":{}}
{"code_raw": "A012"}
{"index":{}}
{"code_raw": "A013"}
{"index":{}}
{"code_raw": "A0134"}
{"index":{}}
{"code_raw": "A0135"}
{"index":{}}
{"code_raw": "A01356"}
{"index":{}}
{"code_raw": "A01359"}

质询

GET test/_search
{
  "query": {
    "match": {
      "code_raw": {
        "query": "A01359"
      }
    }
  }
}

结果:

"hits": [
  {
    "_index": "test",
    "_id": "Jy7yz4UBKJKciEqCsxO7",
    "_score": 1.6739764,
    "_source": {
      "code_raw": "A01"
    }
  },
  {
    "_index": "test",
    "_id": "KS7yz4UBKJKciEqCsxO7",
    "_score": 1.6739764,
    "_source": {
      "code_raw": "A013"
    }
  },
  {
    "_index": "test",
    "_id": "Ky7yz4UBKJKciEqCsxO7",
    "_score": 1.6739764,
    "_source": {
      "code_raw": "A0135"
    }
  },
  {
    "_index": "test",
    "_id": "LS7yz4UBKJKciEqCsxO7",
    "_score": 1.6739764,
    "_source": {
      "code_raw": "A01359"
    }
  }
]

相关问题