【问题标题】:Elastic Search full string match not working弹性搜索完整字符串匹配不起作用
【发布时间】:2020-10-06 15:52:19
【问题描述】:

我正在使用 Elastic builder npm

使用esb.termQuery(Email, "test")

映射:

"CompanyName": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }

数据库字段:

"Email": "test@mycompany.com",
"CompanyName": "my company"

查询 JSON:{ term: { CompanyName: 'my' } }。或{ term: { Email: 'test' } } 结果:

"Email": "test@mycompany.com",
    "CompanyName": "my company"

期望: 没有结果,需要全文匹配,这里的匹配相当于'like'或者queryStringQuery。

我有 3 个过滤器前缀、完全匹配、包含。

【问题讨论】:

  • 您有没有机会浏览我的答案,期待您的反馈????如果我的回答帮助您解决了问题,请不要忘记点赞并接受我的回答????

标签: elasticsearch elasticsearch-query


【解决方案1】:

standard analyzer 是默认分析器,如果没有则使用 指定的。它提供基于语法的标记化

在您的示例中,也许您没有在索引映射中明确指定任何分析器,因此默认分析文本字段,标准分析器是它们的默认分析器。 请参阅此SO answer,以获取对此的详细说明。

如果未定义分析器,则会生成以下标记。

POST/_analyze 

{
  "analyzer" : "standard",
  "text" : "test@mycompany.com"
}

代币是:

{
  "tokens": [
    {
      "token": "test",
      "start_offset": 0,
      "end_offset": 4,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "mycompany.com",
      "start_offset": 5,
      "end_offset": 18,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

如果您想要全文搜索,那么您可以定义一个带有小写过滤器的自定义分析器,小写过滤器将确保在索引文档和搜索之前将所有字母更改为小写。

关键字字段的normalizer属性类似于analyzer 除了它保证分析链产生一个单一的 令牌。

uax_url_email 分词器与标准分词器类似,不同之处在于 它将 URL 和电子邮件地址识别为单个令牌。

索引映射:

{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "uax_url_email"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "CompanyName": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      },
      "Email": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

索引数据:

{
  "Email": "test@mycompany.com",
  "CompanyName": "my company"
}

搜索查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "CompanyName": "My Company"
          }
        },
        {
          "match": {
            "Email": "test"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64220291",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "Email": "test@mycompany.com",
          "CompanyName": "my company"
        }
      }
    ]

【讨论】:

  • 在 CompanyName 中使用带有大写字母的关键字问题。
  • “公司名称”:“我的公司”
  • @Amit Rana 你想匹配大写字母,即My Company 吗?
  • 前端我可以同时获得资本和小额资金。我正在转换为小写,然后使用我在 Elastic 上搜索的术语。如果我使用关键字,则没有小写的结果
  • @Amit Rana 好的,电子邮件的条件是什么,您希望它不应该拆分吗?
猜你喜欢
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多