How to search for username with hyphen

  • Hello,
    assume having two users
    Seemingly the hyphen ist like a special char and won't work with asterisk for like-search.
    You can try this in the web UI:
    Search for username:test -> Users will be found
    Search for username:test* -> Users will be found
    Search for username:test-* -> No search result
    Quoting the hyphen with backslash has no effect. Is there a way to achieve this?

    Thank you and regards

  • Hmmm.

    Which search engine are you using, elasticsearch or the database search engine?

    What version of FusionAuth are you using?

  • Hey @dan
    this seemingly occurs with V1.23.3 and also with V1.17.4.
    And with both search engines, database and elasticsearch.

  • @malle said in How to search for username with hyphen:

    Search for username:test-* -> No search result

    Does this work?

    Search for username:"test-*"

  • Hi @malle ,

    Took some time to dig into this a bit more.

    I have a system which has a user with a username test2-name.

    If I use the database search for this user, all of these return the user:

    • test2
    • test2*
    • test2-
    • test2-*
    • test2-n
    • test2-n*

    Note that you don't put the username: prefix on any database searches.

    If on the other hand I use the elasticsearch search engine, and search for the same user, I see the following:

    • username:test2 -> found
    • username:test2* -> found
    • username:test2- -> found
    • username:test2-* -> not found
    • username:test2-n -> found
    • username:test2-n* -> not found

    I'm not quite sure what's going on with the wildcard causing Elastic to not find the value, but don't think it is related to the -.

  • @dan

    Did you try quoting the value?

    • username:"test2-*"
    • username:"test2-n*"

  • Running with quotes works in one case:

    • username:"test2-*" -> found
    • username:"test2-n*" -> not found

  • @dan
    username:"test2-n*" -> not found still a problem.
    And I do not really care about the FusionAuth Web UI, the problem actually is that I use the Java client via UserSearchCriteria it is quoted anyways. Unfortunetally there are already a lot of 'hyphen-user' in the system and adjusting these by taking the hyphen out is not an option.

  • I think the problem is that we are indexing username with the default text analyzer which uses a hyphen (-) as a separating character.

    This means Elasticsearch has indexed test-good and found two tokens test and good.

    Without changing the way this field is indexed, I think you can achieve your desired results by modifying your query.

    For example, to search for test-g*, you would break this into two parts, and structure your search like this: username:"test" AND username:g*. This will find all usernames that include the token test and a have a second token token that begins with g.

    Try this strategy and see if it works for you.

  • In the meantime we could implemented it with the mentioned workaround.
    Late thanks @robotdan !

Log in to reply

Looks like your connection to FusionAuth Forum was lost, please wait while we try to reconnect.