How to query a clause for solr java client? - java

I am now using solr to query .I want to find all the documents whose key "title" contains text "Bifidobacterium bifidum" or key "abstract" contains text "Bifidobacterium bifidum".So , I write my query like below:
String queryCondition = "title:*Bifidobacterium bifidum* OR abstract:*Bifidobacterium bifidum*";
From the result ,I find out that the returned result is not what I want ,documents whose title contains "Bifidobacterium" or "bifidum" , or whose title contains "Bifidobacterium" or "bifidum" are all returned . So , my question is ,how should I write my query to satisfy my query need?

The * is special symbol, a wildcard. Similar to regular expressions, it tells Solr to match everything. So querying for bifidum* would return everything that starts with bifidum. Not what you want, right?
When reading about Solr's query syntax in the manual, you will find a section named Specifying Terms for the Standard Query Parser there is written
A phrase is a group of words surrounded by double quotes such as "hello dolly"
This is what you need ...

Related

How to get exact search using lucene

When we searching like "prd gem". It returns all results names with prd gem.
but when we search only "prd", it returns all results with prd in it like prd,prd gem, prd time etc. Why not exact search now?
Following was code in picture:
luceneQuery = queryBuilder.phrase()
.onField("productId")
.andField("productName").andField("refId")
.sentence(searchText)
.createQuery();
Exact search is working fine with the name having space in it like if i search "Prd Gem", it shows only one product with name "Prd Gem", but when i search only a word like "prd", exact search is not working, it shows all product like "prd","prd gem"
So what changes need to be done with above code to implement the same?
Thats because you are "tokenizing" the data in the lucene index.
Lucene by default will try to break the strings into tokens in order to allow and speed up such searches.
I assume you are using the latest hibernate. Can you try to annotate the "productName" field with the following:
#Field(name = "productName", index = Index.YES, analyze = Analyze.NO, norms = Norms.NO)
The "analyze = Analyze.no" part should disable this feature.

Spring Data Elasticsearch containing query with spaces

I have an entity named Port with a field portName. I wrote following Spring Data ES query method for containing query:
List<Port> ports = portRepository.findByPortNameContaining(searchText);
It is working fine until the searchText doesn't contain any spaces. If it does, I get the following error:
"Cannot constructQuery '*\"sample port\"*'. Use expression or multiple clauses instead."
When I try Spring Data ES search method as:
List<Port> ports = Lists.newArrayList(portRepository.search(
queryStringQuery(searchText)
.field("portName")
));
If I have a port named Loui Kentucky, I am only able to get results when the searchText is exactly a complete word like Loui or Kentucky or Loui Kentucky. Same happens with analyzeWildcard:
List<Port> ports = Lists.newArrayList(portRepository.search(
boolQuery().should(queryStringQuery(searchText).analyzeWildcard(true).field("portName"))
));
I want to construct a simple containing query which can handle spaces as well. No fuzziness. Search results should appear even when I search for i K as Loui Kentucky contains that substring.

hibernate Query to find record based on substring

I want to make a Hibernate query to check if a string contains a substring.
Suppose a user class having id,name,info.
info is String which contain multiple substrings.
For example info contains strings like "hi I am from Pune".
I want to read all record which contain Pune as substring.
I tried using like query but not working.
Criteria criteria = session.createCriteria(Post.class);
criteria.add(Restrictions.like("content",contentStringToLook));
users = (List<Post>)criteria.list();
Try modifying the restriction as follows:
criteria.add(Restrictions.like("content","%"+contentStringToLook+"%"));
You can use this :
criteria.add(Restrictions.like("content",contentStringToLook, MatchMode.ANYWHERE))
There's also MatchMode.START, .END, and .EXACT.

Escape special characters in SPARQL queries

I'm trying to query dpbedia to get the categories of some wikipedia articles using Jena and ARQ
For example:
select ?category { dbpedia:ARTICLE_NAME dcterms:subject ?category }
Here is an example of a working query
SPARQL results
The problem is when there are special characters in ARTICLE_NAME for example "Parma_F.C.", where there is "."
select ?category { dbpedia:Parma_F.C. dcterms:subject ?category }
ERROR
So, I would like to ask you if someone had a solution for that.
Thanks in advance
The identifier dbpedia:Parma_F.C. is a so-called prefixed name, that is, an abbreviated form of a full URI. The full syntax rules for it are described in the SPARQL 1.1 Query Language specification.
The problem is specifically the full stop at the end of the prefixed name. According to the SPARQL grammar, a prefixed name cannot end on a full stop unless it's escaped. The fix is simply to use a backslash:
dbpedia:Parma_F.C\.
What you can also do as an alternative, is just write out the full URI. The dbpedia prefix maps to the http://dbpedia.org/resource/ namespace, so the full URI in SPARQL would become:
<http://dbpedia.org/resource/Parma_F.C.>
and the full query would become:
select ?category { <http://dbpedia.org/resource/Parma_F.C.> dcterms:subject ?category }

Executing a sql server query containing clause " where col like N'myStr%' " from java using spring

my query works using sql server management studio. However I can not get the query to work with named parameters and springsJDBCTemplate.
So actual sql required that works fine :
select colA from table1 where colb like N'lem%'
and a snippet of what I have tried :
String paramA = "N'lem%'";
select colA from table1 where colb like :paramA
I am using springs namedParamterJDBCTemplate. The N specifies nvarchar and unicode encoding as the actual parameters are encoded in foreign languages, eg cyrillic.
N' is useful when presenting some text to the textual SQL parser in SSMS, but when you are using parameterised queries through spring, DON'T! The string itself should be in Unicode, and the framework with handle it for you by declaring the parameter as NVarchar as well as marshalling the param properly.
String paramA = "lem%";
I don't believe you should even have the single quotes in the string, which I understand you're including only because of the N' notation.
You might want to try escape sequence for
String paramA = "N\'lem%'";
http://docs.oracle.com/javase/tutorial/java/data/characters.html

Categories