Wildcard with space

Hi,

I’m using wildcardquery in java sdk. It’s working if the parameter is only one word, but if there’s a space in between, it’s returning 0 results. Sample code below:

public static void wildCardQueryMethod(Bucket bucket) {
String indexName = “travel-sample-index-stored”;
WildcardQuery query = SearchQuery.wildcard(“pretty girl”).field(“description”);

    SearchQueryResult result = bucket.query(
    new SearchQuery(indexName, query).limit(10).highlight());

    printResult("Wild Card Query", result);
}

Hi @phaigne,

This happens mostly due to the text analysis happening at the indexing time.
Since you haven’t shared any index definition details, I can’t pinpoint the exact problem here.

Mostly, your index time default (inherited) standard analyser for the description field would tokenise the input text like “pretty girl” to “pretty” and “girl”. So these two tokens goes to the FTS index.
You may explore more here ref - http://analysis.blevesearch.com/analysis

Later when you search over a wildcard query with multiple tokens like this - “pretty girl”, this multiple tokens are getting searched as a single token. But now the index doesn’t contain a token like “pretty girl”, it fails there.
(In general, the query texts also undergoes the same index time text analysis automatically. But unlike other queries, FTS don’t do apply text analysis to the wildcard query texts as that would result in undesired side effects to the original wildcard pattern/texts)

You have to create/pick the right text analysers for your fields getting indexed depending on your query time requirements.
ref - https://docs.couchbase.com/server/current/fts/fts-using-analyzers.html

Cheers!

Hi @sreeks ,

Thanks for the great feedback! I’m new to FTS and I’m just using the simple analyzer. Is there any suggested analyzer in my situation? If I type for example “pre gir”, it should return the document with the “pretty girl” in the description field.

@phaigne,

There are many ways to do this. You may take some time to understand your requirements and play around with the analysers and query types FTS has to get the best search performance.

Couple of ways to do this,

Wildcard query

  "query": {
    "conjuncts": [
      {
        "wildcard": "pre*",
        "field": "fieldName"
      },
      {
        "wildcard": "gir*",
        "field": "fieldName"
      }
    ]
  }
}

Or a prefix query

{
  "query": {
    "conjuncts": [
      {
        "prefix": "pre",
        "field": "fieldName"
      },
      {
        "prefix": "gir",
        "field": "fieldName"
      }
    ]
  }
}

@sreeks,

Thank you so much! I’ll play around these snippets you sent.