简体   繁体   中英

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:

  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?

PS this is my index mapping:

  "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:

  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. If you want to find whole email then your query should look as:

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

But elasticsearch has special uax_url_email tokenizer for urls and mails. I would like to recommend use this tokenizer for the email field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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