elastic search query in java - java

I am having trouble making this URL query work in Java, it does not return any results. but from the browser it returns all the results, here is the URL that returns result:
_search?pretty&q=*357*+AND+account_id:574fe92c9179a809fd76f0b8+AND+invalid:false
And here is my code (does not return any results):
FilterBuilder[] filtersArray = new FilterBuilder[2];
filtersArray[0] = FilterBuilders.termFilter("account_id", "574fe92c9179a809fd76f0b8");
filtersArray[1] = FilterBuilders.termFilter("invalid", false);
QueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.simpleQueryStringQuery("*357*"), FilterBuilders.andFilter(filtersArray));
SearchResponse response = esClient.prepareSearch(SecurityManager.getNamespace())
.addSort("created_time", SortOrder.DESC)
.setTypes(dataType)
.setQuery(query)
.addFields("_id")
.setFrom(page * size)
.setSize(size)
.setExplain(false)
.execute()
.actionGet();
Can someone tell me what is the best way to translate the URL query into a java query?

First off, the URL query you should use is this one
?q=*357*+AND+account_id:574fe92c9179a809fd76f0b8+AND+invalid:false
otherwise you'll have no constraint on account_id and invalid
Then, the exact translation of this new URL query in Java is
QueryBuilder query = QueryBuilders.queryStringQuery("*357* AND account_id:574fe92c9179a809fd76f0b8 AND invalid:false");
SearchResponse response = esClient.prepareSearch(SecurityManager.getNamespace())
.addSort("created_time", SortOrder.DESC)
.setTypes(dataType)
.setQuery(query)
.addFields("_id")
.setFrom(page * size)
.setSize(size)
.setExplain(false)
.execute()
.actionGet();
Notes:
queryStringQuery and not simpleQueryStringQuery
no filters as they are all in the query string already

Related

Elasticsearch java api, hits score always be 1.0

I used ElasticSearch java client. I do search with query_string, and I get response, but score always be 1.0 .
code is:
String query = "{\"query\": {\"query_string\": {\"query\": \"weblog data4\"}}}";
SearchRequestBuilder builder = client
.prepareSearch("flume-2016-08-10")
.setQuery(query)
.addHighlightedField("*")
.setHighlighterRequireFieldMatch(false)
.setFrom(0).setSize(60).setExplain(true);
SearchResponse response = builder.execute().actionGet();
System.out.println(response.toString());
System.out.println(response.getHits().getAt(0).getSource());
System.out.println(response.getHits().getAt(0).getHighlightFields());
client.close();
result is:
However, I do search in elaseticsearch-head, I get response with correct score.
So, How do I get correct score with java?
This solved my problem:
change:
String query = "{\"query\": {\"query_string\": {\"query\": \"weblog data4\"}}}";
to:
String query = "{\"query_string\": {\"query\": \"weblog data4\"}}";
more details:
answer by jpountz

Correct use case of String parameter in SetQuery function of SolrQuery?

I have q
queryString = "select?wt=json&rows=0&indent=true&facet=true&q=*:*&facet=true&facet.field=outcome_type"
If queried like :
http://x.x.x.x:8983/solr/abc/queryString
it works. here abc is a core.
Now I would like to execute it programmatically, and using the following approach :
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
QueryResponse resp = server.query(query);
here queryString as defined above, but it return the following error :
Exception in thread "main"
org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException:
undefined field text
What I am missing here ? Or I need to build the query by set functions ?
I see few problems in your tentative.
You should not pass entire query string with the setQuery method. For almost each parameter available in query string there is a corresponding method in SolrQuery class.
SolrQuery does not support json format, SolrJ only supports the javabin and xml formats, I suggest to not specify any wt parameter.
So, you should use setQuery method only for q parameter:
query.setQuery("*:*");
For remaining parameters, the easiest way is use add method:
query.add("rows", "0"); // instead of setRows(0)
query.add("indent", "true");
query.add("facet", "true"); // ... setFacet(true)
query.add("facet.field", "outcome_type"); // ... addFacetField("outcome_type")
Hope this helps
I have used following approach to execute the query and it worked:
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
query.setFacet(true);
query.set("wt", "json");
query.set("indent",true);
query.setRows(0);
query.addFacetField("outcome_type");
QueryResponse resp = server.query(query);

