List of usage examples for org.apache.solr.client.solrj SolrQuery SolrQuery
public SolrQuery()
From source file:com.ssavr.solr.service.impl.IceCreamLocalServiceImpl.java
License:Open Source License
public List<IceCream> searchIceCreamsWithEDisMax(String name, String flavor, String text, String value, int start, int rows, String orderByCol, String orderByType) throws PortalException, SystemException { if (_log.isDebugEnabled()) { _log.debug("searchIceCreams()"); }//from w w w.j a va 2 s. co m List<IceCream> iceCreams = new ArrayList<IceCream>(); String activeServerURL = SolrUtil.getActiveSolrServer(); try { CommonsHttpSolrServer server = new CommonsHttpSolrServer(activeServerURL); SolrQuery query = new SolrQuery(); query.setQuery(getQuery(value)); query.set("qf", "iceCreamName iceCreamFlavor"); /* * List<String> filterQueries = getFilterQueries(name, flavor, * text); for (String fq : filterQueries) { * query.setFilterQueries(fq); } */ query.setStart(start); query.setRows(rows); boolean enableFacet = true; query.setFacet(enableFacet); query.addFacetField("iceCreamName", "iceCreamFlavor"); QueryResponse response = server.query(query); List<SolrResponse> searchResults = response.getBeans(SolrResponse.class); for (SolrResponse result : searchResults) { String iceCreamId = result.getEntryClassPK(); IceCream iceCream = iceCreamPersistence.findByPrimaryKey(Long.parseLong(iceCreamId)); iceCreams.add(iceCream); } } catch (MalformedURLException e) { if (_log.isDebugEnabled()) { _log.debug(e.getMessage()); } throw new SystemException(e); } catch (SolrServerException e) { if (_log.isDebugEnabled()) { _log.debug(e.getMessage()); } throw new SystemException(e); } return iceCreams; }
From source file:com.staticvillage.recommender.indexer.SolrIndexer.java
License:Apache License
@Override public List<Place> getBeans() throws IndexerException { SolrQuery query = new SolrQuery(); query.setQuery("*:*"); //query./*from w ww .j a v a 2 s.c o m*/ try { QueryResponse rsp = solr.query(query); return rsp.getBeans(Place.class); } catch (SolrServerException e) { throw new IndexerException(e.getMessage()); } }
From source file:com.streamsets.pipeline.solr.impl.Solr04TestUtil.java
License:Apache License
@SuppressWarnings("unchecked") public List<Map<String, Object>> query(Map<String, String> q) throws Exception { SolrQuery parameters = new SolrQuery(); for (Map.Entry<String, String> entry : q.entrySet()) { parameters.set(entry.getKey(), entry.getValue()); }//w w w . j ava 2s . com QueryResponse response = solrServer.query(parameters); List<SolrDocument> solrDocumentList = response.getResults(); List<Map<String, Object>> result = new ArrayList(); for (SolrDocument document : solrDocumentList) { result.add(document); } return result; }
From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java
License:Apache License
@Test public void testWriteRecords() throws Exception { Target target = createTarget(); TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target).build(); try {// www . j a v a 2 s .c o m SolrServer solrClient = getSolrServer(); solrClient.ping(); //delete all index solrClient.deleteByQuery("*:*"); runner.runInit(); List<Record> records = new ArrayList<>(); Record record1 = RecordCreator.create(); record1.set(Field.create( ImmutableMap.of("a", Field.create("Hello"), "b", Field.create("i"), "c", Field.create("t")))); Record record2 = RecordCreator.create(); record2.set(Field.create( ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i"), "c", Field.create("t")))); records.add(record1); records.add(record2); runner.runWrite(records); Assert.assertTrue(runner.getErrorRecords().isEmpty()); Assert.assertTrue(runner.getErrors().isEmpty()); SolrQuery parameters = new SolrQuery(); parameters.set("q", "name:Hello"); QueryResponse queryResponse = solrClient.query(parameters); SolrDocumentList solrDocuments = queryResponse.getResults(); Assert.assertEquals(1, solrDocuments.size()); SolrDocument solrDocument = solrDocuments.get(0); String fieldAVal = (String) solrDocument.get("name"); Assert.assertNotNull(fieldAVal); Assert.assertEquals("Hello", fieldAVal); String fieldBVal = (String) solrDocument.get("sku"); Assert.assertNotNull(fieldBVal); Assert.assertEquals("i", fieldBVal); String fieldCVal = (String) solrDocument.get("manu"); Assert.assertNotNull(fieldCVal); Assert.assertEquals("t", fieldCVal); } catch (Exception e) { e.printStackTrace(); throw e; } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java
License:Apache License
@Test public void testWriteRecordsOnErrorDiscard() throws Exception { Target target = createTarget(); TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target) .setOnRecordError(OnRecordError.DISCARD).build(); try {// w w w . ja va 2 s. c o m SolrServer solrClient = getSolrServer(); //delete all index solrClient.deleteByQuery("*:*"); runner.runInit(); List<Record> records = new ArrayList<>(); Record record1 = RecordCreator.create(); record1.set(Field.create(ImmutableMap.of("nota", Field.create("Hello"), "b", Field.create("i1"), "c", Field.create("t1")))); Record record2 = RecordCreator.create(); record2.set(Field.create( ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i2"), "c2", Field.create("t2")))); records.add(record1); records.add(record2); runner.runWrite(records); Assert.assertTrue(runner.getErrorRecords().isEmpty()); Assert.assertTrue(runner.getErrors().isEmpty()); SolrQuery parameters = new SolrQuery(); parameters.set("q", "sku:i1"); QueryResponse queryResponse = solrClient.query(parameters); SolrDocumentList solrDocuments = queryResponse.getResults(); Assert.assertEquals(0, solrDocuments.size()); } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java
License:Apache License
@Test public void testWriteRecordsOnErrorToError() throws Exception { Target target = createTarget(); TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target) .setOnRecordError(OnRecordError.TO_ERROR).build(); try {/*from w w w . j av a2 s .c o m*/ SolrServer solrClient = getSolrServer(); //delete all index solrClient.deleteByQuery("*:*"); runner.runInit(); List<Record> records = new ArrayList<>(); Record record1 = RecordCreator.create(); record1.set(Field.create(ImmutableMap.of("nota", Field.create("Hello"), "b", Field.create("i1"), "c", Field.create("t1")))); Record record2 = RecordCreator.create(); record2.set(Field.create( ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i2"), "c", Field.create("t2")))); records.add(record1); records.add(record2); runner.runWrite(records); Assert.assertEquals(1, runner.getErrorRecords().size()); Assert.assertEquals("Hello", runner.getErrorRecords().get(0).get("/nota").getValueAsString()); Assert.assertTrue(runner.getErrors().isEmpty()); SolrQuery parameters = new SolrQuery(); parameters.set("q", "sku:i1"); QueryResponse queryResponse = solrClient.query(parameters); SolrDocumentList solrDocuments = queryResponse.getResults(); Assert.assertEquals(0, solrDocuments.size()); } finally { runner.runDestroy(); } }
From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java
License:Apache License
@Test public void testWriteRecordsOnErrorToErrorDuringIndexing() throws Exception { //Create target without solr field id String solrURI = jetty.getBaseUrl().toString() + "/" + "collection1"; List<SolrFieldMappingConfig> fieldNamesMap = new ArrayList<>(); fieldNamesMap.add(new SolrFieldMappingConfig("/a", "name")); Target target = new SolrTarget(InstanceTypeOptions.SINGLE_NODE, solrURI, null, ProcessingMode.BATCH, fieldNamesMap);//from w w w. j av a 2 s . c o m TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target) .setOnRecordError(OnRecordError.TO_ERROR).build(); try { SolrServer solrClient = getSolrServer(); //delete all index solrClient.deleteByQuery("*:*"); runner.runInit(); List<Record> records = new ArrayList<>(); Record record1 = RecordCreator.create(); record1.set(Field.create(ImmutableMap.of("nota", Field.create("Hello"), "b", Field.create("i1"), "c", Field.create("t1")))); Record record2 = RecordCreator.create(); record2.set(Field.create( ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i2"), "c", Field.create("t2")))); records.add(record1); records.add(record2); runner.runWrite(records); Assert.assertEquals(2, runner.getErrorRecords().size()); Assert.assertEquals("Hello", runner.getErrorRecords().get(0).get("/nota").getValueAsString()); Assert.assertTrue(runner.getErrors().isEmpty()); SolrQuery parameters = new SolrQuery(); parameters.set("q", "sku:i1"); QueryResponse queryResponse = solrClient.query(parameters); SolrDocumentList solrDocuments = queryResponse.getResults(); Assert.assertEquals(0, solrDocuments.size()); } finally { runner.runDestroy(); } }
From source file:com.tamingtext.fuzzy.MovieMatcher.java
License:Apache License
public MovieMatcher() throws MalformedURLException { solr = new CommonsHttpSolrServer(new URL("http://localhost:8983/solr")); query = new SolrQuery(); query.setRows(10);//from w w w.j ava 2 s. c om }
From source file:com.tamingtext.fuzzy.SpellCorrector.java
License:Apache License
public SpellCorrector(StringDistance sd, float threshold) throws MalformedURLException { solr = new CommonsHttpSolrServer(new URL("http://localhost:8983/solr")); query = new SolrQuery(); query.setFields("word"); query.setRows(50); //<co id="co.dym.num"/> this.sd = sd; this.threshold = threshold; }
From source file:com.tamingtext.solr.SolrJTest.java
License:Apache License
public void doTest() throws IOException, SolrServerException { log.info("--------------------test()---------------------"); String description = "In a stunning defeat for rabbits " + "everywhere, Fred Q. Tortoise capitalized on John " + "Hare's brazen laziness to win the annual Tortoise-Hare 5K Road Race. " + "The lazy Mr. Hare, in hopes of saving face, immediately " + "acccused Mr. Tortoise of doping, in what has become an " + "all too familiar scene in sporting events today. " + "Fans everywhere were appalled by the revelations, with " + "allegiances falling roughly along species lines."; //<start id="solrj"/> SolrServer solr = new CommonsHttpSolrServer(new URL("http://localhost:" + port + "/solr"));//<co id="co.solrj.server"/> SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "http://tortoisehare5k.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain"); doc.addField("title", "Tortoise beats Hare! Hare wants rematch.", 5);//<co id="co.solrj.title"/> Date now = new Date(); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now));//<co id="co.solrj.date"/> doc.addField("description", description); doc.addField("categories_t", "Fairy Tale, Sports");//<co id="co.solrj.dynamic.field"/> solr.add(doc);//<co id="co.solrj.add"/> solr.commit();//<co id="co.solrj.commit"/> /*/*w w w. j a va2s. co m*/ <calloutlist> <callout arearefs="co.solrj.server"><para>Create a HTTP-based Solr Server connection.</para></callout> <callout arearefs="co.solrj.unique"><para>The schema used for this instance of Solr requires a unique field named "id"</para></callout> <callout arearefs="co.solrj.title"><para>Add a Title field to our document and boost it to be 5 times as important as the other fields</para></callout> <callout arearefs="co.solrj.date"><para>Dates must be formatted in a specific way for Solr.</para></callout> <callout arearefs="co.solrj.dynamic.field"><para>A dynamic field allows for the addition of unknown fields to Solr. The "_t" tells Solr this should be treated as a text field.</para></callout> <callout arearefs="co.solrj.add"><para>Send the newly created document to Solr. Solr takes care of creating a correctly formatted XML message and sending it to Solr using Apache Jakarta Commons HTTPClient.</para></callout> <callout arearefs="co.solrj.commit"><para>After you have added all your documents and wish to make them available for searching, send a commit message to Solr</para></callout> </calloutlist> */ //<end id="solrj"/> //Add some more docs doc = new SolrInputDocument();//<co id="co.solrj.doc"/> doc.addField("id", "http://redfox.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain");//<co id="co.solrj.mime"/> doc.addField("title", "Red Fox mocks Lazy Brown Dogs!", 5);//<co id="co.solrj.title"/> now = new Date(); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("description", "Once again, the Red Fox outshined the" + " lazy Brown Dogs to win the vaunted Tally Ho Cup, in what" + " has become an annual ritual. The lazy brown " + "dogs just don't seem to have the drive that Red Fox does." + " The lazy Brown Dogs vow to avenge their latest defeat, " + "but the group's supporters claim to have heard it all before."); doc.addField("categories_t", "Fairy Tale, Sports"); solr.add(doc);//<co id="co.solrj.add"/> doc = new SolrInputDocument();//<co id="co.solrj.doc"/> doc.addField("id", "http://maryLambs.tamingtext.com");//<co id="co.solrj.unique"/> doc.addField("mimeType", "text/plain");//<co id="co.solrj.mime"/> now = new Date(10000); doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("title", "Mary's Little Lamb Stolen!.", 5);//<co id="co.solrj.title"/> doc.addField("description", "In an affront to all that is good and" + " decent in this world, criminals made off with Mary's " + "little Lamb in a late night raid on Mary's farm." + " Early suspects include a Ms. Bo Peep who weeks earlier " + "reported losing several sheep. Police suspect Ms. Peep " + "was going to use the lamb to bring her flock's numbers back up." + " The spokesman for Ms. Peep declined comment at this " + "time. Mary, however, was outraged and promises revenge " + "on the insensitive clod who stole her precious animals."); doc.addField("categories_t", "Fairy Tale, crime, sheep"); solr.add(doc); //add in some random other docs Collection<SolrInputDocument> docs = new HashSet<SolrInputDocument>(); for (int i = 0; i < 100; i++) { doc = new SolrInputDocument(); doc.addField("id", "http://www.tamingtext.com/" + i + ".html"); doc.addField("mimeType", "text/html"); doc.addField("title", "This is title " + i); now = new Date(150 * i + i + 20);//create some dates doc.addField("date", DateUtil.getThreadLocalDateFormat().format(now)); doc.addField("description", "This is description number: " + i + " of something that is very important."); docs.add(doc); } solr.add(docs); solr.commit(); solr.optimize(); //<start id="solrj-search-1"/> SolrQuery queryParams = new SolrQuery();//<co id="solrj-search.co.query"/> queryParams.setFields("description", "title");//<co id="solrj-search.co.fields"/> queryParams.setQuery("description:win OR description:all");//<co id="solrj-search.co.terms"/> queryParams.setRows(10); QueryResponse response = solr.query(queryParams);//<co id="solrj-search.co.process"/> assertTrue("response is null and it shouldn't be", response != null); SolrDocumentList documentList = response.getResults(); System.out.println("Docs: " + documentList); assertTrue("response.getResults() Size: " + documentList.size() + " is not: " + 3, documentList.size() == 3); /* <calloutlist> <callout arearefs="solrj-search.co.query"><para>A <classname>SolrQuery</classname> is a easy-to-use container for creating the most common types of queries.</para></callout> <callout arearefs="solrj-search.co.fields"><para>Set the names of the Fields to be returned in the documents.</para></callout> <callout arearefs="solrj-search.co.terms"><para>Set the terms to search. The "OR" is a boolean operator which allows one or the other or both to be present in the query.</para></callout> <callout arearefs="solrj-search.co.process"><para>Submit the query to Solr and get back a <classname>QueryResponse</classname> which contains the results of the search.</para></callout> </calloutlist> */ //<end id="solrj-search-1"/> //<start id="solrj-search-dismax"/> queryParams.setQuery("lazy"); queryParams.setParam("defType", "dismax");//<co id="solrj-search.co.dismax"/> queryParams.set("qf", "title^3 description^10");//<co id="sorlj-search.co.qf"/> System.out.println("Query: " + queryParams); response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); documentList = response.getResults(); assertTrue("documentList Size: " + documentList.size() + " is not: " + 2, documentList.size() == 2); /* <calloutlist> <callout arearefs="solrj-search.co.dismax"><para>Tell Solr to use the <classname>DisMax</classname> Query Parser (named dismax in solrconfig.xml). </para></callout> <callout arearefs="sorlj-search.co.qf"><para>The DisMax parser will search across the fields given by the "qf" parameter and boosts the terms accordingly.</para></callout> </calloutlist> */ //<end id="solrj-search-dismax"/> //<start id="solrj-search-facets"/> queryParams = new SolrQuery(); queryParams.setQuery("description:number"); queryParams.setRows(10); queryParams.setFacet(true);//<co id="solrj-search-facets-facetOn"/> queryParams.set("facet.field", "date");//<co id="solrj-search-facets-date"/> response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); System.out.println("Query: " + queryParams); documentList = response.getResults(); assertTrue("documentList Size: " + documentList.size() + " is not: " + 10, documentList.size() == 10); System.out.println("Facet Response: " + response.getResponse());//<co id="solrj-search-facets-print"/> /* <calloutlist> <callout arearefs="solrj-search-facets-facetOn"><para>Turn on faceting for this query</para></callout> <callout arearefs="solrj-search-facets-date"><para>Specify the Field to facet on</para></callout> <callout arearefs="solrj-search-facets-print"><para>Print out the facet information</para></callout> </calloutlist> */ //<end id="solrj-search-facets"/> //<start id="solrj-search-more-like-this"/> queryParams = new SolrQuery(); queryParams.setQueryType("/mlt");//<co id="solrj-search.co.mlt"/> queryParams.setQuery("description:number"); queryParams.set("mlt.match.offset", "0");//<co id="solrj-search.co.mlt.off"/> queryParams.setRows(1); queryParams.set("mlt.fl", "description, title");//<co id="solrj-search.co.mlt.fl"/> response = solr.query(queryParams); assertTrue("response is null and it shouldn't be", response != null); SolrDocumentList results = (SolrDocumentList) response.getResponse().get("match"); assertTrue("results Size: " + results.size() + " is not: " + 1, results.size() == 1); /* <calloutlist> <callout arearefs="solrj-search.co.mlt"><para>Create a "MoreLikeThis" search to find similar documents to the specified document.</para></callout> <callout arearefs="solrj-search.co.mlt.fl"><para>Specify the field to use to generate the "MoreLikeThis" query.</para></callout> <callout arearefs="solrj-search.co.mlt.off"><para>Specify which document in the original results to use as the "similar" document. </para></callout> </calloutlist> */ //<end id="solrj-search-more-like-this"/> log.info("--------------------end test()---------------------"); }