我们有一张叫订单的桌子。在这个表中,我有6列:id、agent_type、agent_id、agent_id、customer_number、created_at。
|id|agent_type |agent_id |amount|customer_number|created_at |
|--|-----------------------|----------|------|---------------|-------------------|
|1 |App\Models\Customer | 1 |50 |+88015456752 |2019-05-10 11:50:11|
|2 |App\Models\Customer | 1 |20 |+88015454589 |2019-05-11 11:50:11|
|3 |App\Models\Partner | 1 |30 |+88017457789 |2019-05-15 11:50:11|
|4 |App\Models\Customer | 3 |30 |+88016445649 |2019-05-14 11:50:11|我们正在获取一些基于customer_number的agent_type、agent_id和类似查询的数据-
select * from orders where agent_type = "App\Models\Customer" AND agent_id = 1 AND customer_number like "%01545%"现在我们想在ELASTICSEARCH.中查询那么,设置映射和分析器以及查询以获取数据的最佳方法是什么?
发布于 2021-03-06 17:19:42
您可以使用ngram令牌器在customer_number上进行部分匹配。
使用索引数据、映射、搜索查询和结果添加一个工作示例。
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 2,
"max_gram": 5,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 10
},
"mappings": {
"properties": {
"agent_type": {
"type": "keyword"
},
"agent_id": {
"type": "integer"
},
"amount": {
"type": "integer"
},
"customer_number": {
"type": "text",
"analyzer":"my_analyzer",
"search_analyzer":"standard"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"id": {
"type": "integer"
}
}
}
}搜索查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"agent_type": "App\\Models\\Customer"
}
},
{
"match": {
"agent_id": 1
}
},
{
"match": {
"customer_number": "01545"
}
}
]
}
}
}搜索结果:
"hits": [
{
"_index": "66505400",
"_type": "_doc",
"_id": "1",
"_score": 1.9400072,
"_source": {
"id": 1,
"agent_type": "App\\Models\\Customer",
"agent_id": 1,
"amount": 50,
"customer_number": "+88015456752",
"created_at": "2019-05-10 11:50:11"
}
},
{
"_index": "66505400",
"_type": "_doc",
"_id": "2",
"_score": 1.9400072,
"_source": {
"id": 2,
"agent_type": "App\\Models\\Customer",
"agent_id": 1,
"amount": 20,
"customer_number": "+88015454589",
"created_at": "2019-05-11 11:50:11"
}
}
]https://stackoverflow.com/questions/66505400
复制相似问题