Home > Software design >  Regex query not works in EleasticSearch but works in java
Regex query not works in EleasticSearch but works in java

Time:10-28

The Regex works in java but is not woked in ElasticSearch.

Java:


Pattern pattern = Pattern.compile("(\\d{8}-[01],)*(((202210((2[89])|(3[01])))|(2022((1[12]))\\d{2})|(20((2[3-9])|([3-9][0-9]))\\d{4}))-[01])*([,]\\d{8}-[01])*");
Matcher matcher = pattern.matcher("20221027-0,20221028-1");
System.out.println(matcher.matches());

It prints true

But when I using EleasticSearch, it was not woked.

The folloing json is the document what I want to query in EleasticSearch.


{
    "_index": "eagle_clue_v1",
    "_type": "_doc",
    "_id": "51740",
    "_score": 0.0,
    "_source": {
        "id": 51740,
        "next_follow_time": "20221027-0,20221028-1"
    }
}

The following query was not worked

POST /eagle_clue_v1/_search
{
    "from": 0,
    "size": 10,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "filter": [
                            {
                                "terms": {
                                    "id": [
                                        "51740"
                                    ]
                                }
                            },
                            {
                                "regexp": {
                                    "next_follow_time.keyword": {
                                        "value": "(\\d{8}-[01],)*(((202210((2[89])|(3[01])))|(2022((1[12]))\\d{2})|(20((2[3-9])|([3-9][0-9]))\\d{4}))-[01])([,]\\d{8}-[01])*"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

CodePudding user response:

Check this page for regular expression syntax.

  1. use [0-9] instead of \d.
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "filter": [
              {
                "regexp": {
                  "next_follow_time.keyword": {
                     "value": """([0-9]{8}-[01],)*(((202210((2[89])|(3[01])))|(2022((1[12]))[0-9]{2})|(20((2[3-9])|([3-9][0-9]))[0-9]]{4}))-[01])([,][0-9]{8}-[01])*"""
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
  • Related