<bdo id='2NkRv'></bdo><ul id='2NkRv'></ul>
    <tfoot id='2NkRv'></tfoot>
  1. <small id='2NkRv'></small><noframes id='2NkRv'>

    1. <legend id='2NkRv'><style id='2NkRv'><dir id='2NkRv'><q id='2NkRv'></q></dir></style></legend>
    2. <i id='2NkRv'><tr id='2NkRv'><dt id='2NkRv'><q id='2NkRv'><span id='2NkRv'><b id='2NkRv'><form id='2NkRv'><ins id='2NkRv'></ins><ul id='2NkRv'></ul><sub id='2NkRv'></sub></form><legend id='2NkRv'></legend><bdo id='2NkRv'><pre id='2NkRv'><center id='2NkRv'></center></pre></bdo></b><th id='2NkRv'></th></span></q></dt></tr></i><div id='2NkRv'><tfoot id='2NkRv'></tfoot><dl id='2NkRv'><fieldset id='2NkRv'></fieldset></dl></div>
    3. 如何在 Lucene QueryParser 中指定两个字段?

      时间:2023-09-29
        <bdo id='ytgup'></bdo><ul id='ytgup'></ul>

        1. <tfoot id='ytgup'></tfoot>
            <legend id='ytgup'><style id='ytgup'><dir id='ytgup'><q id='ytgup'></q></dir></style></legend>
          • <i id='ytgup'><tr id='ytgup'><dt id='ytgup'><q id='ytgup'><span id='ytgup'><b id='ytgup'><form id='ytgup'><ins id='ytgup'></ins><ul id='ytgup'></ul><sub id='ytgup'></sub></form><legend id='ytgup'></legend><bdo id='ytgup'><pre id='ytgup'><center id='ytgup'></center></pre></bdo></b><th id='ytgup'></th></span></q></dt></tr></i><div id='ytgup'><tfoot id='ytgup'></tfoot><dl id='ytgup'><fieldset id='ytgup'></fieldset></dl></div>

                <tbody id='ytgup'></tbody>
              • <small id='ytgup'></small><noframes id='ytgup'>

                本文介绍了如何在 Lucene QueryParser 中指定两个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我阅读了 如何在 QueryParser 中合并多个字段? 但我没听懂.

                I read How to incorporate multiple fields in QueryParser? but i didn't get it.

                目前我有一个非常奇怪的结构,例如:

                At the moment i have a very strange construction like:

                parser = New QueryParser("bodytext", analyzer)
                parser2 = New QueryParser("title", analyzer)
                query = parser.Parse(strSuchbegriff)
                query2 = parser.Parse(strSuchbegriff)
                

                我能做些什么:

                parser = New QuerParser ("bodytext" , "title",analyzer)
                query =parser.Parse(strSuchbegriff) 
                

                因此解析器会在bodytext"字段和title"字段中查找搜索词.

                so the Parser looks for the searching word in the field "bodytext" an in the field "title".

                推荐答案

                有 3 种方法可以做到这一点.

                There are 3 ways to do this.

                第一种方式是手动构造查询,这就是QueryParser在内部做的.这是最有效的方法,这意味着如果您想阻止访问 QueryParser 的一些更奇特的功能,则不必解析用户输入:

                The first way is to construct a query manually, this is what QueryParser is doing internally. This is the most powerful way to do it, and means that you don't have to parse the user input if you want to prevent access to some of the more exotic features of QueryParser:

                IndexReader reader = IndexReader.Open("<lucene dir>");
                Searcher searcher = new IndexSearcher(reader);
                
                BooleanQuery booleanQuery = new BooleanQuery();
                Query query1 = new TermQuery(new Term("bodytext", "<text>"));
                Query query2 = new TermQuery(new Term("title", "<text>"));
                booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
                booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
                // Use BooleanClause.Occur.MUST instead of BooleanClause.Occur.SHOULD
                // for AND queries
                Hits hits = searcher.Search(booleanQuery);
                

                第二种方法是使用MultiFieldQueryParser,它的行为类似于QueryParser,允许访问它拥有的所有功能,除了它会搜索多个字段.

                The second way is to use MultiFieldQueryParser, this behaves like QueryParser, allowing access to all the power that it has, except that it will search over multiple fields.

                IndexReader reader = IndexReader.Open("<lucene dir>");
                Searcher searcher = new IndexSearcher(reader);
                
                Analyzer analyzer = new StandardAnalyzer();
                MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                                                        new string[] {"bodytext", "title"},
                                                        analyzer);
                
                Hits hits = searcher.Search(queryParser.parse("<text>"));
                

                最后一种方式是使用QueryParser的特殊语法看这里.

                The final way is to use the special syntax of QueryParser see here.

                IndexReader reader = IndexReader.Open("<lucene dir>");
                Searcher searcher = new IndexSearcher(reader);    
                
                Analyzer analyzer = new StandardAnalyzer();
                QueryParser queryParser = new QueryParser("<default field>", analyzer);
                // <default field> is the field that QueryParser will search if you don't 
                // prefix it with a field.
                string special = "bodytext:" + text + " OR title:" + text;
                
                Hits hits = searcher.Search(queryParser.parse(special));
                

                您的另一种选择是在索引内容时创建一个名为 bodytextandtitle 的新字段,您可以将 bodytext 和标题的内容放入其中,然后您只需搜索一个字段.

                Your other option is to create new field when you index your content called bodytextandtitle, into which you can place the contents of both bodytext and title, then you only have to search one field.

                这篇关于如何在 Lucene QueryParser 中指定两个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我正在尝试通过 SOLR 和 lucene 对文档中的文件进行 下一篇:你如何在 .net 上运行 Lucene?

                相关文章

                最新文章

                  <bdo id='qeTsp'></bdo><ul id='qeTsp'></ul>
              • <i id='qeTsp'><tr id='qeTsp'><dt id='qeTsp'><q id='qeTsp'><span id='qeTsp'><b id='qeTsp'><form id='qeTsp'><ins id='qeTsp'></ins><ul id='qeTsp'></ul><sub id='qeTsp'></sub></form><legend id='qeTsp'></legend><bdo id='qeTsp'><pre id='qeTsp'><center id='qeTsp'></center></pre></bdo></b><th id='qeTsp'></th></span></q></dt></tr></i><div id='qeTsp'><tfoot id='qeTsp'></tfoot><dl id='qeTsp'><fieldset id='qeTsp'></fieldset></dl></div>
                <tfoot id='qeTsp'></tfoot>

                1. <small id='qeTsp'></small><noframes id='qeTsp'>

                  1. <legend id='qeTsp'><style id='qeTsp'><dir id='qeTsp'><q id='qeTsp'></q></dir></style></legend>