<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_TODAY ^_^</title><subtitle type="text">To be a Senior Programmer</subtitle><id>http://feed.cnblogs.com/blog/u/26350/rss</id><updated>2012-05-28T09:50:27Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/26350/rss"/><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/05/22/2513398.html</id><title type="text">Lucene Boost 精度表</title><summary type="text">Lucene中索引阶段Boost的值会被存放在.nrm文件中，用16进制打开就能看的到。每个Boost值最终会被保存于一个Byte中，如果细心的同学就会发现在SetBoost的时候参数类型是float，float是四个字节的，也就是说最后会有精度丢失。废了方便查询给出代码和0-255对应的float下面是换算的代码： public static sbyte FloatToByte315(float f) { int num = BitConverter.ToInt32(BitConverter.GetBytes(f), 0); ...</summary><published>2012-05-22T07:31:00Z</published><updated>2012-05-22T07:31:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/05/22/2513398.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/05/22/2513398.html"/><content type="html">&lt;p&gt;Lucene中索引阶段Boost的值会被存放在.nrm文件中，用16进制打开就能看的到。&lt;/p&gt;&lt;p&gt;每个Boost值最终会被保存于一个Byte中，如果细心的同学就会发现在SetBoost的时候参数类型是float，float是四个字节的，也就是说最后会有精度丢失。&lt;br /&gt;废了方便查询给出代码和0-255对应的float&lt;/p&gt;&lt;p&gt;下面是换算的代码：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        public static sbyte FloatToByte315(float f)&lt;br/&gt;        {&lt;br/&gt;            int num = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);&lt;br/&gt;            int num2 = num &amp;gt;&amp;gt; 0x15;&lt;br/&gt;            if (num2 &amp;lt; 0x180)&lt;br/&gt;            {&lt;br/&gt;                if (num &amp;gt; 0) return 1;&lt;br/&gt;                return 0;&lt;br/&gt;            }&lt;br/&gt;            if (num2 &amp;gt;= 640) return -1;&lt;br/&gt;            return (sbyte) (num2 - 0x180);&lt;br/&gt;        }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        public static float Byte315ToFloat(byte b)&lt;br/&gt;        {&lt;br/&gt;            if (b == 0) return 0f;&lt;br/&gt;            int num = (b &amp;amp; 0xff) &amp;lt;&amp;lt; 0x15;&lt;br/&gt;            num += 0x30000000;&lt;br/&gt;            return BitConverter.ToSingle(BitConverter.GetBytes(num), 0);&lt;br/&gt;        }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;0&lt;br /&gt;5.820766E-10&lt;br /&gt;6.984919E-10&lt;br /&gt;8.149073E-10&lt;br /&gt;9.313226E-10&lt;br /&gt;1.164153E-09&lt;br /&gt;1.396984E-09&lt;br /&gt;1.629815E-09&lt;br /&gt;1.862645E-09&lt;br /&gt;2.328306E-09&lt;br /&gt;2.793968E-09&lt;br /&gt;3.259629E-09&lt;br /&gt;3.72529E-09&lt;br /&gt;4.656613E-09&lt;br /&gt;5.587935E-09&lt;br /&gt;6.519258E-09&lt;br /&gt;7.450581E-09&lt;br /&gt;9.313226E-09&lt;br /&gt;1.117587E-08&lt;br /&gt;1.303852E-08&lt;br /&gt;1.490116E-08&lt;br /&gt;1.862645E-08&lt;br /&gt;2.235174E-08&lt;br /&gt;2.607703E-08&lt;br /&gt;2.980232E-08&lt;br /&gt;3.72529E-08&lt;br /&gt;4.470348E-08&lt;br /&gt;5.215406E-08&lt;br /&gt;5.960464E-08&lt;br /&gt;7.450581E-08&lt;br /&gt;8.940697E-08&lt;br /&gt;1.043081E-07&lt;br /&gt;1.192093E-07&lt;br /&gt;1.490116E-07&lt;br /&gt;1.788139E-07&lt;br /&gt;2.086163E-07&lt;br /&gt;2.384186E-07&lt;br /&gt;2.980232E-07&lt;br /&gt;3.576279E-07&lt;br /&gt;4.172325E-07&lt;br /&gt;4.768372E-07&lt;br /&gt;5.960464E-07&lt;br /&gt;7.152557E-07&lt;br /&gt;8.34465E-07&lt;br /&gt;9.536743E-07&lt;br /&gt;1.192093E-06&lt;br /&gt;1.430511E-06&lt;br /&gt;1.66893E-06&lt;br /&gt;1.907349E-06&lt;br /&gt;2.384186E-06&lt;br /&gt;2.861023E-06&lt;br /&gt;3.33786E-06&lt;br /&gt;3.814697E-06&lt;br /&gt;4.768372E-06&lt;br /&gt;5.722046E-06&lt;br /&gt;6.67572E-06&lt;br /&gt;7.629395E-06&lt;br /&gt;9.536743E-06&lt;br /&gt;1.144409E-05&lt;br /&gt;1.335144E-05&lt;br /&gt;1.525879E-05&lt;br /&gt;1.907349E-05&lt;br /&gt;2.288818E-05&lt;br /&gt;2.670288E-05&lt;br /&gt;3.051758E-05&lt;br /&gt;3.814697E-05&lt;br /&gt;4.577637E-05&lt;br /&gt;5.340576E-05&lt;br /&gt;6.103516E-05&lt;br /&gt;7.629395E-05&lt;br /&gt;9.155273E-05&lt;br /&gt;0.0001068115&lt;br /&gt;0.0001220703&lt;br /&gt;0.0001525879&lt;br /&gt;0.0001831055&lt;br /&gt;0.000213623&lt;br /&gt;0.0002441406&lt;br /&gt;0.0003051758&lt;br /&gt;0.0003662109&lt;br /&gt;0.0004272461&lt;br /&gt;0.0004882813&lt;br /&gt;0.0006103516&lt;br /&gt;0.0007324219&lt;br /&gt;0.0008544922&lt;br /&gt;0.0009765625&lt;br /&gt;0.001220703&lt;br /&gt;0.001464844&lt;br /&gt;0.001708984&lt;br /&gt;0.001953125&lt;br /&gt;0.002441406&lt;br /&gt;0.002929688&lt;br /&gt;0.003417969&lt;br /&gt;0.00390625&lt;br /&gt;0.004882813&lt;br /&gt;0.005859375&lt;br /&gt;0.006835938&lt;br /&gt;0.0078125&lt;br /&gt;0.009765625&lt;br /&gt;0.01171875&lt;br /&gt;0.01367188&lt;br /&gt;0.015625&lt;br /&gt;0.01953125&lt;br /&gt;0.0234375&lt;br /&gt;0.02734375&lt;br /&gt;0.03125&lt;br /&gt;0.0390625&lt;br /&gt;0.046875&lt;br /&gt;0.0546875&lt;br /&gt;0.0625&lt;br /&gt;0.078125&lt;br /&gt;0.09375&lt;br /&gt;0.109375&lt;br /&gt;0.125&lt;br /&gt;0.15625&lt;br /&gt;0.1875&lt;br /&gt;0.21875&lt;br /&gt;0.25&lt;br /&gt;0.3125&lt;br /&gt;0.375&lt;br /&gt;0.4375&lt;br /&gt;0.5&lt;br /&gt;0.625&lt;br /&gt;0.75&lt;br /&gt;0.875&lt;br /&gt;1&lt;br /&gt;1.25&lt;br /&gt;1.5&lt;br /&gt;1.75&lt;br /&gt;2&lt;br /&gt;2.5&lt;br /&gt;3&lt;br /&gt;3.5&lt;br /&gt;4&lt;br /&gt;5&lt;br /&gt;6&lt;br /&gt;7&lt;br /&gt;8&lt;br /&gt;10&lt;br /&gt;12&lt;br /&gt;14&lt;br /&gt;16&lt;br /&gt;20&lt;br /&gt;24&lt;br /&gt;28&lt;br /&gt;32&lt;br /&gt;40&lt;br /&gt;48&lt;br /&gt;56&lt;br /&gt;64&lt;br /&gt;80&lt;br /&gt;96&lt;br /&gt;112&lt;br /&gt;128&lt;br /&gt;160&lt;br /&gt;192&lt;br /&gt;224&lt;br /&gt;256&lt;br /&gt;320&lt;br /&gt;384&lt;br /&gt;448&lt;br /&gt;512&lt;br /&gt;640&lt;br /&gt;768&lt;br /&gt;896&lt;br /&gt;1024&lt;br /&gt;1280&lt;br /&gt;1536&lt;br /&gt;1792&lt;br /&gt;2048&lt;br /&gt;2560&lt;br /&gt;3072&lt;br /&gt;3584&lt;br /&gt;4096&lt;br /&gt;5120&lt;br /&gt;6144&lt;br /&gt;7168&lt;br /&gt;8192&lt;br /&gt;10240&lt;br /&gt;12288&lt;br /&gt;14336&lt;br /&gt;16384&lt;br /&gt;20480&lt;br /&gt;24576&lt;br /&gt;28672&lt;br /&gt;32768&lt;br /&gt;40960&lt;br /&gt;49152&lt;br /&gt;57344&lt;br /&gt;65536&lt;br /&gt;81920&lt;br /&gt;98304&lt;br /&gt;114688&lt;br /&gt;131072&lt;br /&gt;163840&lt;br /&gt;196608&lt;br /&gt;229376&lt;br /&gt;262144&lt;br /&gt;327680&lt;br /&gt;393216&lt;br /&gt;458752&lt;br /&gt;524288&lt;br /&gt;655360&lt;br /&gt;786432&lt;br /&gt;917504&lt;br /&gt;1048576&lt;br /&gt;1310720&lt;br /&gt;1572864&lt;br /&gt;1835008&lt;br /&gt;2097152&lt;br /&gt;2621440&lt;br /&gt;3145728&lt;br /&gt;3670016&lt;br /&gt;4194304&lt;br /&gt;5242880&lt;br /&gt;6291456&lt;br /&gt;7340032&lt;br /&gt;8388608&lt;br /&gt;1.048576E+07&lt;br /&gt;1.258291E+07&lt;br /&gt;1.468006E+07&lt;br /&gt;1.677722E+07&lt;br /&gt;2.097152E+07&lt;br /&gt;2.516582E+07&lt;br /&gt;2.936013E+07&lt;br /&gt;3.355443E+07&lt;br /&gt;4.194304E+07&lt;br /&gt;5.033165E+07&lt;br /&gt;5.872026E+07&lt;br /&gt;6.710886E+07&lt;br /&gt;8.388608E+07&lt;br /&gt;1.006633E+08&lt;br /&gt;1.174405E+08&lt;br /&gt;1.342177E+08&lt;br /&gt;1.677722E+08&lt;br /&gt;2.013266E+08&lt;br /&gt;2.34881E+08&lt;br /&gt;2.684355E+08&lt;br /&gt;3.355443E+08&lt;br /&gt;4.026532E+08&lt;br /&gt;4.69762E+08&lt;br /&gt;5.368709E+08&lt;br /&gt;6.710886E+08&lt;br /&gt;8.053064E+08&lt;br /&gt;9.395241E+08&lt;br /&gt;1.073742E+09&lt;br /&gt;1.342177E+09&lt;br /&gt;1.610613E+09&lt;br /&gt;1.879048E+09&lt;br /&gt;2.147484E+09&lt;br /&gt;2.684355E+09&lt;br /&gt;3.221225E+09&lt;br /&gt;3.758096E+09&lt;br /&gt;4.294967E+09&lt;br /&gt;5.368709E+09&lt;br /&gt;6.442451E+09&lt;br /&gt;7.516193E+09&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2513398.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/05/22/2513398.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/05/15/2501991.html</id><title type="text">Lucene Document getBoost(float) 和 setBoost(float)</title><summary type="text">今天写了个单元测试，看看权重的变化，结果发现索引的时候啥都好好的，索引完拿出来看都是1，困惑了好一会，还好lucene的文档给出了解释：getBoostpublic float getBoost()Returns, at indexing time, the boost factor as set by setBoost(float).Note that once a document is indexed this value is no longer available from the index. At search time, for retrieved documents, thi</summary><published>2012-05-15T09:56:00Z</published><updated>2012-05-15T09:56:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/05/15/2501991.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/05/15/2501991.html"/><content type="html">&lt;p&gt;今天写了个单元测试，看看权重的变化，结果发现索引的时候啥都好好的，索引完拿出来看都是1，困惑了好一会，还好lucene的文档给出了解释：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;&lt;strong&gt;getBoost&lt;/strong&gt;&lt;/p&gt;public float &lt;strong&gt;getBoost&lt;/strong&gt;()&lt;p&gt;Returns, at indexing time, the boost factor as set by &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/document/Document.html#setBoost%28float%29"&gt;&lt;code&gt;setBoost(float)&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Note that once a document is indexed this value is no longer available from the index. At search time, for retrieved documents, this method always returns 1. This however does not mean that the boost value set at indexing time was ignored - it was just combined with other indexing time factors and stored elsewhere, for better indexing and search performance. (For more information see the "norm(t,d)" part of the scoring formula in &lt;a title="class in org.apache.lucene.search" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html"&gt;&lt;code&gt;Similarity&lt;/code&gt;&lt;/a&gt;.)&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;翻译：在索引的时候，激励因子由函数SetBoost（float）设置。注意一旦文档被写入索引，这个值将不再有效（或者说不可逆），在搜索时取出的文档中这个方法总是返回1。然而这并不意味着索引时设置的激励因子被无视-它只是与各种因子合并并存储在了某处，这是为了更好的索引和高效的搜索。&lt;/p&gt;&lt;p&gt;顺便贴上合并的文档：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;public abstract class &lt;strong&gt;Similarity&lt;/strong&gt;extends &lt;a title="class or interface in java.lang" href="http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html?is-external=true"&gt;Object&lt;/a&gt;implements &lt;a title="class or interface in java.io" href="http://java.sun.com/j2se/1.4/docs/api/java/io/Serializable.html?is-external=true"&gt;Serializable&lt;/a&gt;&lt;p&gt;Expert: Scoring API.&lt;/p&gt;&lt;p&gt;Similarity defines the components of Lucene scoring. Overriding computation of these components is a convenient way to alter Lucene scoring.&lt;/p&gt;&lt;p&gt;Suggested reading: &lt;a href="http://nlp.stanford.edu/IR-book/html/htmledition/queries-as-vectors-1.html"&gt; Introduction To Information Retrieval, Chapter 6&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The following describes how Lucene scoring evolves from underlying information retrieval models to (efficient) implementation. We first brief on &lt;em&gt;VSM Score&lt;/em&gt;, then derive from it &lt;em&gt;Lucene's Conceptual Scoring Formula&lt;/em&gt;, from which, finally, evolves &lt;em&gt;Lucene's Practical Scoring Function&lt;/em&gt; (the latter is connected directly with Lucene classes and methods).&lt;/p&gt;&lt;p&gt;Lucene combines &lt;a href="http://en.wikipedia.org/wiki/Standard_Boolean_model"&gt; Boolean model (BM) of Information Retrieval&lt;/a&gt; with &lt;a href="http://en.wikipedia.org/wiki/Vector_Space_Model"&gt; Vector Space Model (VSM) of Information Retrieval&lt;/a&gt; - documents "approved" by BM are scored by VSM.&lt;/p&gt;&lt;p&gt;In VSM, documents and queries are represented as weighted vectors in a multi-dimensional space, where each distinct index term is a dimension, and weights are &lt;a href="http://en.wikipedia.org/wiki/Tfidf"&gt;Tf-idf&lt;/a&gt; values.&lt;/p&gt;&lt;p&gt;VSM does not require weights to be &lt;em&gt;Tf-idf&lt;/em&gt; values, but &lt;em&gt;Tf-idf&lt;/em&gt; values are believed to produce search results of high quality, and so Lucene is using &lt;em&gt;Tf-idf&lt;/em&gt;. &lt;em&gt;Tf&lt;/em&gt; and &lt;em&gt;Idf&lt;/em&gt; are described in more detail below, but for now, for completion, let's just say that for given term &lt;em&gt;t&lt;/em&gt; and document (or query) &lt;em&gt;x&lt;/em&gt;, &lt;em&gt;Tf(t,x)&lt;/em&gt; varies with the number of occurrences of term &lt;em&gt;t&lt;/em&gt; in &lt;em&gt;x&lt;/em&gt; (when one increases so does the other) and &lt;em&gt;idf(t)&lt;/em&gt; similarly varies with the inverse of the number of index documents containing term &lt;em&gt;t&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;&lt;em&gt;VSM score&lt;/em&gt; of document &lt;em&gt;d&lt;/em&gt; for query &lt;em&gt;q&lt;/em&gt; is the &lt;a href="http://en.wikipedia.org/wiki/Cosine_similarity"&gt; Cosine Similarity&lt;/a&gt; of the weighted query vectors &lt;em&gt;V(q)&lt;/em&gt; and &lt;em&gt;V(d)&lt;/em&gt;: &lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;/p&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="1" cellspacing="0" cellpadding="1" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            cosine-similarity(q,d) &amp;nbsp; = &amp;nbsp;          &lt;/td&gt;&lt;td align="center" valign="middle"&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;V(q)&amp;nbsp;&amp;middot;&amp;nbsp;V(d)&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;|V(q)|&amp;nbsp;|V(d)|&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;            &lt;/tbody&gt;&lt;/table&gt;          &lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;/td&gt;&lt;/tr&gt;    &lt;/tbody&gt;&lt;/table&gt;    &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;span style="text-decoration: underline;"&gt;VSM Score&lt;/span&gt;&lt;/center&gt;    &lt;/td&gt;&lt;/tr&gt;  &lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;  &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    Where &lt;em&gt;V(q)&lt;/em&gt; &amp;middot; &lt;em&gt;V(d)&lt;/em&gt; is the &lt;a href="http://en.wikipedia.org/wiki/Dot_product"&gt;dot product&lt;/a&gt; of the weighted vectors, and &lt;em&gt;|V(q)|&lt;/em&gt; and &lt;em&gt;|V(d)|&lt;/em&gt; are their &lt;a href="http://en.wikipedia.org/wiki/Euclidean_norm#Euclidean_norm"&gt;Euclidean norms&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Note: the above equation can be viewed as the dot product of the normalized weighted vectors, in the sense that dividing &lt;em&gt;V(q)&lt;/em&gt; by its euclidean norm is normalizing it to a unit vector. &lt;/p&gt;&lt;p&gt;Lucene refines &lt;em&gt;VSM score&lt;/em&gt; for both search quality and usability: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;Normalizing &lt;em&gt;V(d)&lt;/em&gt; to the unit vector is known to be problematic in that   it removes all document length information.   For some documents removing this info is probably ok,   e.g. a document made by duplicating a certain paragraph &lt;em&gt;10&lt;/em&gt; times,  especially if that paragraph is made of distinct terms.   But for a document which contains no duplicated paragraphs,   this might be wrong.   To avoid this problem, a different document length normalization   factor is used, which normalizes to a vector equal to or larger   than the unit vector: &lt;em&gt;doc-len-norm(d)&lt;/em&gt;.  &lt;/li&gt;&lt;li&gt;At indexing, users can specify that certain documents are more  important than others, by assigning a document boost.  For this, the score of each document is also multiplied by its boost value  &lt;em&gt;doc-boost(d)&lt;/em&gt;.  &lt;/li&gt;&lt;li&gt;Lucene is field based, hence each query term applies to a single  field, document length normalization is by the length of the certain field,  and in addition to document boost there are also document fields boosts.  &lt;/li&gt;&lt;li&gt;The same field can be added to a document during indexing several times,  and so the boost of that field is the multiplication of the boosts of  the separate additions (or parts) of that field within the document.  &lt;/li&gt;&lt;li&gt;At search time users can specify boosts to each query, sub-query, and  each query term, hence the contribution of a query term to the score of  a document is multiplied by the boost of that query term &lt;em&gt;query-boost(q)&lt;/em&gt;.  &lt;/li&gt;&lt;li&gt;A document may match a multi term query without containing all  the terms of that query (this is correct for some of the queries),  and users can further reward documents matching more query terms  through a coordination factor, which is usually larger when  more terms are matched: &lt;em&gt;coord-factor(q,d)&lt;/em&gt;.  &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Under the simplifying assumption of a single field in the index, we get &lt;em&gt;Lucene's Conceptual scoring formula&lt;/em&gt;:  &lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;/p&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="1" cellspacing="0" cellpadding="1" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            score(q,d) &amp;nbsp; = &amp;nbsp;            &lt;span style="color: #ff9933;"&gt;coord-factor(q,d)&lt;/span&gt; &amp;middot; &amp;nbsp;            &lt;span style="color: #cccc00;"&gt;query-boost(q)&lt;/span&gt; &amp;middot; &amp;nbsp;          &lt;/td&gt;&lt;td align="center" valign="middle"&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;&lt;span style="color: #993399;"&gt;V(q)&amp;nbsp;&amp;middot;&amp;nbsp;V(d)&lt;/span&gt;&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;&lt;span style="color: #ff33cc;"&gt;|V(q)|&lt;/span&gt;&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;            &lt;/tbody&gt;&lt;/table&gt;          &lt;/td&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            &amp;nbsp; &amp;middot; &amp;nbsp; &lt;span style="color: #3399ff;"&gt;doc-len-norm(d)&lt;/span&gt;            &amp;nbsp; &amp;middot; &amp;nbsp; &lt;span style="color: #3399ff;"&gt;doc-boost(d)&lt;/span&gt;          &lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;/td&gt;&lt;/tr&gt;    &lt;/tbody&gt;&lt;/table&gt;    &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;span style="text-decoration: underline;"&gt;Lucene Conceptual Scoring Formula&lt;/span&gt;&lt;/center&gt;    &lt;/td&gt;&lt;/tr&gt;  &lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;  &lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;The conceptual formula is a simplification in the sense that (1) terms and documents are fielded and (2) boosts are usually per query term rather than per query. &lt;/p&gt;&lt;p&gt;We now describe how Lucene implements this conceptual scoring formula, and derive from it &lt;em&gt;Lucene's Practical Scoring Function&lt;/em&gt;.   &lt;/p&gt;&lt;p&gt;For efficient score computation some scoring components are computed and aggregated in advance: &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;em&gt;Query-boost&lt;/em&gt; for the query (actually for each query term)  is known when search starts.  &lt;/li&gt;&lt;li&gt;Query Euclidean norm &lt;em&gt;|V(q)|&lt;/em&gt; can be computed when search starts,  as it is independent of the document being scored.  From search optimization perspective, it is a valid question  why bother to normalize the query at all, because all  scored documents will be multiplied by the same &lt;em&gt;|V(q)|&lt;/em&gt;,  and hence documents ranks (their order by score) will not  be affected by this normalization.  There are two good reasons to keep this normalization:&lt;ul&gt;&lt;li&gt;Recall that   &lt;a href="http://en.wikipedia.org/wiki/Cosine_similarity"&gt;   Cosine Similarity&lt;/a&gt; can be used find how similar   two documents are. One can use Lucene for e.g.   clustering, and use a document as a query to compute   its similarity to other documents.   In this use case it is important that the score of document &lt;em&gt;d3&lt;/em&gt;   for query &lt;em&gt;d1&lt;/em&gt; is comparable to the score of document &lt;em&gt;d3&lt;/em&gt;   for query &lt;em&gt;d2&lt;/em&gt;. In other words, scores of a document for two   distinct queries should be comparable.   There are other applications that may require this.   And this is exactly what normalizing the query vector &lt;em&gt;V(q)&lt;/em&gt;   provides: comparability (to a certain extent) of two or more queries.   &lt;/li&gt;&lt;li&gt;Applying query normalization on the scores helps to keep the   scores around the unit vector, hence preventing loss of score data   because of floating point precision limitations.   &lt;/li&gt;&lt;/ul&gt;  &lt;/li&gt;&lt;li&gt;Document length norm &lt;em&gt;doc-len-norm(d)&lt;/em&gt; and document  boost &lt;em&gt;doc-boost(d)&lt;/em&gt; are known at indexing time.  They are computed in advance and their multiplication  is saved as a single value in the index: &lt;em&gt;norm(d)&lt;/em&gt;.  (In the equations below, &lt;em&gt;norm(t in d)&lt;/em&gt; means &lt;em&gt;norm(field(t) in doc d)&lt;/em&gt;  where &lt;em&gt;field(t)&lt;/em&gt; is the field associated with term &lt;em&gt;t&lt;/em&gt;.)  &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;em&gt;Lucene's Practical Scoring Function&lt;/em&gt; is derived from the above. The color codes demonstrate how it relates to those of the &lt;em&gt;conceptual&lt;/em&gt; formula: &lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="2" cellspacing="2" cellpadding="" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;       score(q,d) &amp;nbsp; = &amp;nbsp;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_coord"&gt;&lt;span style="color: #ff9933;"&gt;coord(q,d)&lt;/span&gt;&lt;/a&gt; &amp;nbsp;&amp;middot;&amp;nbsp;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_queryNorm"&gt;&lt;span style="color: #ff33cc;"&gt;queryNorm(q)&lt;/span&gt;&lt;/a&gt; &amp;nbsp;&amp;middot;&amp;nbsp;     &lt;/td&gt;&lt;td rowspan="1" align="center" valign="bottom"&gt;       &lt;big&gt;&lt;big&gt;&lt;big&gt;&amp;sum;&lt;/big&gt;&lt;/big&gt;&lt;/big&gt;     &lt;/td&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;       &lt;big&gt;&lt;big&gt;(&lt;/big&gt;&lt;/big&gt;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_tf"&gt;&lt;span style="color: #993399;"&gt;tf(t in d)&lt;/span&gt;&lt;/a&gt; &amp;nbsp;&amp;middot;&amp;nbsp;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_idf"&gt;&lt;span style="color: #993399;"&gt;idf(t)&lt;/span&gt;&lt;/a&gt;&lt;sup&gt;2&lt;/sup&gt; &amp;nbsp;&amp;middot;&amp;nbsp;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_termBoost"&gt;&lt;span style="color: #cccc00;"&gt;t.getBoost()&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&amp;middot;&amp;nbsp;       &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_norm"&gt;&lt;span style="color: #3399ff;"&gt;norm(t,d)&lt;/span&gt;&lt;/a&gt;       &lt;big&gt;&lt;big&gt;)&lt;/big&gt;&lt;/big&gt;     &lt;/td&gt;   &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td align="center"&gt;&lt;small&gt;t in q&lt;/small&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;   &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;/td&gt;&lt;/tr&gt;  &lt;/tbody&gt;&lt;/table&gt; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;span style="text-decoration: underline;"&gt;Lucene Practical Scoring Function&lt;/span&gt;&lt;/center&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;p&gt; where &lt;/p&gt;&lt;ol&gt;&lt;ol&gt;&lt;li&gt;      &lt;a name="formula_tf"&gt;&lt;/a&gt;      &lt;strong&gt;&lt;em&gt;tf(t in d)&lt;/em&gt;&lt;/strong&gt;      correlates to the term's &lt;em&gt;frequency&lt;/em&gt;,      defined as the number of times term &lt;em&gt;t&lt;/em&gt; appears in the currently scored document &lt;em&gt;d&lt;/em&gt;.      Documents that have more occurrences of a given term receive a higher score.      Note that &lt;em&gt;tf(t in q)&lt;/em&gt; is assumed to be &lt;em&gt;1&lt;/em&gt; and therefore it does not appear in this equation,      However if a query contains twice the same term, there will be      two term-queries with that same term and hence the computation would still be correct (although      not very efficient).      The default computation for &lt;em&gt;tf(t in d)&lt;/em&gt; in      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#tf%28float%29"&gt;&lt;code&gt;DefaultSimilarity&lt;/code&gt;&lt;/a&gt; is:      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#tf%28float%29"&gt;&lt;code&gt;tf(t in d)&lt;/code&gt;&lt;/a&gt; &amp;nbsp; = &amp;nbsp;          &lt;/td&gt;&lt;td rowspan="1" align="center" valign="top"&gt;               frequency&lt;sup&gt;&lt;big&gt;&amp;frac12;&lt;/big&gt;&lt;/sup&gt;          &lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;/li&gt;&lt;li&gt;      &lt;a name="formula_idf"&gt;&lt;/a&gt;      &lt;strong&gt;&lt;em&gt;idf(t)&lt;/em&gt;&lt;/strong&gt; stands for Inverse Document Frequency. This value      correlates to the inverse of &lt;em&gt;docFreq&lt;/em&gt;      (the number of documents in which the term &lt;em&gt;t&lt;/em&gt; appears).      This means rarer terms give higher contribution to the total score.      &lt;em&gt;idf(t)&lt;/em&gt; appears for &lt;em&gt;t&lt;/em&gt; in both the query and the document,      hence it is squared in the equation.      The default computation for &lt;em&gt;idf(t)&lt;/em&gt; in      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#idf%28int,%20int%29"&gt;&lt;code&gt;DefaultSimilarity&lt;/code&gt;&lt;/a&gt; is:      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table border="0" cellspacing="2" cellpadding="2" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align="right" valign="middle"&gt;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#idf%28int,%20int%29"&gt;&lt;code&gt;idf(t)&lt;/code&gt;&lt;/a&gt;&amp;nbsp; = &amp;nbsp;          &lt;/td&gt;&lt;td align="center" valign="middle"&gt;            1 + log &lt;big&gt;(&lt;/big&gt;          &lt;/td&gt;&lt;td align="center" valign="middle"&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;numDocs&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;small&gt;docFreq+1&lt;/small&gt;&lt;/td&gt;&lt;/tr&gt;            &lt;/tbody&gt;&lt;/table&gt;          &lt;/td&gt;&lt;td align="center" valign="middle"&gt;            &lt;big&gt;)&lt;/big&gt;          &lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;/li&gt;&lt;li&gt;      &lt;a name="formula_coord"&gt;&lt;/a&gt;      &lt;strong&gt;&lt;em&gt;coord(q,d)&lt;/em&gt;&lt;/strong&gt;      is a score factor based on how many of the query terms are found in the specified document.      Typically, a document that contains more of the query's terms will receive a higher score      than another document with fewer query terms.      This is a search time factor computed in      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#coord%28int,%20int%29"&gt;&lt;code&gt;coord(q,d)&lt;/code&gt;&lt;/a&gt;      by the Similarity in effect at search time.      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;/li&gt;&lt;li&gt;&lt;strong&gt;      &lt;a name="formula_queryNorm"&gt;&lt;/a&gt;      &lt;em&gt;queryNorm(q)&lt;/em&gt;      &lt;/strong&gt;      is a normalizing factor used to make scores between queries comparable.      This factor does not affect document ranking (since all ranked documents are multiplied by the same factor),      but rather just attempts to make scores from different queries (or even different indexes) comparable.      This is a search time factor computed by the Similarity in effect at search time.      The default computation in      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#queryNorm%28float%29"&gt;&lt;code&gt;DefaultSimilarity&lt;/code&gt;&lt;/a&gt;      produces a &lt;a href="http://en.wikipedia.org/wiki/Euclidean_norm#Euclidean_norm"&gt;Euclidean norm&lt;/a&gt;:      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table border="0" cellspacing="0" cellpadding="1" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            queryNorm(q)  &amp;nbsp; = &amp;nbsp;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/DefaultSimilarity.html#queryNorm%28float%29"&gt;&lt;code&gt;queryNorm(sumOfSquaredWeights)&lt;/code&gt;&lt;/a&gt;            &amp;nbsp; = &amp;nbsp;          &lt;/td&gt;&lt;td rowspan="1" align="center" valign="middle"&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;big&gt;1&lt;/big&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;&lt;big&gt;                  &amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;&amp;ndash;               &lt;/big&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="center"&gt;sumOfSquaredWeights&lt;sup&gt;&lt;big&gt;&amp;frac12;&lt;/big&gt;&lt;/sup&gt;&lt;/td&gt;&lt;/tr&gt;            &lt;/tbody&gt;&lt;/table&gt;          &lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      The sum of squared weights (of the query terms) is      computed by the query &lt;a title="class in org.apache.lucene.search" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Weight.html"&gt;&lt;code&gt;Weight&lt;/code&gt;&lt;/a&gt; object.      For example, a &lt;a title="class in org.apache.lucene.search" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/BooleanQuery.html"&gt;&lt;code&gt;boolean query&lt;/code&gt;&lt;/a&gt;      computes this value as:      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table border="0" cellspacing="0" cellpadding="1" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Weight.html#sumOfSquaredWeights%28%29"&gt;&lt;code&gt;sumOfSquaredWeights&lt;/code&gt;&lt;/a&gt; &amp;nbsp; = &amp;nbsp;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Query.html#getBoost%28%29"&gt;&lt;code&gt;q.getBoost()&lt;/code&gt;&lt;/a&gt; &lt;sup&gt;&lt;big&gt;2&lt;/big&gt;&lt;/sup&gt;            &amp;nbsp;&amp;middot;&amp;nbsp;          &lt;/td&gt;&lt;td rowspan="1" align="center" valign="bottom"&gt;            &lt;big&gt;&lt;big&gt;&lt;big&gt;&amp;sum;&lt;/big&gt;&lt;/big&gt;&lt;/big&gt;          &lt;/td&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            &lt;big&gt;&lt;big&gt;(&lt;/big&gt;&lt;/big&gt;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_idf"&gt;idf(t)&lt;/a&gt; &amp;nbsp;&amp;middot;&amp;nbsp;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#formula_termBoost"&gt;t.getBoost()&lt;/a&gt;            &lt;big&gt;&lt;big&gt;) &lt;sup&gt;2&lt;/sup&gt; &lt;/big&gt;&lt;/big&gt;          &lt;/td&gt;        &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td align="center"&gt;&lt;small&gt;t in q&lt;/small&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;/li&gt;&lt;li&gt;      &lt;a name="formula_termBoost"&gt;&lt;/a&gt;      &lt;strong&gt;&lt;em&gt;t.getBoost()&lt;/em&gt;&lt;/strong&gt;      is a search time boost of term &lt;em&gt;t&lt;/em&gt; in the query &lt;em&gt;q&lt;/em&gt; as      specified in the query text      (see &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html#Boosting%20a%20Term"&gt;query syntax&lt;/a&gt;),      or as set by application calls to      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Query.html#setBoost%28float%29"&gt;&lt;code&gt;setBoost()&lt;/code&gt;&lt;/a&gt;.      Notice that there is really no direct API for accessing a boost of one term in a multi term query,      but rather multi terms are represented in a query as multi      &lt;a title="class in org.apache.lucene.search" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/TermQuery.html"&gt;&lt;code&gt;TermQuery&lt;/code&gt;&lt;/a&gt; objects,      and so the boost of a term in the query is accessible by calling the sub-query      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Query.html#getBoost%28%29"&gt;&lt;code&gt;getBoost()&lt;/code&gt;&lt;/a&gt;.      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;/li&gt;&lt;li&gt;      &lt;a name="formula_norm"&gt;&lt;/a&gt;      &lt;strong&gt;&lt;em&gt;norm(t,d)&lt;/em&gt;&lt;/strong&gt;encapsulates a few (indexing time) boost and length factors:&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Document boost&lt;/strong&gt; - set by calling        &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/document/Document.html#setBoost%28float%29"&gt;&lt;code&gt;doc.setBoost()&lt;/code&gt;&lt;/a&gt;        before adding the document to the index.        &lt;/li&gt;&lt;li&gt;&lt;strong&gt;Field boost&lt;/strong&gt; - set by calling        &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/document/Fieldable.html#setBoost%28float%29"&gt;&lt;code&gt;field.setBoost()&lt;/code&gt;&lt;/a&gt;        before adding the field to a document.        &lt;/li&gt;&lt;li&gt;&lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#lengthNorm%28java.lang.String,%20int%29"&gt;&lt;code&gt;&lt;strong&gt;lengthNorm&lt;/strong&gt;(field)&lt;/code&gt;&lt;/a&gt; - computed        when the document is added to the index in accordance with the number of tokens        of this field in the document, so that shorter fields contribute more to the score.        LengthNorm is computed by the Similarity class in effect at indexing.        &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;      When a document is added to the index, all the above factors are multiplied.      If the document has multiple fields with the same name, all their boosts are multiplied together:      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      &lt;/p&gt;&lt;table border="0" cellspacing="0" cellpadding="1" align="center"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            norm(t,d) &amp;nbsp; = &amp;nbsp;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/document/Document.html#getBoost%28%29"&gt;&lt;code&gt;doc.getBoost()&lt;/code&gt;&lt;/a&gt;            &amp;nbsp;&amp;middot;&amp;nbsp;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#lengthNorm%28java.lang.String,%20int%29"&gt;&lt;code&gt;lengthNorm(field)&lt;/code&gt;&lt;/a&gt;            &amp;nbsp;&amp;middot;&amp;nbsp;          &lt;/td&gt;&lt;td rowspan="1" align="center" valign="bottom"&gt;            &lt;big&gt;&lt;big&gt;&lt;big&gt;&amp;prod;&lt;/big&gt;&lt;/big&gt;&lt;/big&gt;          &lt;/td&gt;&lt;td rowspan="1" align="right" valign="middle"&gt;            &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/document/Fieldable.html#getBoost%28%29"&gt;&lt;code&gt;f.getBoost&lt;/code&gt;&lt;/a&gt;()          &lt;/td&gt;        &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td align="center"&gt;&lt;small&gt;field &lt;em&gt;&lt;strong&gt;f&lt;/strong&gt;&lt;/em&gt; in &lt;em&gt;d&lt;/em&gt; named as &lt;em&gt;&lt;strong&gt;t&lt;/strong&gt;&lt;/em&gt;&lt;/small&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;        &lt;/tr&gt;      &lt;/tbody&gt;&lt;/table&gt;      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      However the resulted &lt;em&gt;norm&lt;/em&gt; value is &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#encodeNorm%28float%29"&gt;&lt;code&gt;encoded&lt;/code&gt;&lt;/a&gt; as a single byte      before being stored.      At search time, the norm byte value is read from the index      &lt;a title="class in org.apache.lucene.store" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/store/Directory.html"&gt;&lt;code&gt;directory&lt;/code&gt;&lt;/a&gt; and      &lt;a href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html#decodeNorm%28byte%29"&gt;&lt;code&gt;decoded&lt;/code&gt;&lt;/a&gt; back to a float &lt;em&gt;norm&lt;/em&gt; value.      This encoding/decoding, while reducing index size, comes with the price of      precision loss - it is not guaranteed that &lt;em&gt;decode(encode(x)) = x&lt;/em&gt;.      For instance, &lt;em&gt;decode(encode(0.89)) = 0.75&lt;/em&gt;.      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      Compression of norm values to a single byte saves memory at search time,       because once a field is referenced at search time, its norms - for       all documents - are maintained in memory.      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      The rationale supporting such lossy compression of norm values is that      given the difficulty (and inaccuracy) of users to express their true information      need by a query, only big differences matter.      &lt;br /&gt;&amp;nbsp;&lt;br /&gt;      Last, note that search time is too late to modify this &lt;em&gt;norm&lt;/em&gt; part of scoring, e.g. by      using a different &lt;a title="class in org.apache.lucene.search" href="http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/api/core/org/apache/lucene/search/Similarity.html"&gt;&lt;code&gt;Similarity&lt;/code&gt;&lt;/a&gt; for search.      &lt;/li&gt;&lt;/ol&gt;&lt;/ol&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2501991.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/05/15/2501991.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/05/08/2490543.html</id><title type="text">对《LINQ能不能用系列（一）数组筛选效率对比》中测试的几个问题</title><summary type="text">Stone_W 同学写了一篇《LINQ能不能用系列（一）数组筛选效率对比》错误一：作为对比测试，测试数组应该为同一个，否则测试数据没有可比性错误二：对比组中对List的使用不对，List默认创建的数组大小为4，每次增长为4，那么这里就会反复重新创建新的数组次数为log10000000次左右当然会比Linq慢很多错误三：面对Linq接近0毫秒的处理能力，稍微有点经验的同学就知道这是不可能的，除非是很强很强的计算机，至于为什么后面给出答案，总之linq查询里肯定有猫腻，直接调用tolist()强制返回结果再说;//这里Stone W在评论中对ToList有质疑，我之所以ToList是为了和第二组进</summary><published>2012-05-08T10:28:00Z</published><updated>2012-05-08T10:28:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/05/08/2490543.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/05/08/2490543.html"/><content type="html">&lt;p&gt;&lt;span style="font-size: 16px;"&gt;&lt;a href="http://home.cnblogs.com/u/stone_w/"&gt;Stone_W &lt;/a&gt;同学写了一篇&lt;a href="http://www.cnblogs.com/stone_w/archive/2012/05/08/2490440.html" target="_blank"&gt;《LINQ能不能用系列（一）数组筛选效率对比》&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;错误一：作为对比测试，测试数组应该为同一个，否则测试数据没有可比性&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;错误二：对比组中对List的使用不对，List默认创建的数组大小为4，每次增长为4，那么这里就会反复重新创建新的数组次数为log&lt;span style="color: #800080;"&gt;10000000&lt;/span&gt;次左右当然会比Linq慢很多&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;错误三：面对Linq接近0毫秒的处理能力，稍微有点经验的同学就知道这是不可能的，除非是很强很强的计算机，至于为什么后面给出答案，总之linq查询里肯定有猫腻，直接调用tolist()强制返回结果再说;&lt;span style="font-size: 13px; color: #ff0000;"&gt;//这里Stone W在评论中对ToList有质疑，我之所以ToList是为了和第二组进行对比，因为第二组得到的结果是一个List，我很奇怪，这次的对比测试到底是为了测试得到两个结果集的算法对比呢还是测试Count算法的对比呢？如果是前者，一个拿到的是IEnumerable的对象一个是List对象，牛跟闹钟怎么对比哪个跑的快呢？也只有在调用ToList的时候才会真正执行Linq的算法也就是下面的嵌套类WhereListIterator；当然如果是为了进行Count对比的话那么对比组二中的算法真的有点拙劣，我想不会有谁会用方法二来统计。&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;下面是修改了如上三个明显错误后的代码，如果哪位同学有补充欢迎留言：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #000000;"&gt;        [Fact]&lt;br/&gt;        &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; LinqTest()&lt;br/&gt;        {&lt;br/&gt;            TestLinq(&lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br/&gt;            TestLinq(&lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br/&gt;            TestLinq(&lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; TestLinq(&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; time)&lt;br/&gt;        {&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; listCount = &lt;span style="color: #800080;"&gt;10000000&lt;/span&gt;; &lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; 数组长度&lt;/span&gt;&lt;br/&gt;            Random random = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; Random(); &lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; 数据随机构建值&lt;br/&gt;&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; 数组构建 &lt;/span&gt;&lt;br/&gt;            List&amp;lt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&amp;gt; list1 = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&amp;gt;&lt;span style="color: #000000;"&gt;();&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; i = &lt;span style="color: #800080;"&gt;0&lt;/span&gt;; i &amp;lt; listCount; i++&lt;span style="color: #000000;"&gt;)&lt;br/&gt;            {&lt;br/&gt;                list1.Add(random.Next(&lt;/span&gt;&lt;span style="color: #800080;"&gt;10000&lt;/span&gt;&lt;span style="color: #000000;"&gt;));&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; 效率测试内容：提取数组中数值大于的100的数组&lt;br/&gt;&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; LINQ 测试&lt;/span&gt;&lt;br/&gt;            Stopwatch linq_Stopwatch = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Stopwatch();&lt;br/&gt;            linq_Stopwatch.Start();&lt;br/&gt;&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;var&lt;/span&gt; linqList = (&lt;span style="color: #0000ff;"&gt;from&lt;/span&gt; num &lt;span style="color: #0000ff;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; list1&lt;br/&gt;                            &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; num &amp;gt; &lt;span style="color: #800080;"&gt;100&lt;/span&gt;&lt;br/&gt;                            &lt;span style="color: #0000ff;"&gt;select&lt;/span&gt;&lt;span style="color: #000000;"&gt; num).ToList();&lt;br/&gt;            linq_Stopwatch.Stop();&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; 普通方式 测试&lt;/span&gt;&lt;br/&gt;            Stopwatch before_Stopwatch = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Stopwatch();&lt;br/&gt;            before_Stopwatch.Start();&lt;br/&gt;&lt;br/&gt;            List&lt;/span&gt;&amp;lt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&amp;gt; beforeList = &lt;span style="color: #0000ff;"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;&amp;gt;(&lt;span style="color: #800080;"&gt;10000000&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;br/&gt;            &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; i = &lt;span style="color: #800080;"&gt;0&lt;/span&gt;; i &amp;lt; list1.Count(); i++&lt;span style="color: #000000;"&gt;)&lt;br/&gt;            {&lt;br/&gt;                &lt;/span&gt;&lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (list1[i] &amp;gt; &lt;span style="color: #800080;"&gt;100&lt;/span&gt;&lt;span style="color: #000000;"&gt;)&lt;br/&gt;                    beforeList.Add(list1[i]);&lt;br/&gt;            }&lt;br/&gt;            before_Stopwatch.Stop();&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;            Console.WriteLine(&lt;br/&gt;                String.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;第{0}次测试,测试：{5}条数据。\n\r \t LINQ用时：{1}毫秒,筛选了{2}条数据。\n\r\t 普通用时：{3}毫秒,筛选了{4}条数据。\r\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;br/&gt;                              time, linq_Stopwatch.ElapsedMilliseconds, linqList.Count(),&lt;br/&gt;                              before_Stopwatch.ElapsedMilliseconds, beforeList.Count(), listCount));&lt;br/&gt;&lt;br/&gt;        }&lt;/span&gt;&lt;/div&gt;&lt;p&gt;测试结果：&lt;/p&gt;&lt;p&gt;第1次测试,测试：10000000条数据。&lt;br /&gt;      LINQ用时：448毫秒,筛选了9898832条数据。&lt;br /&gt;     普通用时：437毫秒,筛选了9898832条数据。&lt;/p&gt;&lt;p&gt;&lt;br /&gt;第2次测试,测试：10000000条数据。&lt;br /&gt;      LINQ用时：516毫秒,筛选了9899569条数据。&lt;br /&gt;     普通用时：460毫秒,筛选了9899569条数据。&lt;/p&gt;&lt;p&gt;&lt;br /&gt;第3次测试,测试：10000000条数据。&lt;br /&gt;      LINQ用时：608毫秒,筛选了9899231条数据。&lt;br /&gt;     普通用时：470毫秒,筛选了9899231条数据。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff0000;"&gt;结论：Linq在实现灵活性提高编写效率的时候牺牲了一定的性能，当然这个是必须的，有的必有失嘛。&lt;/span&gt;&lt;/p&gt;&lt;p&gt;我的选择：绝大部分时候使用Linq，在对性能要求高的时候使用普通的迭代；&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14pt;"&gt;&amp;nbsp;0毫秒的秘密：&lt;/span&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;var&lt;/span&gt; linqList = (&lt;span style="color: #0000ff;"&gt;from&lt;/span&gt; num &lt;span style="color: #0000ff;"&gt;in&lt;/span&gt; list1 &lt;span style="color: #0000ff;"&gt;where&lt;/span&gt; num &amp;gt; &lt;span style="color: #800080;"&gt;100&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;select&lt;/span&gt; num)&lt;/div&gt;&lt;p&gt;先看看这个LinqList的类型（Console.WriteLine(linqList.GetType().FullName);）：System.Linq.Enumerable+WhereListIterator`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]&lt;/p&gt;&lt;p&gt;可以看到这是一个嵌套类，作用是对Where 条件进行迭代操作，贴上它的源代码：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        class WhereListIterator&amp;lt;TSource&amp;gt; : Iterator&amp;lt;TSource&amp;gt; &lt;br/&gt;        {&lt;br/&gt;            List&amp;lt;TSource&amp;gt; source;&lt;br/&gt;            Func&amp;lt;TSource, bool&amp;gt; predicate;&lt;br/&gt;            List&amp;lt;TSource&amp;gt;.Enumerator enumerator; &lt;br/&gt;&lt;br/&gt;            public WhereListIterator(List&amp;lt;TSource&amp;gt; source, Func&amp;lt;TSource, bool&amp;gt; predicate) { &lt;br/&gt;                this.source = source; &lt;br/&gt;                this.predicate = predicate;&lt;br/&gt;            } &lt;br/&gt;&lt;br/&gt;            public override Iterator&amp;lt;TSource&amp;gt; Clone() {&lt;br/&gt;                return new WhereListIterator&amp;lt;TSource&amp;gt;(source, predicate);&lt;br/&gt;            } &lt;br/&gt;&lt;br/&gt;            public override bool MoveNext() { &lt;br/&gt;                switch (state) { &lt;br/&gt;                    case 1:&lt;br/&gt;                        enumerator = source.GetEnumerator(); &lt;br/&gt;                        state = 2;&lt;br/&gt;                        goto case 2;&lt;br/&gt;                    case 2:&lt;br/&gt;                        while (enumerator.MoveNext()) { &lt;br/&gt;                            TSource item = enumerator.Current;&lt;br/&gt;                            if (predicate(item)) { &lt;br/&gt;                                current = item; &lt;br/&gt;                                return true;&lt;br/&gt;                            } &lt;br/&gt;                        }&lt;br/&gt;                        Dispose();&lt;br/&gt;                        break;&lt;br/&gt;                } &lt;br/&gt;                return false;&lt;br/&gt;            } &lt;br/&gt; &lt;br/&gt;            public override IEnumerable&amp;lt;TResult&amp;gt; Select&amp;lt;TResult&amp;gt;(Func&amp;lt;TSource, TResult&amp;gt; selector) {&lt;br/&gt;                return new WhereSelectListIterator&amp;lt;TSource, TResult&amp;gt;(source, predicate, selector); &lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            public override IEnumerable&amp;lt;TSource&amp;gt; Where(Func&amp;lt;TSource, bool&amp;gt; predicate) {&lt;br/&gt;                return new WhereListIterator&amp;lt;TSource&amp;gt;(source, CombinePredicates(this.predicate, predicate)); &lt;br/&gt;            }&lt;br/&gt;        } &lt;br/&gt;&lt;/div&gt;&lt;p&gt;&lt;span style="font-size: 14pt; color: #ff0000;"&gt;真相大白于天下。&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14pt; color: #ff0000;"&gt;ps：下面是原文代码的截图&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2012/22791/2012050822094159.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14pt; color: #ff0000;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2490543.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/05/08/2490543.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/05/03/2481018.html</id><title type="text">几种Lucene.Net打开IndexReader的方式</title><summary type="text">入门第一式：IndexReader.Open(Dir,readOnly);当readOnly为true的时候效率最高，最好不要用Reader删除修改记录；入门第二式：reader.Reopen(readOnly);优点：比第一式效率高，推荐使用；缺点：前提是需要有Reader存在；入门第三式：writer.GetReader();优点：NRL的实现，对实时性要求高的不二选择；缺点：writer说它很痛苦，因为每次调用内部都会flush一次以保证所有index会被启用；追求效率（中阶）： public class IndexReaderProxy { ...</summary><published>2012-05-03T10:27:00Z</published><updated>2012-05-03T10:27:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/05/03/2481018.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/05/03/2481018.html"/><content type="html">&lt;p&gt;入门第一式：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;IndexReader.Open(Dir,readOnly);&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;当readOnly为true的时候效率最高，最好不要用Reader删除修改记录；&lt;/p&gt;&lt;p&gt;入门第二式：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;reader.Reopen(readOnly);&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;优点：比第一式效率高，推荐使用；&lt;/p&gt;&lt;p&gt;缺点：前提是需要有Reader存在；&lt;/p&gt;&lt;p&gt;入门第三式：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;writer.GetReader();&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;优点：NRL的实现，对实时性要求高的不二选择；&lt;/p&gt;&lt;p&gt;缺点：writer说它很痛苦，因为每次调用内部都会flush一次以保证所有index会被启用；&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;追求效率（中阶）：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;        public class IndexReaderProxy&lt;br/&gt;        {&lt;br/&gt;            private IndexReader _indexReader;&lt;br/&gt;            private readonly object _indexReaderLock = new object();&lt;br/&gt;&lt;br/&gt;            public IndexReaderProxy(Directory directory, bool readOnly)&lt;br/&gt;            {&lt;br/&gt;                _indexReader = IndexReader.Open(directory, readOnly);&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            public IndexReader GetCurrentIndexReader()&lt;br/&gt;            {&lt;br/&gt;                ReopenIndexReaderIfNotCurrent();&lt;br/&gt;                return _indexReader;&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            private void ReopenIndexReaderIfNotCurrent()&lt;br/&gt;            {&lt;br/&gt;                if (_indexReader.IsCurrent()) return;&lt;br/&gt;                lock (_indexReaderLock)&lt;br/&gt;                {&lt;br/&gt;                    if (_indexReader.IsCurrent()) return;&lt;br/&gt;                    var newIndexReader = _indexReader.Reopen();&lt;br/&gt;                    _indexReader.Close();&lt;br/&gt;                    _indexReader = newIndexReader;&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;优点：效率和实时的一个平衡，在对实时要求不高的地方使用；&lt;/p&gt;&lt;p&gt;缺点：不属于NRL范畴，对实时性要求较高的地方就无法使用了；&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2481018.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/05/03/2481018.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/20/2460652.html</id><title type="text">缓存管理的帮助类（更新clear）</title><summary type="text">public class DictionaryCacheManager&lt;TK,TV&gt; { private Dictionary&lt;TK, TV&gt; cacheDic = new Dictionary&lt;TK,TV&gt;(); private Dictionary&lt;TK, object&gt; lockDic = new Dictionary&lt;TK, object&gt;(); public TV Get(TK key,Func&lt;TV&gt; getValue) { if(!CheckKey(key)) { ...</summary><published>2012-04-20T14:46:00Z</published><updated>2012-04-20T14:46:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/20/2460652.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/20/2460652.html"/><content type="html">&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;    public class DictionaryCacheManager&amp;lt;TK,TV&amp;gt;&lt;br/&gt;    {&lt;br/&gt;        private Dictionary&amp;lt;TK, TV&amp;gt; cacheDic = new Dictionary&amp;lt;TK,TV&amp;gt;();&lt;br/&gt;        private Dictionary&amp;lt;TK, object&amp;gt; lockDic = new Dictionary&amp;lt;TK, object&amp;gt;();&lt;br/&gt;&lt;br/&gt;        public TV Get(TK key,Func&amp;lt;TV&amp;gt; getValue)&lt;br/&gt;        {&lt;br/&gt;            if(!CheckKey(key))&lt;br/&gt;            {&lt;br/&gt;                CheckLock(key);&lt;br/&gt;            }&lt;br/&gt;            &lt;br/&gt;            if(!CheckKey(key))&lt;br/&gt;            {&lt;br/&gt;                lock(lockDic[key])&lt;br/&gt;                {&lt;br/&gt;                    if(!CheckKey(key)||cacheDic[key]==null)&lt;br/&gt;                    {&lt;br/&gt;                        cacheDic[key] = getValue();&lt;br/&gt;                    }&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;            return cacheDic[key];&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        private object _lock = new object();&lt;br/&gt;        private void CheckLock(TK key)&lt;br/&gt;        {&lt;br/&gt;            lock(_lock)&lt;br/&gt;            {&lt;br/&gt;                if(!lockDic.ContainsKey(key))&lt;br/&gt;                {&lt;br/&gt;                    lockDic.Add(key, new object());&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        private bool CheckKey(TK key)&lt;br/&gt;        {&lt;br/&gt;            return cacheDic.ContainsKey(key);&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        public void Clear()&lt;br/&gt;        {&lt;br/&gt;            lock(_lock)&lt;br/&gt;            {&lt;br/&gt;                lockDic.Clear();&lt;br/&gt;                cacheDic.Clear();&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;        public void Clear(TK key)&lt;br/&gt;        {&lt;br/&gt;            lock(lockDic[key])&lt;br/&gt;            {&lt;br/&gt;                cacheDic.Remove(key);&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;很简单&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2460652.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/20/2460652.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/19/2456930.html</id><title type="text">未处理的异常导致基于 ASP.NET 的应用程序意外退出</title><summary type="text">问题具体见：http://q.cnblogs.com/q/34874/相应站点的w3wp.exe 会意外终止EventLog中的信息：An unhandled exception occurred and the process was terminated. Application ID: DefaultDomain Process ID: 43644 Exception: System.Runtime.Serialization.SerializationException Message: Unable to find assembly 'Lucene.Net, Version=</summary><published>2012-04-19T05:14:00Z</published><updated>2012-04-19T05:14:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/19/2456930.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/19/2456930.html"/><content type="html">&lt;p&gt;问题具体见：&lt;a href="http://q.cnblogs.com/q/34874/"&gt;http://q.cnblogs.com/q/34874/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;相应站点的w3wp.exe 会意外终止&lt;/p&gt;&lt;div&gt;&lt;p&gt;EventLog中的信息：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;p&gt;An unhandled exception occurred and the process was terminated. &lt;/p&gt;&lt;p&gt; Application ID: DefaultDomain &lt;/p&gt;&lt;p&gt;Process ID: 43644 &lt;/p&gt;&lt;p&gt;Exception:  System.Runtime.Serialization.SerializationException &lt;/p&gt;&lt;p&gt;Message: Unable to  find assembly 'Lucene.Net, Version=2.9.4.1, Culture=neutral,  PublicKeyToken=85089178b9ac3181'. &lt;/p&gt;&lt;p&gt;StackTrace: at  System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()  &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo  assemblyInfo, String name) &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String  objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA,  Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader  objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo,  SizedArray assemIdToAssemblyTable) &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped  record) &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler  handler, __BinaryParser serParser, Boolean fCheck, Boolean  isCrossAppDomain, IMethodCallMessage methodCallMessage) &lt;/p&gt;&lt;p&gt;at  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream  serializationStream, HeaderHandler handler, Boolean fCheck, Boolean  isCrossAppDomain, IMethodCallMessage methodCallMessage) &lt;/p&gt;&lt;p&gt;at  System.AppDomain.Deserialize(Byte[] blob) at  System.AppDomain.UnmarshalObject(Byte[] blob)&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;具体原因和解决方案如下：&lt;/p&gt;&lt;p&gt;&lt;a href="http://support.microsoft.com/kb/911816"&gt;http://support.microsoft.com/kb/911816&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;div&gt;出现这个问题的原因在于未处理异常的默认策略在 .NET Framework 2.0 中已发生更改。默认情况下，未处理异常的策略是结束工作进程。&lt;br /&gt;在 Microsoft .NET Framework 1.1 和 Microsoft .NET Framework 1.0 中，会忽略托管线程上的未处理异常。除非附加调试程序以捕获异常，否则您可能意识不到出错。&lt;br /&gt;ASP.NET 在 .NET Framework 2.0 中使用未处理异常的默认策略。引发未处理的异常时，基于 ASP.NET 的应用程序将会意外退出。&lt;br /&gt;如果在请求上下文中出现异常，则不会发生上述行为。这类异常仍由 &lt;strong&gt;HttpException&lt;/strong&gt; 对象进行处理和包装。在请求上下文中出现的异常不会导致工作进程结束。但是，请求上下文之外的未处理异常（如计时器线程上或回调函数中的异常）会导致工作进程结束。&lt;/div&gt;&lt;/blockquote&gt;&lt;p&gt;&amp;nbsp; 如果嫌麻烦用已经做好的msi程序安装httpmodule：&lt;a href="http://unhandledexception.codeplex.com/"&gt;http://unhandledexception.codeplex.com/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2456930.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/19/2456930.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/12/2444440.html</id><title type="text">[转]Lucene 性能优化带数据</title><summary type="text">虽然是很久了的数据，还是有很好的参考价值的：lucene.commit.batch.size=0 lucene.commit.time.interval=0 These properties allow commits in batch, you can either set how many document changes a batch will contain (commit will happen after X docs are modified) or set a time interval in milliseconds (commit will happen ever...</summary><published>2012-04-12T10:35:00Z</published><updated>2012-04-12T10:35:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/12/2444440.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/12/2444440.html"/><content type="html">&lt;p&gt;虽然是很久了的数据，还是有很好的参考价值的：&lt;/p&gt;&lt;p&gt;&lt;div&gt;&lt;p&gt;lucene.commit.batch.size=0&lt;br /&gt; lucene.commit.time.interval=0&lt;/p&gt;  &lt;p&gt;These properties allow commits in batch, you can either set how many  document changes a batch will contain (commit will happen after X docs  are modified) or set a time interval in milliseconds (commit will happen  every X milliseconds).&lt;/p&gt;  &lt;p&gt;lucene.buffer.size=16&lt;/p&gt;  &lt;p&gt;This will call IndexWriter's setRAMBufferSizeMB method, this the max  memory in megabytes used by Lucene before flushing documents, a higher  number means less disk writes.&lt;/p&gt;  &lt;p&gt;These 3 new properties will make reindexing faster, I've made stress  tests and I could perceive a 30% improvement in certain configurations, I  also learned the following while tweaking Lucene properties:&lt;/p&gt;  &lt;p&gt;A higher lucene.buffer.size helps a lot during reindex, but there is a  up bound limit, over that limit, reindex won't get faster, for example,  setting to 32MB or 48MB gave the same results, but 32MB was much better  than the default, 16MB.&lt;/p&gt;  &lt;p&gt;It's a bad idea to set lucene.merge.factor to a very high number,  there will be much more disk write accesses and this will degrade  performance, keep it at 10.&lt;/p&gt;  &lt;p&gt;Set lucene.autocommit.documents.interval to the number of documents  you have in the index, this means only 1 commit will happen. I thought  this property would bring better performance results, but it made it  faster around 15% only.&lt;/p&gt;  &lt;p&gt;Setting a higher lucene.optimize.interval can make some improvement,  but since the reindex process also make searches, it's important that  you also optimize the index often during reindex, you need to find a  balance.&lt;/p&gt;  &lt;p&gt;My stress tests consisted in reindexing 30.000 blog entries, I tested  on a Intel Quadcore 2.66GHz, 4MB of RAM, Ubuntu 32 bits. The best  result was around 4 minutes with this configuration:&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=30000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=10000&lt;br /&gt; lucene.buffer.size=48&lt;/p&gt;  &lt;p&gt;The worst result was 7:35 minutes (not considering the one I set lucene.merge.factor to 1000):&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=0&lt;br /&gt; lucene.merge.factor=50&lt;br /&gt; lucene.optimize.interval=1000&lt;br /&gt; lucene.buffer.size=16&lt;/p&gt;  &lt;p&gt;Here are the other results:&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=0&lt;br /&gt; lucene.merge.factor=15&lt;br /&gt; lucene.optimize.interval=30000&lt;br /&gt; lucene.buffer.size=16&lt;br /&gt; 7:30 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=0&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=100&lt;br /&gt; lucene.buffer.size=16&lt;br /&gt; 7:18 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=10000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=100&lt;br /&gt; lucene.buffer.size=16&lt;br /&gt; 06:23 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=1000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=100&lt;br /&gt; lucene.buffer.size=16&lt;br /&gt; 6:00 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=30000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=100&lt;br /&gt; lucene.buffer.size=32&lt;br /&gt; 5:00 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=30000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=100&lt;br /&gt; lucene.buffer.size=48&lt;br /&gt; 5:00 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=30000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=50000&lt;br /&gt; lucene.buffer.size=48&lt;br /&gt; 5:00 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=15000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=10000&lt;br /&gt; lucene.buffer.size=48&lt;br /&gt; 5:00 minutes&lt;/p&gt;  &lt;p&gt;lucene.commit.time.interval=30000&lt;br /&gt; lucene.merge.factor=10&lt;br /&gt; lucene.optimize.interval=1000&lt;br /&gt; lucene.buffer.size=48&lt;br /&gt; 4:30 minutes&lt;/p&gt;&lt;/div&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2444440.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/12/2444440.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/08/2437188.html</id><title type="text">【转】URL短地址压缩算法 微博短地址原理解析</title><summary type="text">引自 http://www.nowamagic.net/webdesign/webdesign_ShortUrlInTwitter.php 短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn，新郎的sinaurl.cn等。 我们在QQ微博上发布网址的时候，微博会自动判别网址，并将其转换，例如：http://url.cn/2hytQx 为什么要这样做的，原因我想有这样几点： 微博限制字数为140字一条，那么如果我们需要发一些连接上去，但是这个连接非常的长，以至于将近要占用我们内容的一半篇幅，这肯定是不能被允许的，所以短网址应运而生了。 短网址可以在我们项目里可以很好的对开.</summary><published>2012-04-07T16:20:00Z</published><updated>2012-04-07T16:20:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/08/2437188.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/08/2437188.html"/><content type="html">&lt;div&gt;&lt;p&gt;&amp;nbsp;引自 &lt;a href="http://www.nowamagic.net/webdesign/webdesign_ShortUrlInTwitter.php"&gt;http://www.nowamagic.net/webdesign/webdesign_ShortUrlInTwitter.php&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn，新郎的sinaurl.cn等。&lt;/p&gt; &lt;p&gt;我们在QQ微博上发布网址的时候，微博会自动判别网址，并将其转换，例如：http://url.cn/2hytQx&lt;/p&gt; &lt;p&gt;为什么要这样做的，原因我想有这样几点：&lt;/p&gt; &lt;ol&gt;&lt;li&gt;微博限制字数为140字一条，那么如果我们需要发一些连接上去，但是这个连接非常的长，以至于将近要占用我们内容的一半篇幅，这肯定是不能被允许的，所以短网址应运而生了。 &lt;/li&gt;&lt;li&gt;短网址可以在我们项目里可以很好的对开放级URL进行管理。有一部分网址可以会涵盖色情，暴力，广告等信息，这样我们可以通过用户的举报，完全管理这个连接将不出现在我们的应用中，应为同样的URL通过加密算法之后，得到的地址是一样的。 &lt;/li&gt;&lt;li&gt;我们可以对一系列的网址进行流量，点击等统计，挖掘出大多数用户的关注点，这样有利于我们对项目的后续工作更好的作出决策。 &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;其实以上三点纯属个人观点，因为在我接下来的部分项目中会应用到，所以就了解了一下，下面先来看看短网址映射算法的理论（网上找到的资料）：&lt;/p&gt; &lt;ol&gt;&lt;li&gt;将长网址md5生成32位签名串，分为4段，每段8个字节； &lt;/li&gt;&lt;li&gt;对这四段循环处理，取8个字节，将他看成16进制串与0x3fffffff(30位1)与操作，即超过30位的忽略处理； &lt;/li&gt;&lt;li&gt;这30位分成6段，每5位的数字作为字母表的索引取得特定字符，依次进行获得6位字符串； &lt;/li&gt;&lt;li&gt;总的md5串可以获得4个6位串；取里面的任意一个就可作为这个长url的短url地址； &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;很简单的理论，我们并不一定说得到的URL是唯一的，但是我们能够取出4组URL，这样几乎不会出现太大的重复。&lt;/p&gt; &lt;p&gt;下面来看看程序部分：&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;div nogutter=""  bg_c-sharp:nogutter"=""&gt;&lt;div&gt;&lt;div&gt;&lt;strong&gt;[c-sharp:nogutter]&lt;/strong&gt; &lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="view plain"&gt;view plain&lt;/a&gt;&lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="copy"&gt;copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;ol start="1"&gt;&lt;li&gt;&lt;span&gt;public&amp;nbsp;static&amp;nbsp;string[]&amp;nbsp;ShortUrl(string&amp;nbsp;url)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;{&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//可以自定义生成MD5加密字符传前的混合KEY&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;string&amp;nbsp;key&amp;nbsp;=&amp;nbsp;"Leejor";&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//要使用生成URL的字符&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;string[]&amp;nbsp;chars&amp;nbsp;=&amp;nbsp;new&amp;nbsp;string[]{&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"a","b","c","d","e","f","g","h",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"i","j","k","l","m","n","o","p",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"q","r","s","t","u","v","w","x",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"y","z","0","1","2","3","4","5",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"6","7","8","9","A","B","C","D",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"E","F","G","H","I","J","K","L",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"M","N","O","P","Q","R","S","T",&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;"U","V","W","X","Y","Z"&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//对传入网址进行MD5加密&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;string&amp;nbsp;hex&amp;nbsp;=&amp;nbsp;System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key&amp;nbsp;+&amp;nbsp;url,&amp;nbsp;"md5");&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;string[]&amp;nbsp;resUrl&amp;nbsp;=&amp;nbsp;new&amp;nbsp;string[4];&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;for&amp;nbsp;(int&amp;nbsp;i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;4;&amp;nbsp;i++)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;int&amp;nbsp;hexint&amp;nbsp;=&amp;nbsp;0x3FFFFFFF&amp;nbsp;&amp;amp;&amp;nbsp;Convert.ToInt32("0x"&amp;nbsp;+&amp;nbsp;hex.Substring(i&amp;nbsp;*&amp;nbsp;8,&amp;nbsp;8),&amp;nbsp;16);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;string&amp;nbsp;outChars&amp;nbsp;=&amp;nbsp;string.Empty;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;for&amp;nbsp;(int&amp;nbsp;j&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;j&amp;nbsp;&amp;lt;&amp;nbsp;6;&amp;nbsp;j++)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//把得到的值与0x0000003D进行位与运算，取得字符数组chars索引&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;int&amp;nbsp;index&amp;nbsp;=&amp;nbsp;0x0000003D&amp;nbsp;&amp;amp;&amp;nbsp;hexint;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//把取得的字符相加&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outChars&amp;nbsp;+=&amp;nbsp;chars[index];&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//每次循环按位右移5位&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hexint&amp;nbsp;=&amp;nbsp;hexint&amp;nbsp;&amp;gt;&amp;gt;&amp;nbsp;5;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;//把字符串存入对应索引的输出数组&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resUrl[i]&amp;nbsp;=&amp;nbsp;outChars;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;return&amp;nbsp;resUrl;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;}&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt; &lt;p&gt;现在可以直接使用该方法，可以等到下面四组值：&lt;/p&gt; &lt;div nogutter=""  bg_java:nogutter"=""&gt;&lt;div&gt;&lt;div&gt;&lt;strong&gt;[java:nogutter]&lt;/strong&gt; &lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="view plain"&gt;view plain&lt;/a&gt;&lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="copy"&gt;copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;ol start="1"&gt;&lt;li&gt;&lt;span&gt;ShortUrl(http://www.me3.cn)[0];&amp;nbsp;&amp;nbsp;//得到值fAVfui&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;ShortUrl(http:&lt;span&gt;//www.me3.cn)[1];&amp;nbsp;&amp;nbsp;//得到值3ayQry&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;ShortUrl(http:&lt;span&gt;//www.me3.cn)[2];&amp;nbsp;&amp;nbsp;//得到值UZzyUr&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;ShortUrl(http:&lt;span&gt;//www.me3.cn)[3];&amp;nbsp;&amp;nbsp;//得到值36rQZn&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;  &lt;p&gt;在存放这个URL的数据方面，我个人推荐TTServer，有的朋友可以没有听说过，下面是这个数据库的介绍：&lt;/p&gt; &lt;p&gt;Tokyo Cabinet 是日本人 Mikio Hirabayashi（平林幹雄）のページ  开发的一款DBM数据库(注：大名鼎鼎的DBM数据库qdbm就是他开发的)，该数据库读写非常快。insert:0.4sec/1000000  recordes(2500000qps)，写入100万数据只需要0.4秒。search:0.33sec/1000000 recordes  (3000000 qps)，读取100万数据只需要0.33秒。 &lt;/p&gt; &lt;p&gt;可以看到对于字典类型的数据Key/Value的查询，这个数据库可以说是我目前见过效率非常高的，况且他如此的小巧，用来对short url/long url的配对再好不过。&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;该系统使用6个短码字符来表示任何长度的网址。 有效的字符代码是ASCII 'A'到'Z'和'0'的'5'，其中每个字符包含2 ^ 5（32）状态。&amp;nbsp; 6短码字符可用于绘制32 ^ 6（1073741824）的网址 &lt;br /&gt;&lt;br /&gt;首先，你需要一个数据库表来存储和检索你映射的网址。&amp;nbsp;&lt;/p&gt; &lt;div nogutter=""  bg_c-sharp:nogutter"=""&gt;&lt;div&gt;&lt;div&gt;&lt;strong&gt;[c-sharp:nogutter]&lt;/strong&gt; &lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="view plain"&gt;view plain&lt;/a&gt;&lt;a href="http://blog.csdn.net/lile269/article/details/6378408#" title="copy"&gt;copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;ol start="1"&gt;&lt;li&gt;&lt;span&gt;CREATE&amp;nbsp;TABLE&amp;nbsp;mappedURL&amp;nbsp;(的CREATE&amp;nbsp;TABLE&amp;nbsp;mappedURL（&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;shortCode&amp;nbsp;&lt;span&gt;char(6)&amp;nbsp;not&amp;nbsp;null,&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;lognURL&amp;nbsp;&amp;nbsp;text&amp;nbsp;not&amp;nbsp;&lt;span&gt;null,&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;PRIMARY&amp;nbsp;KEY&amp;nbsp;&amp;nbsp;shortCodeInd&amp;nbsp;(shortCode),&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;&lt;li&gt;);&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt; 其次，你需要定义一个算法将长的URL映射到短的URL。 算法上面已经介绍过了。&lt;br /&gt;&lt;br /&gt;第三，你需要创建一个网页，从数据库的短网址的映射找到原始的URL,并重定向之。&lt;/div&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2437188.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/08/2437188.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/05/2433142.html</id><title type="text">QueryParse后的查询：“中华 共和国”查不到，但是“中华人民共和国”查的到</title><summary type="text">我想被这个东西拌到的人估计不少，问题就出在QueryParse会将分词结果用语法查询PhraseQuery，而语法查询默认slop是0（关于slop请看：http://www.blogjava.net/freeman1984/archive/2011/02/25/345116.html）。泪奔，一直以为索引的问题，感谢luke：http://luke.codeplex.com/ 下面是一个分解查询设置slop为99的帮助方法 :privatevoidSetSlop(Queryq){varbQuery=qasBooleanQuery;if(bQuery!=null){foreach(varbq.</summary><published>2012-04-05T06:44:00Z</published><updated>2012-04-05T06:44:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/05/2433142.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/05/2433142.html"/><content type="html">&lt;p&gt; 我想被这个东西拌到的人估计不少，问题就出在QueryParse会将分词结果用语法查询PhraseQuery，而语法查询默认slop是0（关于slop请看：&lt;a href="http://www.blogjava.net/freeman1984/archive/2011/02/25/345116.html"&gt;http://www.blogjava.net/freeman1984/archive/2011/02/25/345116.html&lt;/a&gt;）。&lt;/p&gt;&lt;p&gt;泪奔，一直以为索引的问题，感谢luke：&lt;a href="http://luke.codeplex.com/"&gt;http://luke.codeplex.com/ &lt;/a&gt;&lt;/p&gt;&lt;p&gt;下面是一个分解查询设置slop为99的帮助方法 :&lt;br /&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&amp;nbsp;SetSlop(Query&amp;nbsp;q)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&amp;nbsp;bQuery&amp;nbsp;=&amp;nbsp;q&amp;nbsp;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&amp;nbsp;BooleanQuery;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&amp;nbsp;(bQuery&amp;nbsp;!=&amp;nbsp;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&amp;nbsp;(&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&amp;nbsp;bq&amp;nbsp;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&amp;nbsp;bQuery.Clauses())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;bq&amp;nbsp;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&amp;nbsp;BooleanClause;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;(query!=&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{SetSlop(query.GetQuery());}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;var&lt;/span&gt;&amp;nbsp;pQuery&amp;nbsp;=&amp;nbsp;q&amp;nbsp;&lt;span style="color: #0000FF;"&gt;as&lt;/span&gt;&amp;nbsp;PhraseQuery;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&amp;nbsp;(pQuery&amp;nbsp;!=&amp;nbsp;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;pQuery.SetSlop(&lt;span style="color: #800080;"&gt;99&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/div&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2433142.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/05/2433142.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/jinzhao/archive/2012/04/01/2428546.html</id><title type="text">Dot NET 内存泄漏</title><summary type="text">因为正好解决了手头一个泄漏的问题，正好做个总结。一开始我认为托管代码是不存在泄漏问题的（不使用指针，不操作unsafe的代码） ，我想跟我这样想的人应该不少，呵呵。传统的内存泄漏主要是因为使用后没有释放造成的（维基解释http://en.wikipedia.org/wiki/Memory_leak），而在托管平台下内存的回收是由gc完成，所以托管中出现内存泄漏一般都是因为gc无法分辨对象是否可以被回收造成的，而最常见的就是循环引用，另外委托也是重灾区，4.0中引入的task也成为新的增长点（插播新闻http://news.cnblogs.com/n/124603/）。（插播新闻：在我的代码中大</summary><published>2012-04-01T05:33:00Z</published><updated>2012-04-01T05:33:00Z</updated><author><name>今昭</name><uri>http://www.cnblogs.com/jinzhao/</uri></author><link rel="alternate" href="http://www.cnblogs.com/jinzhao/archive/2012/04/01/2428546.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/jinzhao/archive/2012/04/01/2428546.html"/><content type="html">&lt;p&gt;&lt;span style="font-size: 14pt;"&gt;因为正好解决了手头一个泄漏的问题，正好做个总结。一开始我认为托管代码是不存在泄漏问题的（不使用指针，不操作unsafe的代码）&lt;/span&gt; &lt;span style="font-size: 14pt;"&gt;，&lt;/span&gt;&lt;span style="font-size: 14pt;"&gt;我想跟我这样想的人应该不少，&lt;/span&gt;&lt;span style="font-size: 14pt;"&gt;呵呵。&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14pt;"&gt;传统的内存泄漏主要是因为使用后没有释放造成的（维基解释&lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Memory_leak"&gt;&lt;span style="font-size: 14pt;"&gt;http://en.wikipedia.org/wiki/Memory_leak&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size: 14pt;"&gt;），而在托管平台下内存的回收是由gc完成，所以托管中出现内存泄漏一般都是因为gc无法分辨对象是否可以被回收造成的，而最常见的就是循环引用，另外委托也是重灾区，4.0中引入的task也成为新的增长点&lt;/span&gt;（插播新闻&lt;a title=".NET 4.5中任务并行类库的改进" href="http://news.cnblogs.com/n/124603/"&gt;http://news.cnblogs.com/n/124603/&lt;/a&gt;）。&lt;br /&gt;&lt;span style="font-size: 14pt;"&gt;（插播新闻：&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size: 14pt;"&gt;在我的代码中大量使用了task，造成泄漏是因为我使用了&lt;/span&gt;&lt;span style="font-size: 14pt; color: red;"&gt;TaskCreationOptions.AttachedToParent&lt;/span&gt;&lt;span style="font-size: 14pt;"&gt;，它会保持引用关系而不被回收。&lt;/span&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/jinzhao/aggbug/2428546.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/jinzhao/archive/2012/04/01/2428546.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
