Solr正则表达式搜索结果不正确(Solr regex search incorrect results)
我正在使用Solr 4.1并尝试使用Query进行正则表达式模式。 样本数据是
56% AB, 78% DC 65% AB, 55% IJ 70% AB, 35% LJ
我正在尝试这种模式
/([1-6][0-9]% AB)/
,这是行不通的,所以我试过了/([1-6][1-9])??AB/
它显示了上述所有内容,因为当模式适用时它不使用AND运算符,并且它为每个字符应用OR运算符。 对于Eg:以下结果也会出现
77% DD, 89% FF
有没有人用Solr 4.0及以上版本试过正则表达式?
I am using Solr 4.1 and trying a regex pattern with Query. The sample data is
56% AB, 78% DC 65% AB, 55% IJ 70% AB, 35% LJ
I am trying this pattern
/([1-6][0-9]% AB)/
, this is not working so i tried/([1-6][1-9])??AB/
it shows all the above as it is not using AND operator when the pattern applies and it is applying OR operator for each character. For Eg: the below results will also appear
77% DD, 89% FF
Has anybody tried regex with Solr 4.0 and above?
原文:https://stackoverflow.com/questions/14771703
最满意答案
如果您正在尝试获取数字数组的Base64表示,那么看起来像很多代码。 我错过了目标吗?
如果你想做的就是从base64字符串中获取int或long数组,试试这个:
private static string ConvertArrayToBase64<T>(IList<T> array) where T : struct { if (typeof(T).IsPrimitive) { int size = System.Runtime.InteropServices.Marshal.SizeOf<T>(); var byteArray = new byte[array.Count * size]; Buffer.BlockCopy(array.ToArray(), 0, byteArray, 0, byteArray.Length); return Convert.ToBase64String(byteArray); } throw new InvalidOperationException("Only primitive types are supported."); } private static T[] ConvertBase64ToArray<T>(string base64String) where T : struct { if (typeof(T).IsPrimitive) { var byteArray = Convert.FromBase64String(base64String); var array = new T[byteArray.Length / System.Runtime.InteropServices.Marshal.SizeOf<T>()]; Buffer.BlockCopy(byteArray, 0, array, 0, byteArray.Length); return array; } throw new InvalidOperationException("Only primitive types are supported."); }
虽然这个代码有几件事需要考虑......
它确实是数组的完整副本,因此如果您正在处理大型数组或性能敏感操作,则可能不是最好的方法。
这应该适用于任何“原始值类型”数组,它应包括所有数字类型,如int,long,uint,float等。
要演示用法,请参阅此示例:
var longArray = new long[] { 11111, 22222, 33333, 44444 }; var intArray = new int[] { 55555, 66666, 77777, 88888}; string base64longs = ConvertArrayToBase64(longArray); Console.WriteLine(base64longs); Console.WriteLine(string.Join(", ", ConvertBase64ToArray<long>(base64longs))); string base64ints = ConvertArrayToBase64(intArray); Console.WriteLine(base64ints); Console.WriteLine(string.Join(", ", ConvertBase64ToArray<int>(base64ints)));
它能做什么:
- 验证数组是否只有基本类型。
- 确定数组中元素的大小,以计算要分配的字节数组的长度。
- 它将数组复制到字节数组。
- 返回base64表示。
互补功能则相反。
更新:这是.NET 2.0兼容版本......
private static string ConvertArrayToBase64<T>(IList<T> array) where T : struct { if (typeof(T).IsPrimitive) { int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); var byteArray = new byte[array.Count * size]; Buffer.BlockCopy((Array)array, 0, byteArray, 0, byteArray.Length); return Convert.ToBase64String(byteArray); } throw new InvalidOperationException("Only primitive types are supported."); } private static T[] ConvertBase64ToArray<T>(string base64String) where T : struct { if (typeof(T).IsPrimitive) { var byteArray = Convert.FromBase64String(base64String); var array = new T[byteArray.Length / System.Runtime.InteropServices.Marshal.SizeOf(typeof(T))]; Buffer.BlockCopy(byteArray, 0, array, 0, byteArray.Length); return array; } throw new InvalidOperationException("Only primitive types are supported."); }
Seems like a lot of code if all you are doing is trying to get a Base64 representation of a numeric array. Am I missing the goal?
If all you want to do is get int or long arrays to and from base64 strings, try this:
private static string ConvertArrayToBase64<T>(IList<T> array) where T : struct { if (typeof(T).IsPrimitive) { int size = System.Runtime.InteropServices.Marshal.SizeOf<T>(); var byteArray = new byte[array.Count * size]; Buffer.BlockCopy(array.ToArray(), 0, byteArray, 0, byteArray.Length); return Convert.ToBase64String(byteArray); } throw new InvalidOperationException("Only primitive types are supported."); } private static T[] ConvertBase64ToArray<T>(string base64String) where T : struct { if (typeof(T).IsPrimitive) { var byteArray = Convert.FromBase64String(base64String); var array = new T[byteArray.Length / System.Runtime.InteropServices.Marshal.SizeOf<T>()]; Buffer.BlockCopy(byteArray, 0, array, 0, byteArray.Length); return array; } throw new InvalidOperationException("Only primitive types are supported."); }
There are a couple things to consider with this code though...
It does make a complete copy of the array, so if you are dealing with a large array or a performance sensitive operation, it may not be the best way.
This should work with any "primitive value type" arrays, which should include all the numeric types like int, long, uint, float, etc.
To demonstrate usage, see this example:
var longArray = new long[] { 11111, 22222, 33333, 44444 }; var intArray = new int[] { 55555, 66666, 77777, 88888}; string base64longs = ConvertArrayToBase64(longArray); Console.WriteLine(base64longs); Console.WriteLine(string.Join(", ", ConvertBase64ToArray<long>(base64longs))); string base64ints = ConvertArrayToBase64(intArray); Console.WriteLine(base64ints); Console.WriteLine(string.Join(", ", ConvertBase64ToArray<int>(base64ints)));
What it does:
- Verifies that the array only has primitive types.
- Determines the size of the elements in the array to calculate the length of the byte array to allocate.
- It copies the array to the byte array.
- Returns the base64 representation.
The complementary function does the reverse.
Update: Here are the .NET 2.0 compatible versions...
private static string ConvertArrayToBase64<T>(IList<T> array) where T : struct { if (typeof(T).IsPrimitive) { int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)); var byteArray = new byte[array.Count * size]; Buffer.BlockCopy((Array)array, 0, byteArray, 0, byteArray.Length); return Convert.ToBase64String(byteArray); } throw new InvalidOperationException("Only primitive types are supported."); } private static T[] ConvertBase64ToArray<T>(string base64String) where T : struct { if (typeof(T).IsPrimitive) { var byteArray = Convert.FromBase64String(base64String); var array = new T[byteArray.Length / System.Runtime.InteropServices.Marshal.SizeOf(typeof(T))]; Buffer.BlockCopy(byteArray, 0, array, 0, byteArray.Length); return array; } throw new InvalidOperationException("Only primitive types are supported."); }
相关问答
更多-
这是一个可以与GCC 5.2和Clang 3.7“协同工作”的解决方案。 我使用FilipRoséen的Constexpr Meta-Container进行了一些小改动。 正如技术委员会指出的那样,这可能会在未来变得不合时宜 ,所以这种解决方案在生产代码中完全不合理,但现在非常酷。 我甚至不确定这是否符合100%标准。 // This is our meta-container using TypeMap = atch::meta_list
; // Get a unique integr ... -
使用java.nio命名空间中找到的java.nio ,特别是ByteBuffer 。 它可以为你做所有的工作。 byte[] arr = { 0x00, 0x01 }; ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default short num = wrapped.getShort(); // 1 ByteBuffer dbuf = ByteBuffer.allocate(2); dbuf.putShort(num); by ...
-
C#中数字数组的通用转换为字节,反之亦然(Generic conversion of number arrays into bytes and vice-versa in C#)[2024-02-15]
如果您正在尝试获取数字数组的Base64表示,那么看起来像很多代码。 我错过了目标吗? 如果你想做的就是从base64字符串中获取int或long数组,试试这个: private static string ConvertArrayToBase64(IList array) where T : struct { if (typeof(T).IsPrimitive) { int size = System.Runtime.In ... -
从二进制形式转换浮动NaN值,反之亦然,导致不匹配(Converting float NaN values from binary form and vice-versa results a mismatch)[2024-02-26]
我只是将@HansPassant评论翻译为答案。 “x86抖动使用FPU来处理浮点值。这不是一个错误。你假设那些字节值是一个接受浮点参数的方法的正确参数是错误的。” 换句话说,这只是一个GIGO案例( Garbage In,Garbage Out )。 I just translate @HansPassant comment as an answer. "The x86 jitter uses the FPU to handle floating point values. This is not a b ... -
您的代码的问题在于您假设malloc以某种方式“知道”正在分配的内容的大小,就像Java的数组new知道分配5 int和5 short s之间的区别一样。 嗯, malloc没有。 除非你另有说明,否则它会分配所需的字节数。 这就是你这样做的原因 uint16_t*shorts = (uint16_t*)malloc(size/2); 然后将size/2 uint16_t写入其中,超出缓冲区。 在C中(以及在Objective-C中,它是C的超集)分配基元数组的正确方法如下: size_t count = ...
-
我会想象有点变化和| 运营商应该是最有效的方式。 int i = (bytes[2] << 0) | (bytes[1] << 8) | (bytes[0] << 16); 另外,作为一个领导者,你放弃了最重要的字节,而不是最低有效字节; p I would imagine bit shifting and the | operator should be the most efficient way of doing this. int i = (bytes[2] << 0) | (bytes[1] << ...
-
如何将数据库大小从兆字节转换为字节或反之亦然?(How do I convert the database size from mega bytes to bytes or vice-versa?)[2023-06-02]
字节到兆字节= Bytes / (1024 * 1024) 兆字节到字节= Megabytes * (1024 * 1024.0) 一定要考虑整数除法,因此使用1024.0浮点数。 Bytes to Megabytes = Bytes / (1024 * 1024) Megabytes to Bytes = Megabytes * (1024 * 1024.0) Be sure to account for integer division, thus the 1024.0 so a floating po ... -
输入后,变量中会有换行符。 使用chomp摆脱它。 然后会出现第二个问题 - 你在输出语句中使用$fah或$cel 。 这应该是$temp变量,否则你会得到这样的错误: 在连接(。)或字符串中使用未初始化的值$ cel ... 这是更新的代码: #!/usr/bin/perl use strict; use warnings; print "Enter the temperature: "; my $temp =
; chomp($temp); print "Enter the Convers ... -
将字符串转换为整数的最小方法(反之亦然)(Smallest method of turning a string into an integer(and vice-versa))[2023-02-15]
没有错误检查,'因为有超过512B的wussies玩: #include// alternative: // #define isdigit(C) ((C) >= '0' && (C) <= '9') unsigned long myatol(const char *s) { unsigned long n = 0; while (isdigit(*s)) n = 10 * n + *s++ - '0'; return n; } gcc -O2将其编译为47个字节,但对 ... -
class Encoding支持你需要的东西,下面的例子假设你需要使用UTF8将string转换为byte[] ,反之亦然: string S = Encoding.UTF8.GetString(B); byte[] B = Encoding.UTF8.GetBytes(S); 如果您需要使用其他编码,您可以轻松更改: Encoding.Unicode Encoding.ASCII ... class Encoding supports what you need, example below assu ...