Search Change-Id strings using ChangeIndex

Move the legacy Change.Id value to _id field and implement change_id
field as the Change-Id string, taking advantage of the existing PREFIX
support added for commit.

Change-Id: I94b924f14474fb2d364819b9950ba3e85a2b2db9
diff --git a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
index 78fa0da..b3d7b05 100644
--- a/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
+++ b/gerrit-lucene/src/main/java/com/google/gerrit/lucene/LuceneChangeIndex.java
@@ -14,7 +14,6 @@
 
 package com.google.gerrit.lucene;
 
-import static com.google.gerrit.server.query.change.ChangeQueryBuilder.FIELD_CHANGE;
 import static com.google.gerrit.server.query.change.IndexRewriteImpl.CLOSED_STATUSES;
 import static com.google.gerrit.server.query.change.IndexRewriteImpl.OPEN_STATUSES;
 import static org.apache.lucene.search.BooleanClause.Occur.MUST;
@@ -104,6 +103,7 @@
   public static final Version LUCENE_VERSION = Version.LUCENE_43;
   public static final String CHANGES_OPEN = "changes_open";
   public static final String CHANGES_CLOSED = "changes_closed";
+  private static final String ID_FIELD = ChangeField.LEGACY_ID.getName();
 
   private static IndexWriterConfig getIndexWriterConfig(Config cfg, String name) {
     IndexWriterConfig writerConfig = new IndexWriterConfig(LUCENE_VERSION,
@@ -223,7 +223,7 @@
   }
 
   private Term idTerm(ChangeData cd) {
-    return intTerm(FIELD_CHANGE, cd.getId().get());
+    return intTerm(ID_FIELD, cd.getId().get());
   }
 
   private Query toQuery(Predicate<ChangeData> p) throws QueryParseException {
@@ -389,7 +389,7 @@
             Lists.newArrayListWithCapacity(docs.scoreDocs.length);
         for (ScoreDoc sd : docs.scoreDocs) {
           Document doc = searchers[sd.shardIndex].doc(sd.doc);
-          Number v = doc.getField(FIELD_CHANGE).numericValue();
+          Number v = doc.getField(ID_FIELD).numericValue();
           result.add(new ChangeData(new Change.Id(v.intValue())));
         }
 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/index/ChangeField.java b/gerrit-server/src/main/java/com/google/gerrit/server/index/ChangeField.java
index 726e239..e20959e 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/index/ChangeField.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/index/ChangeField.java
@@ -44,11 +44,11 @@
  */
 public class ChangeField {
   /** Increment whenever making schema changes. */
-  public static final int SCHEMA_VERSION = 6;
+  public static final int SCHEMA_VERSION = 7;
 
   /** Legacy change ID. */
-  public static final FieldDef<ChangeData, Integer> CHANGE_ID =
-      new FieldDef.Single<ChangeData, Integer>(ChangeQueryBuilder.FIELD_CHANGE,
+  public static final FieldDef<ChangeData, Integer> LEGACY_ID =
+      new FieldDef.Single<ChangeData, Integer>("_id",
           FieldType.INTEGER, true) {
         @Override
         public Integer get(ChangeData input, FillArgs args) {
@@ -56,6 +56,17 @@
         }
       };
 
+  /** Newer style Change-Id key. */
+  public static final FieldDef<ChangeData, String> ID =
+      new FieldDef.Single<ChangeData, String>("change_id",
+          FieldType.PREFIX, true) {
+        @Override
+        public String get(ChangeData input, FillArgs args)
+            throws OrmException {
+          return input.change(args.db).getKey().get();
+        }
+      };
+
   /** Change status string, in the same format as {@code status:}. */
   public static final FieldDef<ChangeData, String> STATUS =
       new FieldDef.Single<ChangeData, String>(ChangeQueryBuilder.FIELD_STATUS,
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeIdPredicate.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeIdPredicate.java
index 7116aa9..457d657 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeIdPredicate.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/ChangeIdPredicate.java
@@ -16,17 +16,18 @@
 
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.query.OperatorPredicate;
+import com.google.gerrit.server.index.ChangeField;
+import com.google.gerrit.server.index.IndexPredicate;
 import com.google.gwtorm.server.OrmException;
 import com.google.gwtorm.server.ResultSet;
 import com.google.inject.Provider;
 
-class ChangeIdPredicate extends OperatorPredicate<ChangeData> implements
+class ChangeIdPredicate extends IndexPredicate<ChangeData> implements
     ChangeDataSource {
   private final Provider<ReviewDb> dbProvider;
 
   ChangeIdPredicate(Provider<ReviewDb> dbProvider, String id) {
-    super(ChangeQueryBuilder.FIELD_CHANGE, id);
+    super(ChangeField.ID, ChangeQueryBuilder.FIELD_CHANGE, id);
     this.dbProvider = dbProvider;
   }
 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/LegacyChangeIdPredicate.java b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/LegacyChangeIdPredicate.java
index 4937d6c..48ae48f 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/query/change/LegacyChangeIdPredicate.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/query/change/LegacyChangeIdPredicate.java
@@ -31,7 +31,7 @@
   private final Change.Id id;
 
   LegacyChangeIdPredicate(Provider<ReviewDb> db, Change.Id id) {
-    super(ChangeField.CHANGE_ID, id.toString());
+    super(ChangeField.LEGACY_ID, ChangeQueryBuilder.FIELD_CHANGE, id.toString());
     this.db = db;
     this.id = id;
   }