简体   繁体   中英

Elasticsearch index field with wildcard and search for it

I have a document with a field "serial number". That serial number is ABC.XXX.DEF where XXX indicates wildcards. XXX can be \\d{3}[a-zA-Z0-9].

So users can search for:

ABC.123.DEF

ABC.234.DEF

ABC.XYZ.DEF

while the document only includes

ABC.XXX.DEF

When a user queries ABC.123.DEF i need a hit on that document containing ABC.XXX.DEF. As other documents might contain ABC.DEF.XXX and must not be hit I am running out of ideas with my basic elasticsearch knowledge.

Do I have to attack the problem from the query side or when analyzing/tokenizing the pattern?

Can anyone give me an example how to approach that problem?

As long as serial number is well defined the first solution that comes to my mind is to split serial number into three parts ("part1", "part2" and "part3", for example) and index them as three separate fields. Parts consisting of wildcards should have special value or may not be indexed at all. Then at query time I would split serial number provided by user in the same way. Assuming that parts consisting of wildcards are not indexed my query would look like this:

"query": {
  "bool": {
    "must":[
      {
        "bool": {
          "should": [
            {
              "match": {
                "part1": "ABC"
              }
            },
            {
              "bool": {
                "must_not": {
                  "exists": {
                    "field": "part1"
                  }
                }
              }
            }
          ]
        }
      },
      ... // Similar code for other parts
    ] 
  }
}

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