ElasticSearch in java

I started to use ElasticSearch in my test project and cant figure out hot to create search for all fields. For instance we have some words as a search query and i want to find all indexed object in ElasticSearch, using Java API.
My obj have: id, name, adress, etc
I searched for this kind of info and wrote this:
Node node = nodeBuilder().node();
Client client = node.client();
RegexpFilterBuilder qFilter = FilterBuilders.regexpFilter("_all", (".*" + query + ".*").replace(" ", ".*"));
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setPostFilter(qFilter)
.setFrom(0).setSize(100).setExplain(true)
.execute()
.actionGet();
SearchHit[] results = response.getHits().getHits();
System.out.println("Current results: " + results.length);
I also tried to use one field:
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(termQuery(field, value))
.setFrom(0).setSize(100).setExplain(true)
.execute()
.actionGet();
I always get 0 result.
Can you show me, how to do this in right way in java?
Ok, i spent more time on documentation and i found a solution, hope it helps to someone else!
You just need to use QueryBuilders.multiMatchQuery, the value is our searching word and other strings are the columns where to search to.
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(QueryBuilders.multiMatchQuery(value,
"name", "address1", "city", "postalCode",
"countryCode", "airportCode", "locationDescription",
"shortDescription"
))
.setFrom(0).setSize(100).setExplain(true)
.execute()
.actionGet();

How to query using CloudSorlServer

I have a problem when I want to query from my java web service into solr server. My code looks like this:
CloudSolrServer solr = new CloudSolrServer("BigDataNew1:2181,BigDataNew2:2181,BigDataNew3:2181,BigDataNew4:2181,BigDataNew5:2181/solr");
Solr Queryquery = new SolrQuery();
ModifiableSolrParams param = new ModifiableSolrParams();
param.set("q",keyword).set("fl"," id, title, desc, pubDate, media, person, location").set("count","1").set("wt", "json").set("facet", true).set("start", "0").set("rows", "5");
QueryResponse response = solr.query(param);
SolrDocumentList list = response.getResults();
I got the following error:org.apache.solr.client.solrj.SolrServerException: No collection param specified on request and no default collection has been set.
Anybody know what the problem is?
Thanks
You'll need to tell CloudSolrServer which collection you want to query.
You can do this by setting it with setDefaultCollection:
solr.setDefaultCollection("foobar");

Hibernate search with Criteria restriction returning incorrect count

The result list is perfect but the getResultSize() is incorrect.
I've knocked up some code to illustrate.
Criteria criteria2 = this.getSession().createCriteria(Film.class);
Criterion genre = Restrictions.eq("genreAlias.genreName", details.getSearch().getGenreName());
criteria2.createAlias("genres", "genreAlias", CriteriaSpecification.INNER_JOIN);
criteria2.add(genre);
criteria2.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult());
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.createFullTextEntityManager(entityManager);
org.apache.lucene.queryParser.QueryParser parser2 = new QueryParser("title", new StopAnalyzer() );
org.apache.lucene.search.Query luceneQuery2 = parser2.parse( "title:"+details.getSearch()");
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery2, Film.class);
fullTextQuery.setCriteriaQuery(criteria2);
fullTextQuery.getResultList()); // Returns the correctly filtered list
fullTextQuery.getResultSize()); // Returns the retsult size without the genre resrtiction
From http://docs.jboss.org/hibernate/search/3.3/api/org/hibernate/search/jpa/FullTextQuery.html
int getResultSize()
Returns the number of hits for this search Caution: The number of results might be slightly different from getResultList().size() because getResultList() may be not in sync with the database at the time of query.
You should try to use some of the more specialized queries like this one:
Query query = new FuzzyQuery(new Term("title", q));
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Film.class);
int filmCount = fullTextQuery.getResultSize();
and this is how you do pagination requests (I'm guessing you have improperly implemented your paggination):
FullTextQuery hits = Search.getFullTextSession(getSession()).createFullTextQuery(query, Film.class)
.setFirstResult((pageNumber - 1) * perPageItems).setMaxResults(perPageItems);
The above works for me every time. You should keep in mind that the result of getResultSize() more of estimate. I use pagination a lot and I have experienced the number changing between pages. So you should say "about xxxx" results.

Categories

Resources