List of usage examples for org.apache.solr.client.solrj SolrQuery SolrQuery
public SolrQuery(String q)
From source file:com.lyncode.oai.proxy.data.ProxyItemRepository.java
License:Apache License
@Override public ListItemIdentifiersResult getItemIdentifiers(List<Filter> filters, int offset, int length) { try {//from w w w . j a va2s . c o m List<String> whereCond = new ArrayList<String>(); for (Filter filter : filters) { if (filter.getFilter() instanceof ProxyFilter) { ProxyFilter proxyFilter = (ProxyFilter) filter.getFilter(); whereCond.add("(" + proxyFilter.query() + ")"); } } if (whereCond.isEmpty()) whereCond.add("*:*"); SolrQuery query = new SolrQuery("(" + StringUtils.join(whereCond.iterator(), ") AND (") + ")"); query.setStart(offset); query.setRows(length); SolrDocumentList list = SolrServerManager.getServer().query(query).getResults(); List<AbstractItemIdentifier> results = new ArrayList<AbstractItemIdentifier>(); for (SolrDocument doc : list) results.add(new ProxyItem(doc)); return new ListItemIdentifiersResult(list.getNumFound() > offset + length, results, (int) list.getNumFound()); } catch (SolrServerException e) { return new ListItemIdentifiersResult(false, new ArrayList<AbstractItemIdentifier>()); } }
From source file:com.lyncode.oai.proxy.data.ProxyItemRepository.java
License:Apache License
@Override public ListItemsResults getItems(List<Filter> filters, int offset, int length) { try {//w w w .j a v a 2 s . co m List<String> whereCond = new ArrayList<String>(); for (Filter filter : filters) { if (filter.getFilter() instanceof ProxyFilter) { ProxyFilter proxyFilter = (ProxyFilter) filter.getFilter(); whereCond.add("(" + proxyFilter.query() + ")"); } } if (whereCond.isEmpty()) whereCond.add("*:*"); SolrQuery query = new SolrQuery("(" + StringUtils.join(whereCond.iterator(), ") AND (") + ")"); query.setStart(offset); query.setRows(length); SolrDocumentList list = SolrServerManager.getServer().query(query).getResults(); List<AbstractItem> results = new ArrayList<AbstractItem>(); for (SolrDocument doc : list) results.add(new ProxyItem(doc)); return new ListItemsResults(list.getNumFound() > offset + length, results, (int) list.getNumFound()); } catch (SolrServerException e) { return new ListItemsResults(false, new ArrayList<AbstractItem>()); } }
From source file:com.mycompany.sparkrentals.client.RentalSolrClient.java
/** * search results based on field values in cleanedData * * @param cleanedData/* w w w .j a v a 2 s . co m*/ * @param perPage * @return * @throws SolrServerException * @throws IOException */ public QueryResponse searchRentals(Map<String, Object> cleanedData, int perPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery("*"); //start adding filtering for (String field : Arrays.asList("city", "province", "country", "type", "zipCode")) { if (cleanedData.get(field) != null) { query.addFilterQuery(field + ":\"" + cleanedData.get(field) + "\""); } } for (String field : Arrays.asList("hasAirCondition", "hasGarden", "hasPool", "isCloseToBeach")) { if (cleanedData.get(field) != null) { boolean isYes = cleanedData.get(field).equals("Yes"); query.addFilterQuery(field + ":\"" + isYes + "\""); } } if (cleanedData.get("roomsNumberFrom") != null || cleanedData.get("roomsNumberTo") != null) { String roomsNumberFrom = "*"; String roomsNumberTo = "*"; if (cleanedData.get("roomsNumberFrom") != null) { roomsNumberFrom = String.valueOf((int) cleanedData.get("roomsNumberFrom")); } if (cleanedData.get("roomsNumberTo") != null) { roomsNumberTo = String.valueOf((int) cleanedData.get("roomsNumberTo")); } String filterString = "roomsNumber:[" + roomsNumberFrom + " TO " + roomsNumberTo + "]"; query.addFilterQuery(filterString); } if (cleanedData.get("dailyPriceFrom") != null || cleanedData.get("dailyPriceTo") != null) { String dailyPriceFrom = "*"; String dailyPriceTo = "*"; if (cleanedData.get("dailyPriceFrom") != null) { dailyPriceFrom = String.format("%.2f", (float) cleanedData.get("dailyPriceFrom")); } if (cleanedData.get("dailyPriceTo") != null) { dailyPriceTo = String.format("%.2f", (float) cleanedData.get("dailyPriceTo")); } String filterString = "dailyPrice:[" + dailyPriceFrom + " TO " + dailyPriceTo + "]"; query.addFilterQuery(filterString); } if (cleanedData.get("timePeriod") != null) { int timePeriod = (int) cleanedData.get("timePeriod"); query.addFilterQuery("updated:[NOW-" + timePeriod + "DAY TO * ]"); } //pagination int currentPage = (int) cleanedData.getOrDefault("page", 1); query.setStart((currentPage - 1) * perPage); query.setRows(perPage); return solrClient.query(query); }
From source file:com.mycompany.sparkrentals.client.RentalSolrClientTest.java
/** * Test of addRental method, of class RentalSolrClient. *///from w w w . j a v a2s . co m @Test public void testAddRental() throws IOException, SolrServerException { Rental rental = new Rental("A9999", "city90", "province90", "country90", "9d8Ui", "Villa", true, true, true, true, 120.7f, "$", 3, new Date()); client.addRental(rental); SolrQuery query = new SolrQuery("id:A9999"); QueryResponse response = server.query(query); List<Rental> solrResults = response.getBeans(Rental.class); assertEquals(solrResults.size(), 1); Rental solrRental = solrResults.get(0); assertEquals(rental.getId(), solrRental.getId()); assertEquals(rental.getCity(), rental.getCity()); assertEquals(rental.getProvince(), rental.getProvince()); assertEquals(rental.getCountry(), rental.getCountry()); assertEquals(rental.getZipCode(), rental.getZipCode()); assertEquals(rental.getType(), rental.getType()); assertEquals(rental.isHasAirCondition(), rental.isHasAirCondition()); assertEquals(rental.isHasPool(), rental.isHasPool()); assertEquals(rental.isHasGarden(), rental.isHasGarden()); assertEquals(rental.isIsCloseToBeach(), rental.isIsCloseToBeach()); assertEquals(rental.getDailyPrice(), rental.getDailyPrice(), 0.1f); assertEquals(rental.getCurrency(), rental.getCurrency()); assertEquals(rental.getRoomsNumber(), rental.getRoomsNumber()); assertEquals(rental.getUpdated(), rental.getUpdated()); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
@Test public void testIndexerDefinitionChangesPickedUp() throws Exception { createTable("table1", "family1"); WriteableIndexerModel indexerModel = main.getIndexerModel(); IndexerDefinition indexerDef = new IndexerDefinitionBuilder().name("indexer1") .configuration(Bytes.toBytes(("<indexer table='table1'><field name='field1_s' " + "value='family1:qualifier1'/></indexer>"))) .connectionType("solr").connectionParams(ImmutableMap.of("solr.zk", solrTestingUtility.getZkConnectString(), "solr.collection", "collection1")) .build();//from w ww. j a va2 s . c o m indexerModel.addIndexer(indexerDef); HTable table = new HTable(conf, "table1"); Put put = new Put(b("row1")); put.add(b("family1"), b("qualifier1"), b("value1")); table.put(put); // Also put a value which should not be processed by the current config put = new Put(b("row2")); put.add(b("family1"), b("qualifier2"), b("value1")); table.put(put); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); QueryResponse response = collection1.query(new SolrQuery("*:*")); assertEquals(1, response.getResults().size()); // update indexer model markEventCounts(); String lock = indexerModel.lockIndexer("indexer1"); indexerDef = new IndexerDefinitionBuilder().startFrom(indexerModel.getFreshIndexer("indexer1")) .configuration(Bytes .toBytes("<indexer table='table1'>" + "<field name='field1_s' value='family1:qualifier1'/>" + "<field name='field2_s' value='family1:qualifier2'/>" + "</indexer>")) .build(); indexerModel.updateIndexer(indexerDef, lock); indexerModel.unlockIndexer(lock); waitOnEventsProcessed(1); put = new Put(b("row3")); put.add(b("family1"), b("qualifier2"), b("value1")); table.put(put); SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); response = collection1.query(new SolrQuery("*:*")); assertEquals(2, response.getResults().size()); table.close(); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
@Test public void testTwoTablesTwoIndexers() throws Exception { createTable("table1", "family1"); createTable("table2", "family1"); assertEquals(0, collection1.query(new SolrQuery("*:*")).getResults().size()); assertEquals(0, collection2.query(new SolrQuery("*:*")).getResults().size()); WriteableIndexerModel indexerModel = main.getIndexerModel(); IndexerDefinition indexerDef = new IndexerDefinitionBuilder().name("indexer1") .configuration(Bytes.toBytes("<indexer table='table1'>" + "<field name='field1_s' value='family1:qualifier1'/>" + "</indexer>")) .connectionType("solr").connectionParams(ImmutableMap.of("solr.zk", solrTestingUtility.getZkConnectString(), "solr.collection", "collection1")) .build();//from w w w. j a v a 2 s.c om indexerModel.addIndexer(indexerDef); indexerDef = new IndexerDefinitionBuilder().name("indexer2") .configuration(Bytes.toBytes("<indexer table='table2'>" + "<field name='field1_s' value='family1:qualifier1'/>" + "</indexer>")) .connectionType("solr").connectionParams(ImmutableMap.of("solr.zk", solrTestingUtility.getZkConnectString(), "solr.collection", "collection2")) .build(); indexerModel.addIndexer(indexerDef); HTable table1 = new HTable(conf, "table1"); HTable table2 = new HTable(conf, "table2"); Put put = new Put(b("row1")); put.add(b("family1"), b("qualifier1"), b("value1")); table1.put(put); put = new Put(b("row2")); put.add(b("family1"), b("qualifier1"), b("value1")); table2.put(put); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer2")); SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); collection2.commit(); assertEquals(1, collection1.query(new SolrQuery("*:*")).getResults().size()); assertEquals(1, collection2.query(new SolrQuery("*:*")).getResults().size()); table1.close(); table2.close(); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
/** * When adding a new replication peer (= SEP consumer), HBase replication will deliver events starting * from the beginning of the current hlog file. These events might already be quite a while in there (if * there hasn't been much activity), which would lead to the surprising effect of these being indexed. * Therefore, the SEP has the concept of a subscriptionTimestamp, and events older than that ts are * ignored.//from w ww . ja v a2s. c om */ @Test public void testSubscriptionTimestamp() throws Exception { createTable("table1", "family1"); HTable table = new HTable(conf, "table1"); for (int i = 0; i < 10; i++) { Put put = new Put(b("row" + i)); put.add(b("family1"), b("qualifier1"), b("value1")); table.put(put); } // Ensure that the index is added on a different timestamp than the last put Thread.sleep(5); WriteableIndexerModel indexerModel = main.getIndexerModel(); IndexerDefinition indexerDef = new IndexerDefinitionBuilder().name("indexer1") .configuration(Bytes.toBytes("<indexer table='table1'>" + "<field name='field1_s' value='family1:qualifier1'/>" + "</indexer>")) .connectionType("solr").connectionParams(ImmutableMap.of("solr.zk", solrTestingUtility.getZkConnectString(), "solr.collection", "collection1")) .build(); indexerModel.addIndexer(indexerDef); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); SepTestUtil.waitOnReplication(conf, 60000L); // Check that records created before the indexer was added are not indexed collection1.commit(); SolrDocumentList results = collection1.query(new SolrQuery("*:*")).getResults(); assertEquals("Got results " + results + " while expecting none", 0, results.size()); // But newly added rows should be indexed for (int i = 0; i < 10; i++) { Put put = new Put(b("row_b_" + i)); put.add(b("family1"), b("qualifier1"), b("value1")); table.put(put); } SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); assertEquals(10, collection1.query(new SolrQuery("*:*")).getResults().size()); table.close(); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
@Test public void testCustomKeyFormatter() throws Exception { createTable("table1", "family1"); HTable table = new HTable(conf, "table1"); StringBuilder indexerConf = new StringBuilder(); indexerConf.append("<indexer table='table1'"); indexerConf.append(// ww w . j a v a 2 s . c o m " unique-key-formatter='com.ngdata.hbaseindexer.uniquekey.HexUniqueKeyFormatter'>"); indexerConf.append(" <field name='field1_s' value='family1:field1' type='string'/>"); indexerConf.append("</indexer>"); createIndexer1(indexerConf.toString()); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); Put put = new Put(new byte[] { 0, 0, 0, 0 }); put.add(b("family1"), b("field1"), b("value1")); table.put(put); SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); QueryResponse response = collection1.query(new SolrQuery("*:*")); assertEquals(1, response.getResults().size()); SolrDocument doc = response.getResults().get(0); assertEquals("00000000", doc.getFirstValue("id").toString()); table.close(); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
@Test public void testDefaultKeyFormatter() throws Exception { createTable("table1", "family1"); HTable table = new HTable(conf, "table1"); StringBuilder indexerConf = new StringBuilder(); indexerConf.append("<indexer table='table1'>"); indexerConf.append(" <field name='field1_s' value='family1:field1' type='string'/>"); indexerConf.append("</indexer>"); createIndexer1(indexerConf.toString()); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); Put put = new Put(new byte[] { 0, 0, 0, 0 }); put.add(b("family1"), b("field1"), b("value1")); table.put(put);/* www .j a va2 s .c om*/ SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); QueryResponse response = collection1.query(new SolrQuery("*:*")); assertEquals(1, response.getResults().size()); SolrDocument doc = response.getResults().get(0); assertEquals("#0;#0;#0;#0;", doc.getFirstValue("id").toString()); table.close(); }
From source file:com.ngdata.hbaseindexer.indexer.IndexerIT.java
License:Apache License
@Test public void testSomeDataTypesIncludingCustomValueMapper() throws Exception { createTable("table1", "family1"); HTable table = new HTable(conf, "table1"); StringBuilder indexerConf = new StringBuilder(); indexerConf.append("<indexer table='table1'>"); indexerConf.append(" <field name='field1_s' value='family1:field1' type='string'/>"); indexerConf.append(" <field name='field2_s' value='family1:field2' type='int'/>"); indexerConf.append(/*from w ww . j a v a 2s. c om*/ " <field name='field3_ss' value='family1:field3' type='com.ngdata.hbaseindexer.indexer.CsvByteArrayValueMapper'>"); indexerConf.append(" <param name='defaults' value='default1,default2'/>"); indexerConf.append(" </field>"); indexerConf.append("</indexer>"); createIndexer1(indexerConf.toString()); SepTestUtil.waitOnReplicationPeerReady(peerId("indexer1")); Put put = new Put(Bytes.toBytes("cry baby")); put.add(b("family1"), b("field1"), b("value1")); put.add(b("family1"), b("field2"), Bytes.toBytes(836)); put.add(b("family1"), b("field3"), b("blue,green,black,orange")); table.put(put); SepTestUtil.waitOnReplication(conf, 60000L); collection1.commit(); QueryResponse response = collection1.query(new SolrQuery("*:*")); assertEquals(1, response.getResults().size()); SolrDocument doc = response.getResults().get(0); assertEquals("value1", doc.getFirstValue("field1_s")); assertEquals(836, Integer.parseInt(doc.getFirstValue("field2_s").toString())); assertEquals(6, doc.getFieldValues("field3_ss").size()); assertTrue(doc.getFieldValues("field3_ss").contains("orange")); assertTrue(doc.getFieldValues("field3_ss").contains("default1")); table.close(); }