r13736 - in search_server/trunk/index/src: main/java/org/musicbrainz/search/index test/java/org/musicbrainz/search/index

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

r13736 - in search_server/trunk/index/src: main/java/org/musicbrainz/search/index test/java/org/musicbrainz/search/index

root-9
Author: ijabz
Date: 2014-04-25 11:06:42 +0000 (Fri, 25 Apr 2014)
New Revision: 13736

Modified:
   search_server/trunk/index/src/main/java/org/musicbrainz/search/index/AreaIndex.java
   search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AbstractIndexTest.java
   search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AreaIndexTest.java
Log:
SEARCH-338: Return containing areas in area search

Modified: search_server/trunk/index/src/main/java/org/musicbrainz/search/index/AreaIndex.java
===================================================================
--- search_server/trunk/index/src/main/java/org/musicbrainz/search/index/AreaIndex.java 2014-04-25 10:32:18 UTC (rev 13735)
+++ search_server/trunk/index/src/main/java/org/musicbrainz/search/index/AreaIndex.java 2014-04-25 11:06:42 UTC (rev 13736)
@@ -35,6 +35,7 @@
 import org.musicbrainz.mmd2.*;
 import org.musicbrainz.search.MbDocument;
 
+import java.awt.geom.Area;
 import java.io.IOException;
 import java.sql.*;
 import java.util.HashMap;
@@ -47,6 +48,8 @@
 
     public static final String INDEX_NAME = "area";
 
+    private static final String AREA_RELATION_TYPE = "area";
+
     public AreaIndex(Connection dbConnection) {
         super(dbConnection);
     }
@@ -93,6 +96,19 @@
                         " WHERE a.id BETWEEN ? AND ? " +
                         " ORDER BY a.id");
 
+        addPreparedStatement("AREA_PARENT",
+                "SELECT a2.id as areaid, a.id, a.gid, a.name, a.comment as comment, at.name as type, " +
+                        "   a.begin_date_year, a.begin_date_month, a.begin_date_day, " +
+                        "  a.end_date_year, a.end_date_month, a.end_date_day, a.ended, lt.name as link, lt.gid as linkid" +
+                        " FROM area a" +
+                        " INNER JOIN l_area_area laa on a.id=laa.entity0" +
+                        " INNER JOIN area a2         on a2.id=laa.entity1" +
+                        " INNER JOIN link l          ON laa.link   = l.id " +
+                        " INNER JOIN link_type lt    ON l.link_type=lt.id" +
+                        " LEFT JOIN area_type at ON a.type = at.id " +
+                        " WHERE a2.id BETWEEN ? AND ? " +
+                        " ORDER BY a2.id");
+
         addPreparedStatement("ALIASES",
                 "SELECT a.area as area, a.name as alias, a.sort_name as alias_sortname, a.primary_for_locale, a.locale, att.name as type," +
                         "a.begin_date_year, a.begin_date_month, a.begin_date_day, a.end_date_year, a.end_date_month, a.end_date_day" +
@@ -217,18 +233,75 @@
             iso3List.getIso31663Code().add(rs.getString("code"));
         }
 
