public virtual void TestFieldSetValue() { Field field = new Field("id", "id1", Field.Store.YES, Field.Index.NOT_ANALYZED); Document doc = new Document(); doc.Add(field); doc.Add(new Field("keyword", "test", Field.Store.YES, Field.Index.NOT_ANALYZED)); RAMDirectory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); writer.AddDocument(doc); field.SetValue("id2"); writer.AddDocument(doc); field.SetValue("id3"); writer.AddDocument(doc); writer.Close(); Searcher searcher = new IndexSearcher(dir); Query query = new TermQuery(new Term("keyword", "test")); // ensure that queries return expected results without DateFilter first ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs; Assert.AreEqual(3, hits.Length); int result = 0; for (int i = 0; i < 3; i++) { Document doc2 = searcher.Doc(hits[i].doc); Field f = doc2.GetField("id"); if (f.StringValue().Equals("id1")) { result |= 1; } else if (f.StringValue().Equals("id2")) { result |= 2; } else if (f.StringValue().Equals("id3")) { result |= 4; } else { Assert.Fail("unexpected id field"); } } searcher.Close(); dir.Close(); Assert.AreEqual(7, result, "did not see all IDs"); }
/// <summary>Returns the string value of the field with the given name if any exist in /// this document, or null. If multiple fields exist with this name, this /// method returns the first value added. If only binary fields with this name /// exist, returns null. /// </summary> public System.String Get(System.String name) { for (int i = 0; i < fields.Count; i++) { Field field = (Field)fields[i]; if (field.Name().Equals(name) && (!field.IsBinary())) { return(field.StringValue()); } } return(null); }
/// <summary> Returns an array of values of the field specified as the method parameter. /// This method can return <code>null</code>. /// /// </summary> /// <param name="name">the name of the field /// </param> /// <returns> a <code>String[]</code> of field values /// </returns> public System.String[] GetValues(System.String name) { System.Collections.ArrayList result = new System.Collections.ArrayList(); for (int i = 0; i < fields.Count; i++) { Field field = (Field)fields[i]; if (field.Name().Equals(name) && (!field.IsBinary())) { result.Add(field.StringValue()); } } if (result.Count == 0) { return(null); } return((System.String[])(result.ToArray(typeof(System.String)))); }
private static KeyValuePair<string, RavenJToken> CreateProperty(Field fld, Document document) { var stringValue = fld.StringValue(); if (document.GetField(fld.Name() + "_ConvertToJson") != null) { var val = RavenJToken.Parse(fld.StringValue()) as RavenJObject; return new KeyValuePair<string, RavenJToken>(fld.Name(), val); } if (stringValue == Constants.NullValue) stringValue = null; if (stringValue == Constants.EmptyString) stringValue = string.Empty; return new KeyValuePair<string, RavenJToken>(fld.Name(), stringValue); }
static protected Property GetPropertyFromDocument (Field f, Document doc, bool from_primary_index) { // Note: we don't use the document that we pass in, // but in theory we could. At some later point we // might need to split a property's data across two or // more fields in the document. if (f == null) return null; bool internal_prop = false; string field_name; field_name = f.Name (); if (field_name.Length < 7 || ! field_name.StartsWith ("prop:")) { if (DumpIndexMode) internal_prop = true; else return null; } string field_value; field_value = f.StringValue (); Property prop; prop = new Property (); if (DumpIndexMode) { prop.Type = CodeToType ( internal_prop ? 'k' : field_name [5]); prop.Key = (internal_prop ? field_name : field_name.Substring (7)); prop.Value = (internal_prop ? field_value : field_value.Substring (3)); } else { prop.Type = CodeToType (field_name [5]); prop.Key = field_name.Substring (7); prop.Value = field_value.Substring (3); } prop.IsSearched = (field_value [0] == 's'); prop.IsPersistent = (field_value [1] == 'p'); prop.IsMutable = ! from_primary_index; prop.IsStored = true; // Unstored fields cannot be retrieved return prop; }
public virtual void searchIndex(System.String dirName, System.String oldName) { //QueryParser parser = new QueryParser("contents", new WhitespaceAnalyzer()); //Query query = parser.parse("handle:1"); dirName = FullDir(dirName); Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirName)); IndexSearcher searcher = new IndexSearcher(dir, true, null); IndexReader reader = searcher.IndexReader; _TestUtil.CheckIndex(dir); for (int i = 0; i < 35; i++) { if (!reader.IsDeleted(i)) { Document d = reader.Document(i, null); var fields = d.GetFields(); if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22.")) { if (d.GetField("content3") == null) { int numFields = oldName.StartsWith("29.") ? 7 : 5; Assert.AreEqual(numFields, fields.Count); Field f = d.GetField("id"); Assert.AreEqual("" + i, f.StringValue(null)); f = (Field)d.GetField("utf8"); Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue(null)); f = (Field)d.GetField("autf8"); Assert.AreEqual("Lu\uD834\uDD1Ece\uD834\uDD60ne \u0000 \u2620 ab\ud917\udc17cd", f.StringValue(null)); f = (Field)d.GetField("content2"); Assert.AreEqual("here is more content with aaa aaa aaa", f.StringValue(null)); f = (Field)d.GetField("fie\u2C77ld"); Assert.AreEqual("field with non-ascii name", f.StringValue(null)); } } } // Only ID 7 is deleted else { Assert.AreEqual(7, i); } } ScoreDoc[] hits = searcher.Search(new TermQuery(new Term("content", "aaa")), null, 1000, null).ScoreDocs; // First document should be #21 since it's norm was // increased: Document d2 = searcher.Doc(hits[0].Doc, null); Assert.AreEqual("21", d2.Get("id", null), "didn't get the right document first"); TestHits(hits, 34, searcher.IndexReader); if (!oldName.StartsWith("19.") && !oldName.StartsWith("20.") && !oldName.StartsWith("21.") && !oldName.StartsWith("22.")) { // Test on indices >= 2.3 hits = searcher.Search(new TermQuery(new Term("utf8", "\u0000")), null, 1000, null).ScoreDocs; Assert.AreEqual(34, hits.Length); hits = searcher.Search(new TermQuery(new Term("utf8", "Lu\uD834\uDD1Ece\uD834\uDD60ne")), null, 1000, null).ScoreDocs; Assert.AreEqual(34, hits.Length); hits = searcher.Search(new TermQuery(new Term("utf8", "ab\ud917\udc17cd")), null, 1000, null).ScoreDocs; Assert.AreEqual(34, hits.Length); } searcher.Close(); dir.Close(); }
private static JProperty CreateProperty(Field fld, Document document) { if (document.GetField(fld.Name() + "_ConvertToJson") != null) { var val = JsonConvert.DeserializeObject(fld.StringValue()); return new JProperty(fld.Name(), val); } return new JProperty(fld.Name(), fld.StringValue()); }
private static JProperty CreateProperty(Field fld, Document document) { var stringValue = fld.StringValue(); if (document.GetField(fld.Name() + "_ConvertToJson") != null) { object val = JsonConvert.DeserializeObject(stringValue); return new JProperty(fld.Name(), val); } if (stringValue == Constants.NullValue) stringValue = null; return new JProperty(fld.Name(), stringValue); }
// FIXME: This basically queries the value against the field // and is really really slow! private bool FieldIsPredicate (Field field, string value) { string field_name = field.Name (); string field_value = field.StringValue (); Console.WriteLine ("Reverse searching for '{0}' value in {1}='{2}'", value, field_name, field_value); // Simply run the value of the property against the right analyzer // and check if there is any match TokenStream source = IndexingAnalyzer.TokenStream (field_name, new StringReader (field_value)); StringBuilder sb = new StringBuilder (); try { Lucene.Net.Analysis.Token token; while (true) { token = source.Next (); if (token == null) break; sb.Append (token.TermText ()); sb.Append (" "); break; } } finally { try { source.Close (); } catch { } } string field_analyzed = sb.ToString (); sb.Length = 0; source = QueryAnalyzer.TokenStream (field_name, new StringReader (value)); try { Lucene.Net.Analysis.Token token; while (true) { token = source.Next (); if (token == null) break; sb.Append (token.TermText ()); sb.Append (" "); break; } } finally { try { source.Close (); } catch { } } string value_analyzed = sb.ToString (); return field_analyzed.Contains (value_analyzed); }
private void AddFieldRow(string fieldName, Field f) { string feature; ListViewItem item = new ListViewItem("<" + fieldName + ">"); if (f != null && f.IsIndexed()) feature = "+"; else feature = " "; item.SubItems.Add(feature); if (f != null && f.IsTokenized()) feature = "+"; else feature = " "; item.SubItems.Add(feature); if (f != null && f.IsStored()) feature = "+"; else feature = " "; item.SubItems.Add(feature); if (f != null && f.IsTermVectorStored()) feature = "+"; else feature = " "; item.SubItems.Add(feature); if (f != null) item.SubItems.Add(f.GetBoost().ToString("0.0####")); else item.SubItems.Add(""); if (f != null) item.SubItems.Add(f.StringValue()); else { item.SubItems.Add(_luke.resources.GetString("FieldNotAvailable")); } listDocFields.Items.Add(item); }
public SearchField(Field field, IndexField properties) : this(field.Name(), properties.Caption, field.StringValue(), field.StringValue(), properties.Boost, properties.IsTitle, true, properties.Order) { }
public SearchField(Field field, string caption, bool isTitle) : this(field.Name(), caption, field.StringValue(), field.StringValue(), field.GetBoost(), isTitle, true, 0) { }
public SearchField(Field field) : this(field.Name(), field.Name(), field.StringValue(), field.StringValue(), field.GetBoost(), false, false, 0) { }