首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹性搜索的映射与搜索

弹性搜索的映射与搜索
EN

Stack Overflow用户
提问于 2021-03-06 12:00:22
回答 1查看 41关注 0票数 0

我们有一张叫订单的桌子。在这个表中,我有6列:id、agent_type、agent_id、agent_id、customer_number、created_at

代码语言:javascript
复制
|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_numberagent_type、agent_id和类似查询的数据-

代码语言:javascript
复制
select * from orders where agent_type = "App\Models\Customer" AND agent_id = 1 AND customer_number like "%01545%"

现在我们想在ELASTICSEARCH.中查询那么,设置映射和分析器以及查询以获取数据的最佳方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-06 17:19:42

您可以使用ngram令牌器customer_number上进行部分匹配。

使用索引数据、映射、搜索查询和结果添加一个工作示例。

索引映射:

代码语言:javascript
复制
{
  "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"
      }
    }
  }
}

搜索查询:

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "agent_type": "App\\Models\\Customer"
          }
        },
        {
          "match": {
            "agent_id": 1
          }
        },
        {
          "match": {
            "customer_number": "01545"
          }
        }
      ]
    }
  }
}

搜索结果:

代码语言:javascript
复制
 "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"
        }
      }
    ]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66505400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档