使用Lucene Android

时间:2016-01-02 19:34:02

标签: android regex apache lucene proximity

  

披露我是Android的初学者,所以在阅读了提示之后,我   我明智地发布了这个问题。如果我在某处错了,请原谅。我见过SO和许多其他博客的很多帖子,但除了理论,   我没有为我的代码获得任何特定的解决方案。

我正在做的项目,我的任务是让lucene工作项目从json文件中搜索数据。

现在,我需要做的是,当用户在searchView上输入内容时,用户必须使用PROXIMITY搜索示例获取依赖于输入文本的列表视图。

到目前为止我尝试过的是,

Analyzer analyzer = null;
        QueryParser queryParser = new QueryParser(string_searchText, analyzer);
        try {
            Query query = queryParser.parse("\"how today\"~10");

            Toast.makeText(getApplicationContext(), query.toString(),
                    Toast.LENGTH_LONG).show();

            Log.v("..query..", query.toString());

        } catch (org.apache.lucene.queryparser.classic.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这里,string_searchText是用户在searchView中输入的内容,并且该文本存储在此字符串中,因此可能有5-10个单词,所以现在我有2个单词的逻辑...

  

我想要有更多单词的逻辑,所以我该怎么办?

  

我应该只接近两个单词?

但我收到的错误......

01-03 00:53:21.073: D/AbsListView(26352): onVisibilityChanged() is called, visibility : 4
01-03 00:53:21.073: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:21.103: D/AbsListView(26352): Get MotionRecognitionManager
01-03 00:53:21.133: D/AbsListView(26352): onVisibilityChanged() is called, visibility : 4
01-03 00:53:21.133: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:21.143: D/AbsListView(26352): onVisibilityChanged() is called, visibility : 0
01-03 00:53:21.143: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:21.193: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:21.303: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:21.313: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:22.524: D/WritingBuddyImpl(26352): getCurrentWritingBuddyView() 
01-03 00:53:22.694: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:24.456: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:24.646: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:25.227: D/AbsListView(26352): onVisibilityChanged() is called, visibility : 0
01-03 00:53:25.227: D/AbsListView(26352): unregisterIRListener() is called 
01-03 00:53:25.397: D/AndroidRuntime(26352): Shutting down VM
01-03 00:53:25.397: W/dalvikvm(26352): threadid=1: thread exiting with uncaught exception (group=0x40e23930)
01-03 00:53:25.417: E/AndroidRuntime(26352): FATAL EXCEPTION: main
01-03 00:53:25.417: E/AndroidRuntime(26352): java.lang.NullPointerException
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.util.QueryBuilder.createFieldQuery(QueryBuilder.java:205)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParserBase.newFieldQuery(QueryParserBase.java:480)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParserBase.getFieldQuery(QueryParserBase.java:472)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParserBase.getFieldQuery(QueryParserBase.java:494)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParserBase.handleQuotedTerm(QueryParserBase.java:887)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParser.Term(QueryParser.java:452)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParser.Clause(QueryParser.java:247)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParser.Query(QueryParser.java:171)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParser.TopLevelQuery(QueryParser.java:160)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:118)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at com.abc.luceneprac.FAQs$ResultAdapter.getView(FAQs.java:574)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.AbsListView.obtainView(AbsListView.java:2604)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.ListView.makeAndAddView(ListView.java:1840)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.ListView.fillDown(ListView.java:681)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.ListView.fillFromTop(ListView.java:742)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.ListView.layoutChildren(ListView.java:1647)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.AbsListView.onLayout(AbsListView.java:2427)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.View.layout(View.java:14905)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewGroup.layout(ViewGroup.java:4601)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.View.layout(View.java:14905)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewGroup.layout(ViewGroup.java:4601)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.View.layout(View.java:14905)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewGroup.layout(ViewGroup.java:4601)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1694)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1552)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.View.layout(View.java:14905)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewGroup.layout(ViewGroup.java:4601)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.View.layout(View.java:14905)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewGroup.layout(ViewGroup.java:4601)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2213)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2027)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1237)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5164)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.Choreographer.doFrame(Choreographer.java:561)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.os.Handler.handleCallback(Handler.java:725)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.os.Looper.loop(Looper.java:176)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at android.app.ActivityThread.main(ActivityThread.java:5302)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at java.lang.reflect.Method.invokeNative(Native Method)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at java.lang.reflect.Method.invoke(Method.java:511)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-03 00:53:25.417: E/AndroidRuntime(26352):    at dalvik.system.NativeStart.main(Native Method)

在我的项目中,第574行是,

这:Query query = queryParser.parse("\"how today\"~10");

那么,我怎样才能达到理想的输出效果。

  

而且我也希望将这种接近应用于   搜索结果,列表视图必须以这种方式更新。

感谢。

更新 -

这个怎么样?我可以用这个吗?

\b(?:word1(?:\W+\w+){1,3}?\W+word2|word2(?:\W+\w+){1,3}?\W+word1)\b

...

1 个答案:

答案 0 :(得分:0)

传递到null构造函数的分析器

QueryParser不是可接受的值。您必须使用实际的分析仪。

此外,QueryParser构造函数的第一个参数不是查询,它是解析器的默认字段名称。查询以parse方法传递。