Fetching numbers from contacts using content resolver query - special case - java

I am trying to query phone number stored in the contacts database of android.
I am providing an input sting for comparison and I need the query to pick only those rows which matches this input string.
While querying the contacts db, I would like the query to ignore all the special characters contained in a number and compare the input string with only the digits contained in the field.
eg; if the stored number is (944) 6-129-337 , the query should compare the input string with 9446129337 and not with (944) 6-129-337 .
Following is the code with which I query the db and fetch the numbers.
contactCursor = getContentResolver().query(uri, projection,
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " +
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ? ",new String[] { input_string }, null);
The problem I have here is, it just does a plain search in each phone number field without internally removing(using regexp or modifying the query?) the special characters.
I do not know how to do that cleaning up of special characters inside query. Any help in this regard which would help me resolve this query issue will be well appreciated
Thanks,
Anees

As a hack that may work for you... you could pass your number using wildcards between every digit, and then re-filter the results in your Java code... e.g. ... LIKE '%9%4%4%6%1%2%9%3%3%7%' ... is liable to return very few results, including (944) 6-129-337 and (perhaps) +1 (944) 612-9337; but you can collect these results, strip the non-digit values in your own code, and compare them. If you have very short search strings, though, your number of "false positive" hits will be much greater. Unfortunately, I haven't tested this with the Android content system, so it's plausible that you could overflow the number of wildcard characters permitted, or similar :-(

Use This
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/%915894%");
Return Only your need.

Related

Ordering Sql result based on number of token matches from RLIKE

I am trying to implement a simple search query, where I am splitting the search text into tokens and then returning all results that contain any of the tokens, I am using RLIKE 'token1|token2|token3|...', this is working correctly and returning all the result, but now I would want to Order the result by the numbers of tokens from the RLIKE that matches, is that anyway possible anyway? thanks in advance.
SELECT p.* FROM product p
WHERE p.title RLIKE 'token1|token2|token3';
You can use the operator LIKE for each of the tokens in the ORDER BY clause:
ORDER BY (p.title LIKE '%token1%') +
(p.title LIKE '%token2%') +
(p.title LIKE '%token3%') DESC
Each of the boolean expressions p.title LIKE '%tokenX%' evaluates to 1 for true or 0 for false.

Solr Query: replacing whitespace with +

The application I'm working on uses solr to index and search entries. I've been reading a bit about the logic and syntax behind in. Currently there's a bit of code that I'm confuses me and I'm hoping someone can clear up why the person who wrote this bit of code did it the way they did.
trimmedSearchField = SolrQueryUtil.escapeQueryString(trimmedSearchField).replaceAll("\\s+", "+");
String qString = "+(title:" + trimmedSearchField + "^100 OR description_t:" + trimmedSearchField + "^10 " +
"OR +" + trimmedSearchField +"^1)";
I'm just wanting bring attention to the .replaceAll method, why would we want to replace whitespace with +? My goal is to refactor a bit a search bar and I get better results ommitting the replaceAll call.
Example: two elements with the descriptions: "Helen of Troy" and "Helen from Troy" respectively. With replaceAll present, searching "Helen of Troy" will provide me with only the first element, with replaceAll removed, both will appear (which is what I want to occur)
that .replaceAll() call is just encoding any series of consecutive whitespaces into a single '+', which mean 'required' in lucene syntax (and Solr)
So it makes 'trimmedSearchField' mandatory in those fields.

Nrs in Endeca query is not fetching results when we give encoded value along with English character in url

We are using Endeca to fetch the records since they are huge in number. We have a dataTable at frontend that displays the records fetched from Endeca through Endeca query.
Now, when we filter the results based on the checkbox values at frontend, query appends Nrs attribute and get the filtered results. For any chinese or russian or special characters, we encode them and create the query. Example:
N=0&Ntk=All&Ntx=mode+matchall&Ntt=rumtek&Nrs=collection()/record[(customerName="%22RUMTEK%22+LTD.")]&No=0&Ns=,Endeca.stratify(collection()/record[not%20(invoiceDate)])||invoiceDate|1||,Endeca.stratify(collection()/record[not%20(invoiceNumber)])||invoiceNumber|1
In above query, results are fetched based on value "rumtek" and we apply filter by giving value as ""RUMTEK" LTD.". After encoding, filter value is converted to "%22RUMTEK%22+LTD.". This query fetches no result.
Results are fetched when we either give the complete encoded term (like for any chinese word we give encoded value) or any English word. Results are not fetched when give terms containing double quotes(") example "ABC" LTD. or AB&C (AB%26C).
One more issue is:- what if we have made AB as Stop word (words that won't be searched). If we search for AB&C, then would it search the results for AB&C or it world make the entire term as stop word.
Any suggestion will be appreciated.
Thanks in Advance.
First, you need to make sure that your Nrs parameter is entirely and properly URL encoded. Second, you need to make sure you properly escape your double quotes because you want to match against them.
As you said, your data contains some record whose customerName property is (without brackets) ["RUMTEK" LTD.]. According to the MDEX Development Guide, to use double quotes as a literal value you need to escape it by prepending it with a double quote character (how confusing!). So, in order to match on this, you would need to have a query string like (separated into lines for readability):
N=0&
Ntk=All&
Ntx=mode+matchall&
Ntt=rumtek&
Nrs=collection()/record[(customerName="""RUMTEK"" LTD.")]&
&No=0&
Ns=,Endeca.stratify(collection()/record[not%20(invoiceDate)])||invoiceDate|1||,Endeca.stratify(collection()/record[not%20(invoiceNumber)])||invoiceNumber|1
Now, it isn't ready yet. You need to URL encode the ENTIRE Nrs parameter value. So it would become:
N=0&
Ntk=All&
Ntx=mode+matchall&
Ntt=rumtek&
Nrs=collection%28%29%2Frecord%5B%28customerName%3D%22%22%22RUMTEK%22%22+LTD.%22%29%5D&
&No=0&
Ns=,Endeca.stratify(collection()/record[not%20(invoiceDate)])||invoiceDate|1||,Endeca.stratify(collection()/record[not%20(invoiceNumber)])||invoiceNumber|1
That should get you what you need without having to resort to wildcard queries.

QueryBuilders based query in elasticsearch for searching text mixed with special characters(John-one) across multple fields?

I use This query in my application for getting all matched records without special characters from across all fields of my database collection.Here I pass q value as run time search text(John).Now problem is when I search for text(John-One) with special characters it's returns 0.
QueryBuilder queryBuilderForUserSearch =
QueryBuilders.boolQuery().must(QueryBuilders.fieldQuery("active", Boolean.TRUE))
.must(QueryBuilders.fieldQuery("_all", "*" + q + "*"));
Here q is my passing text. It's working fine when I pass only text (like John) without special characters(like John-one). But I should fetch all records with special characters also.
The problem is, elasticsearch tokenizes the data at the time of indexing when special characters are encountered.
Try marking your fields as "not_analyzed". This will store your field value "as is" in index without tokenizing it. This will cause the side effect that your normal term queries, without wildcards will not work as expected.
Relevant mapping snippet.
{
"field1":{
"type":"string",
"index":"not_analyzed",
"store":true
},
"field2":{
"type":"string",
"index":"not_analyzed",
"store":true
}
}

Search java string for 'special' characters before inserting into derbyDB varchar field

I am trying to convert from MS Access to DerbyDB. However some varchar fields have 'special' characters, such as newlines, tabs, percent marks, foreign characters etc.
I created a quick method...
public String charCheck(String s)
{
errLog.add(1, "converting string from " + s);
s.replaceAll("'", "''");//an apostrophe is escaped with an apostrophy in Derby...
s.replaceAll("%", "\\%");//a percent sign
s.replaceAll("\\s+n", " ");//whitespace characters (newlines and tabs etc)
s.replaceAll("/", "\\/");//the 'divide' \ character,
s.replaceAll("<", "\\<");//mathematical symbol less than
s.replaceAll(">", "\\>");//mathematical symbol greater than
errLog.add(1, "to " + s);
return s;
}//end method
Which I run whenever I determine that I need a varchar (or long varchar) data type. The strange thing is that my error log prints out the messages, but in the output whitespace characters do not appear to change (ie tabs and new lines, do not get converted to a simple space) and any apostrophe in the string is not replaced.
a sample of the output of this method produces the following.
converting string from 2. FIN DE L’ESSAI
to 2. FIN DE L’ESSAI
So the string remains obviously unchanged, which upsets derbyDB when I run the insert statement, also I am not finding any obvious documentation on the escape sequence for inserting multiple records into a table, I would like to use a statement then add the escape keyword after it, ie
stmt.execute("{call "+ sqlInsertStatement +"}{escape '" + escapeCharacter +"'" );
I also read from the docs that the escape keyword may not be usefull in the above statement, if so how can I te
I need to know where to go to sort the insert error that I get.
If I copy and past the insert statement directly into ij, then remove the special characters the record will insert fine, I just don't understand why it isn't being converted in the first instance.
I have also tried surrounding varchar and longvarchar fields with double quotes, but again derby kicks out an error saying that a double quote was found!
I want to get this sorted as I feel like I am so close...
Thanks in advance
Strings are immutable, all operations you perform on them results new Strings. You need to assign current reference to new String.
Example:
s= s.replaceAll("'", "''");
If it is just replace, then replace() may be best option than using replaceAll()
Instead of working through the String, turn the String into a char array, then use a do...while loop to work through each char in turn using if tests to replace each char. Then turn the array back to a String then return it

Categories