首页 \ 问答 \ Solr正则表达式搜索结果不正确(Solr regex search incorrect results)

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
更新时间:2023-12-09 13:12

最满意答案

如果您正在尝试获取数字数组的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.");
    }

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。