+        //Linked area id to a relation containing the parent (if has one)
+        Map<Integer,RelationList> areaParent = new HashMap<Integer, RelationList>();
+        st = getPreparedStatement("AREA_PARENT");
+        st.setInt(1, min);
+        st.setInt(2, max);
+        rs = st.executeQuery();
+        while (rs.next()) {
+            int areaId = rs.getInt("areaid");
+
+            DefAreaElementInner area = of.createDefAreaElementInner();
+            String guid = rs.getString("gid");
+            area.setId(guid);
+
+            String name = rs.getString("name");
+            area.setName(name);
+
+            area.setSortName(name);
+
+            String comment = rs.getString("comment");
+            if (!Strings.isNullOrEmpty(comment)) {
+                area.setDisambiguation(comment);
+            }
+
+            String type = rs.getString("type");
+            if (!Strings.isNullOrEmpty(type)) {
+                area.setType(type);
+            }
+
+            boolean ended = rs.getBoolean("ended");
+
+            String begin = Utils.formatDate(rs.getInt("begin_date_year"), rs.getInt("begin_date_month"), rs.getInt("begin_date_day"));
+            String end = Utils.formatDate(rs.getInt("end_date_year"), rs.getInt("end_date_month"), rs.getInt("end_date_day"));
+            LifeSpan lifespan = of.createLifeSpan();
+            area.setLifeSpan(lifespan);
+            if(!Strings.isNullOrEmpty(begin)) {
+                lifespan.setBegin(begin);
+            }
+            if(!Strings.isNullOrEmpty(end)) {
+                lifespan.setEnd(end);
+            }
+            lifespan.setEnded(Boolean.toString(ended));
+
+            RelationList rl   = of.createRelationList();
+            rl.setTargetType(AREA_RELATION_TYPE);
+            Relation relation = of.createRelation();
+            relation.setArea(area);
+            Target target = of.createTarget();
+            target.setValue(area.getId());
+            relation.setTarget(target);
+            relation.setType(rs.getString("link"));
+            relation.setTypeId(rs.getString("linkid"));
+            relation.setDirection(DefDirection.BACKWARD);
+            rl.getRelation().add(relation);
+            areaParent.put(areaId, rl);
+        }
         st = getPreparedStatement("AREA");
         st.setInt(1, min);
         st.setInt(2, max);
         rs = st.executeQuery();
         while (rs.next()) {
-            indexWriter.addDocument(documentFromResultSet(rs, aliases, iso1, iso2, iso3));
+            indexWriter.addDocument(documentFromResultSet(rs, areaParent, aliases, iso1, iso2, iso3));
         }
         rs.close();
 
     }
 
+
     public Document documentFromResultSet(ResultSet rs,
+                                          Map<Integer,RelationList> areaParents,
                                           Map<Integer, Set<Alias>> aliases,
                                           Map<Integer, Iso31661CodeList> iso1,
                                           Map<Integer, Iso31662CodeList> iso2,
@@ -286,6 +359,10 @@
         }
         lifespan.setEnded(Boolean.toString(ended));
 
+        if(areaParents.containsKey(areaId)) {
+            RelationList rl = areaParents.get(areaId);
+            area.getRelationList().add(rl);
+        }
 
         if (aliases.containsKey(areaId)) {
             AliasList aliasList = of.createAliasList();

Modified: search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AbstractIndexTest.java
===================================================================
--- search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AbstractIndexTest.java 2014-04-25 10:32:18 UTC (rev 13735)
+++ search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AbstractIndexTest.java 2014-04-25 11:06:42 UTC (rev 13736)
@@ -122,6 +122,7 @@
                 stmt.addBatch("DROP TABLE area_alias_type");
                 stmt.addBatch("DROP TABLE area_alias");
                 stmt.addBatch("DROP TABLE tag");
+                stmt.addBatch("DROP TABLE l_area_area");
 
                 stmt.addBatch("DROP TABLE artist");
                 stmt.addBatch("DROP TABLE artist_alias");
@@ -322,6 +323,16 @@
                 "                         primary_for_locale  BOOLEAN NOT NULL DEFAULT false"+
                 ");");
 
+        stmt.addBatch("CREATE TABLE l_area_area" +
+                "(" +
+                "    id                  SERIAL," +
+                "    link                INTEGER NOT NULL," +
+                "    entity0             INTEGER NOT NULL," +
+                "    entity1             INTEGER NOT NULL," +
+                "    edits_pending       INTEGER NOT NULL DEFAULT 0," +
+                "    last_updated        TIMESTAMP" +
+                ")");
+
         stmt.addBatch("CREATE TABLE tag (" +
                 "  id serial NOT NULL," +
                 "  name character varying(255) NOT NULL," +

Modified: search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AreaIndexTest.java
===================================================================
--- search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AreaIndexTest.java 2014-04-25 10:32:18 UTC (rev 13735)
+++ search_server/trunk/index/src/test/java/org/musicbrainz/search/index/AreaIndexTest.java 2014-04-25 11:06:42 UTC (rev 13736)
@@ -6,10 +6,7 @@
 import org.apache.lucene.index.IndexWriter;
 import org.apache.lucene.store.RAMDirectory;
 import org.junit.Test;
-import org.musicbrainz.mmd2.Alias;
-import org.musicbrainz.mmd2.AreaList;
-import org.musicbrainz.mmd2.DefAreaElementInner;
-import org.musicbrainz.mmd2.LifeSpan;
+import org.musicbrainz.mmd2.*;
 
 import java.sql.Statement;
 
@@ -39,6 +36,7 @@
         Statement stmt = conn.createStatement();
         stmt.addBatch("INSERT INTO area (id, gid,name, comment, type, begin_date_year, end_date_year) VALUES (1, 'aa95182f-df0a-3ad6-8bfb-4b63482cd276', 'Afghanistan','A Country in Asia',1,1830,2020)");
         stmt.addBatch("INSERT INTO area_type(id, name) VALUES (1, 'Country')");
+        stmt.addBatch("INSERT INTO area_type(id, name) VALUES (2, 'Subdivision')");
         stmt.addBatch("INSERT INTO area_alias (id, area, sort_name, name, primary_for_locale, locale, type ) VALUES (3, 1, 'Afghan', 'Afghany', true, 'en',1)");
         stmt.addBatch("INSERT INTO iso_3166_1(area, code) VALUES (1,'AF')");
         stmt.addBatch("INSERT INTO iso_3166_2(area, code) VALUES (1,'North')");
@@ -48,6 +46,27 @@
         stmt.close();
     }
 
+    private void addAreaTwo() throws Exception {
+
+        Statement stmt = conn.createStatement();
+        stmt.addBatch("INSERT INTO area_type(id, name) VALUES (1, 'Country')");
+        stmt.addBatch("INSERT INTO area_type(id, name) VALUES (2, 'Subdivision')");
+        stmt.addBatch("INSERT INTO link (id, link_type) values (1,10)");
+        stmt.addBatch("INSERT INTO link_type (id, name, gid) values (10,'part of','bb95182f-df0a-3ad6-8bfb-4b63482cd276')");
+
+
+        stmt.addBatch("INSERT INTO area (id, gid,name, comment, type, begin_date_year, end_date_year) VALUES (1, 'aa95182f-df0a-3ad6-8bfb-4b63482cd276', 'San Francisco','San Francisco, Argentina',2,1830,2020)");
+        stmt.addBatch("INSERT INTO area_alias (id, area, sort_name, name, primary_for_locale, locale, type ) VALUES (3, 1, 'San Francisco', 'San Francisco', true, 'en',1)");
+        stmt.addBatch("INSERT INTO iso_3166_1(area, code) VALUES (1,'AR')");
+
+        stmt.addBatch("INSERT INTO area (id, gid,name, comment, type, begin_date_year, end_date_year) VALUES (2, 'aa95182f-df0a-3ad6-8bfb-4b63482cd400', 'Argentina','Argentina',1,1330,2020)");
+
+        stmt.addBatch("INSERT INTO l_area_area (id, link, entity0, entity1) values (1,1,2,1)");
+
+        stmt.executeBatch();
+        stmt.close();
+    }
+
     @Test
     public void testIndexAreaName() throws Exception {
 
@@ -275,5 +294,85 @@
         ir.close();
     }
 
+    @Test
+    public void testIndexAreaParent() throws Exception {
 
+        addAreaTwo();
+        RAMDirectory ramDir = new RAMDirectory();
+        createIndex(ramDir);
+
+        IndexReader ir = DirectoryReader.open(ramDir);
+        assertEquals(3, ir.numDocs());
+        {
+            checkTerm(ir, AreaIndexField.AREA, "argentina");
+
+        }
+        ir.close();
+    }
+
+    @Test
+    public void testStoredIndexAreaWithParent() throws Exception {
+
+        addAreaTwo();
+        RAMDirectory ramDir = new RAMDirectory();
+        createIndex(ramDir);
+
+        IndexReader ir = DirectoryReader.open(ramDir);
+        assertEquals(3, ir.numDocs());
+        {
+
+            Document doc = ir.document(1);
+            AreaList arealist = (AreaList) MMDSerializer.unserialize(doc.get(AreaIndexField.AREA_STORE.getName()), AreaList.class);
+            org.musicbrainz.mmd2.DefAreaElementInner area = arealist.getArea().get(0);
+            assertEquals("aa95182f-df0a-3ad6-8bfb-4b63482cd276", area.getId());
+            assertEquals("San Francisco", area.getName());
+            assertEquals("San Francisco", area.getSortName());
+            assertEquals("San Francisco, Argentina", area.getDisambiguation());
+            assertEquals("Subdivision", area.getType());
+            assertNotNull(area.getAliasList());
+            assertEquals(1,area.getAliasList().getAlias().size());
+
+            Alias alias =  area.getAliasList().getAlias().get(0);
+            assertEquals("San Francisco",alias.getContent());
+            assertEquals("San Francisco", alias.getSortName());
+            assertEquals("en",alias.getLocale());
+            assertEquals("AliasType",alias.getType());
+
+            assertNotNull(area.getLifeSpan());
+            LifeSpan lifeSpan = area.getLifeSpan();
+            assertEquals("1830",lifeSpan.getBegin());
+            assertEquals("2020",lifeSpan.getEnd());
+            assertEquals("false",lifeSpan.getEnded());
+
+            assertNotNull(area.getIso31661CodeList());
+            assertEquals("AR", area.getIso31661CodeList().getIso31661Code().get(0));
+
+            assertNotNull(area.getRelationList());
+            assertNotNull(area.getRelationList().get(0));
+            assertNotNull(area.getRelationList().get(0).getRelation());
+            assertNotNull(area.getRelationList().get(0).getRelation().get(0));
+            assertEquals("area",area.getRelationList().get(0).getTargetType());
+            Relation relation = area.getRelationList().get(0).getRelation().get(0);
+            assertEquals("bb95182f-df0a-3ad6-8bfb-4b63482cd276",relation.getTypeId());
+            assertEquals("part of",relation.getType());
+            assertEquals("aa95182f-df0a-3ad6-8bfb-4b63482cd400",relation.getTarget().getValue());
+            assertEquals("backward",relation.getDirection().value());
+            assertEquals("aa95182f-df0a-3ad6-8bfb-4b63482cd400", relation.getArea().getId());
+            assertEquals("Argentina", relation.getArea().getName());
+            assertEquals("Argentina", relation.getArea().getSortName());
+
+            //The parent in its own right
+            doc = ir.document(2);
+            arealist = (AreaList) MMDSerializer.unserialize(doc.get(AreaIndexField.AREA_STORE.getName()), AreaList.class);
+            area = arealist.getArea().get(0);
+            assertEquals("aa95182f-df0a-3ad6-8bfb-4b63482cd400", area.getId());
+            assertEquals("Argentina", area.getName());
+            assertEquals("Argentina", area.getSortName());
+            assertEquals("Argentina", area.getDisambiguation());
+            assertEquals("Country", area.getType());
+
+        }
+        ir.close();
+    }
+
 }
\ No newline at end of file


_______________________________________________
MusicBrainz-commits mailing list
[hidden email]
http://lists.musicbrainz.org/mailman/listinfo/musicbrainz-commits