简体   繁体   English

使用 elasticsearch js 中的术语

[英]using terms in elasticsearch js

because of the wonderful documentation Elasticsearch has, I cant figure out the proper syntax to search for a term, this is my code:由于 Elasticsearch 有精彩的文档,我无法找出搜索术语的正确语法,这是我的代码:

  let checkuser = await client.search({
        index: "users",
        type: "my_users",
        body: {
          query: {
            term: {
              email: req.body.email
            }
          }
        }
      });

I wish to search for an object that has a key value pair of 'email' with a certain email, but I wish it to be the exact email I wrote, if its a@mail.com ab@mail.com should not match, I know I need to use terms but when i write it like that it doesn't work, whats wrong with my syntax? I wish to search for an object that has a key value pair of 'email' with a certain email, but I wish it to be the exact email I wrote, if its a@mail.com ab@mail.com should not match,我知道我需要使用术语,但是当我这样写它不起作用时,我的语法有什么问题?

PS this is my index mapping: PS这是我的索引映射:

  "users" : {
    "mappings" : {
      "jobix_users" : {
        "properties" : {
          "confirmed" : {
            "type" : "boolean"
          },
          "email" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "firstName" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "jobNotification" : {
            "type" : "boolean"
          },
          "jobTitle" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "lastName" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "password" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "userName" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }

You use default mapping and this means that you use standard tokenizer when you index documents.您使用默认映射,这意味着您在索引文档时使用标准标记器。

How you can see in mapping email field have two interpretations:如何在映射 email 字段中看到有两种解释:

  1. text 文本
  2. keyword 关键词

In text standard tokenizer is work and save tokens to your index.在文本标准标记器是工作并将标记保存到您的索引。 This mean you can find for term alex and for term mail.com.这意味着您可以找到术语 alex 和术语 mail.com。 If you want to find whole email then your query should look as:如果您想查找整个 email 那么您的查询应如下所示:

{
    "query": {
        "term": {
            "email.keyword": req.body.email
        }
    }
}   

But elasticsearch has special uax_url_email tokenizer for urls and mails.但是 elasticsearch 具有用于 url 和邮件的特殊uax_url_email 标记器。 I would like to recommend use this tokenizer for the email field.我想建议将此标记器用于 email 字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM