[#4321] Change solr type of 'labels' field

Change to white-space-tokenized string field instead of text
field to get exact whole-word matching without stemming.

Copyfield added to solr schema to create new _ws on the fly
during reindexing, while still allowing the existing code
to work with the existing _t field.

After reindexing is complete, do a global
s/labels_t/labels_ws/ on the project (already tested).

Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>
diff --git a/Allura/allura/model/artifact.py b/Allura/allura/model/artifact.py
index 681ac26..2acd9d8 100644
--- a/Allura/allura/model/artifact.py
+++ b/Allura/allura/model/artifact.py
@@ -111,10 +111,9 @@
     @classmethod
     def translate_query(cls, q, fields):
         for f in fields:
-            if f[-2] == '_':
-                base = f[:-2]
-                actual = f
-                q = q.replace(base+':', actual+':')
+            if '_' in f:
+                base, typ = f.rsplit('_', 1)
+                q = q.replace(base + ':', f + ':')
         return q
 
     @LazyProperty
diff --git a/Allura/allura/tests/unit/test_artifact.py b/Allura/allura/tests/unit/test_artifact.py
new file mode 100644
index 0000000..6e470c0
--- /dev/null
+++ b/Allura/allura/tests/unit/test_artifact.py
@@ -0,0 +1,29 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+
+import unittest
+
+from allura import model as M
+
+
+class TestArtifact(unittest.TestCase):
+
+    def test_translate_query(self):
+        fields = ['foo_s', 'bar_ws']
+        query = 'foo:1 AND bar:2 AND foo_bar_baz:3'
+        q = M.Artifact.translate_query(query, fields)
+        self.assertEqual(q, 'foo_s:1 AND bar_ws:2 AND foo_bar_baz:3')
diff --git a/ForgeTracker/forgetracker/tracker_main.py b/ForgeTracker/forgetracker/tracker_main.py
index 3f64528..f76cf74 100644
--- a/ForgeTracker/forgetracker/tracker_main.py
+++ b/ForgeTracker/forgetracker/tracker_main.py
@@ -113,7 +113,7 @@
     elif name == 'mod_date':
         return 'mod_date_dt'
     elif name == 'labels':
-        return 'labels_s'
+        return 'labels_t'
     else:
         for field in c.app.globals.sortable_custom_fields_shown_in_search():
             if name == field['name']:
@@ -458,7 +458,7 @@
                     label='Updated',
                     active=c.app.globals.show_in_search['mod_date']),
                dict(name='labels',
-                   sort_name='labels_s',
+                   sort_name='labels_t',
                    label='Labels',
                    active=c.app.globals.show_in_search['labels']),
                ]
diff --git a/ForgeTracker/forgetracker/widgets/ticket_search.py b/ForgeTracker/forgetracker/widgets/ticket_search.py
index 0417094..4444a60 100644
--- a/ForgeTracker/forgetracker/widgets/ticket_search.py
+++ b/ForgeTracker/forgetracker/widgets/ticket_search.py
@@ -122,7 +122,7 @@
 <h2>Sorting search results</h2>
 <p>Ticket search results can be sorted by clicking the header of the column you want to sort by. The first click will sort the results in ascending order. Clicking the header again will sort the column in descending order. In addition to sorting by the column headers, you can manually sort on these properties:</p>
 <ul>
-<li>Labels assigned to the ticket - labels_s</li>
+<li>Labels assigned to the ticket - labels_t</li>
 <li>Milestone the ticket is assigned to - _milestone_s</li>
 <li>Last modified date - mod_date_dt</li>
 <li>Body of the ticket - text_s</li>
diff --git a/solr_config/schema.xml b/solr_config/schema.xml
index 45aca76..005eab0 100644
--- a/solr_config/schema.xml
+++ b/solr_config/schema.xml
@@ -203,6 +203,7 @@
    <dynamicField name="*_f"  type="float"  indexed="true"  stored="true"/>
    <dynamicField name="*_d"  type="double" indexed="true"  stored="true"/>
    <dynamicField name="*_dt" type="date"    indexed="true"  stored="true"/>
+   <dynamicField name="*_ws" type="text_ws" indexed="true"  stored="true"/>
 
  </fields>
 
@@ -240,6 +241,7 @@
 
    <!-- copy name to alphaNameSort, a field designed for sorting by name -->
    <!-- <copyField source="name" dest="alphaNameSort"/> -->
+   <copyField source="labels_t" dest="labels_ws"/>
 
   <types>
     <!-- field type definitions. The "name" attribute is
diff --git a/vagrant/start_allura b/vagrant/start_allura
index c396f7a..c3f7016 100755
--- a/vagrant/start_allura
+++ b/vagrant/start_allura
@@ -31,7 +31,7 @@
 echo "Logs are in /var/log/allura"
 
 # Start solr
-if pgrep -f solr.*start.jar >/dev/null
+if pgrep -f start.jar >/dev/null
 then
     echo "Solr is running."
 else