• <bdo id='gqKEv'></bdo><ul id='gqKEv'></ul>

    <small id='gqKEv'></small><noframes id='gqKEv'>

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

    1. <tfoot id='gqKEv'></tfoot>
    2. <legend id='gqKEv'><style id='gqKEv'><dir id='gqKEv'><q id='gqKEv'></q></dir></style></legend>

        lucene - 越接近标题的开头赋予更多的权重

        lucene - give more weight the closer term is to beginning of title(lucene - 越接近标题的开头赋予更多的权重)

          <small id='rYPM7'></small><noframes id='rYPM7'>

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

                <bdo id='rYPM7'></bdo><ul id='rYPM7'></ul>
                  本文介绍了lucene - 越接近标题的开头赋予更多的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我了解如何在索引时或查询时提升字段.但是,如何提高匹配靠近标题开头的术语的分数?

                  I understand how to boost fields either at index time or query time. However, how could I increase the score of matching a term closer to the beginning of a title?

                  例子:

                  Query = "lucene"
                  
                  Doc1 title = "Lucene: Homepage"
                  Doc2 title = "I have a question about lucene?"
                  

                  我希望第一个文档得分更高,因为lucene"更接近开头(暂时忽略词频).

                  I would like the first document to score higher since "lucene" is closer to the beginning (ignoring term freq for now).

                  我了解如何使用 SpanQuery 指定术语之间的接近度,但我不确定如何使用有关字段中位置的信息.

                  I see how to use the SpanQuery for specifying the proximity between terms, but I'm not sure how to use the information about the position in the field.

                  我在 Java 中使用 Lucene 4.1.

                  I am using Lucene 4.1 in Java.

                  推荐答案

                  我会使用 SpanFirstQuery,匹配字段开头附近的术语.作为所有跨度查询,它依赖于位置,在 lucene 中进行索引时默认启用.

                  I would make use of a SpanFirstQuery, which matches terms near the beginning of a field. As all span queries it relies on positions, enabled by default while indexing in lucene.

                  让我们独立测试一下:你只需要提供你的 SpanTermQuery 以及可以找到该术语的最大位置(在我的示例中为一个).

                  Let's test it independently: you just have to provide your SpanTermQuery and the maximum position where the term can be found (one in my example).

                  SpanTermQuery spanTermQuery = new SpanTermQuery(new Term("title", "lucene"));
                  SpanFirstQuery spanFirstQuery = new SpanFirstQuery(spanTermQuery, 1);
                  

                  鉴于您的两个文档,如果您使用 StandardAnalyzer 进行分析,此查询将只找到标题为Lucene: Homepage"的第一个.

                  Given your two documents this query will find only the first one with title "Lucene: Homepage", if you analyzed it with the StandardAnalyzer.

                  现在我们可以以某种方式将上面的 SpanFirstQuery 与普通文本查询结合起来,让第一个查询只影响分数.您可以使用 BooleanQuery 轻松做到这一点 并将 span 查询作为 should 子句,如下所示:

                  Now we can somehow combine the above SpanFirstQuery with a normal text query, and have the first one only influencing the score. You can easily do it using a BooleanQuery and putting the span query as a should clause like this:

                  Term term = new Term("title", "lucene");
                  TermQuery termQuery = new TermQuery(term);
                  SpanFirstQuery spanFirstQuery = new SpanFirstQuery(new SpanTermQuery(term), 1);
                  BooleanQuery booleanQuery = new BooleanQuery();
                  booleanQuery.add(new BooleanClause(termQuery, BooleanClause.Occur.MUST));
                  booleanQuery.add(new BooleanClause(spanFirstQuery, BooleanClause.Occur.SHOULD));
                  

                  可能有不同的方法可以实现相同的目标,也可能使用 CustomScoreQuery 或自定义代码来实现评分,但在我看来这是最简单的方法.

                  There are probably different ways to achieve the same, maybe using a CustomScoreQuery too, or custom code to implement the scoring, but this seems to me the easiest one.

                  我用来测试它的代码打印以下输出(包括分数)首先执行唯一的 TermQuery,然后是唯一的 SpanFirstQuery,最后是组合的 BooleanQuery:

                  The code I used to test it prints the following output (score included) executing the only TermQuery first, then the only SpanFirstQuery and finally the combined BooleanQuery:

                  ------ TermQuery --------
                  Total hits: 2
                  title: I have a question about lucene - score: 0.26010898
                  title: Lucene: I have a really hard question about it - score: 0.22295055
                  ------ SpanFirstQuery --------
                  Total hits: 1
                  title: Lucene: I have a really hard question about it - score: 0.15764984
                  ------ BooleanQuery: TermQuery (MUST) + SpanFirstQuery (SHOULD) --------
                  Total hits: 2
                  title: Lucene: I have a really hard question about it - score: 0.26912516
                  title: I have a question about lucene - score: 0.09196242
                  

                  完整代码如下:

                  public static void main(String[] args) throws Exception {
                  
                          Directory directory = FSDirectory.open(new File("data"));
                  
                          index(directory);
                  
                          IndexReader indexReader = DirectoryReader.open(directory);
                          IndexSearcher indexSearcher = new IndexSearcher(indexReader);
                  
                          Term term = new Term("title", "lucene");
                  
                          System.out.println("------ TermQuery --------");
                          TermQuery termQuery = new TermQuery(term);
                          search(indexSearcher, termQuery);
                  
                          System.out.println("------ SpanFirstQuery --------");
                          SpanFirstQuery spanFirstQuery = new SpanFirstQuery(new SpanTermQuery(term), 1);
                          search(indexSearcher, spanFirstQuery);
                  
                          System.out.println("------ BooleanQuery: TermQuery (MUST) + SpanFirstQuery (SHOULD) --------");
                          BooleanQuery booleanQuery = new BooleanQuery();
                          booleanQuery.add(new BooleanClause(termQuery, BooleanClause.Occur.MUST));
                          booleanQuery.add(new BooleanClause(spanFirstQuery, BooleanClause.Occur.SHOULD));
                          search(indexSearcher, booleanQuery);
                      }
                  
                      private static void index(Directory directory) throws Exception {
                          IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_41, new StandardAnalyzer(Version.LUCENE_41));
                  
                          IndexWriter writer = new IndexWriter(directory, config);
                  
                          FieldType titleFieldType = new FieldType();
                          titleFieldType.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
                          titleFieldType.setIndexed(true);
                          titleFieldType.setStored(true);
                  
                          Document document = new Document();
                          document.add(new Field("title","I have a question about lucene", titleFieldType));
                          writer.addDocument(document);
                  
                          document = new Document();
                          document.add(new Field("title","Lucene: I have a really hard question about it", titleFieldType));
                          writer.addDocument(document);
                  
                          writer.close();
                      }
                  
                      private static void search(IndexSearcher indexSearcher, Query query) throws Exception {
                          TopDocs topDocs = indexSearcher.search(query, 10);
                  
                          System.out.println("Total hits: " + topDocs.totalHits);
                  
                          for (ScoreDoc hit : topDocs.scoreDocs) {
                              Document result = indexSearcher.doc(hit.doc);
                              for (IndexableField field : result) {
                                  System.out.println(field.name() + ": " + field.stringValue() +  " - score: " + hit.score);
                              }
                          }
                      }
                  

                  这篇关于lucene - 越接近标题的开头赋予更多的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Lucene Porter Stemmer not public(Lucene Porter Stemmer 未公开)
                  How to index pdf, ppt, xl files in lucene (java based or python or php any of these is fine)?(如何在 lucene 中索引 pdf、ppt、xl 文件(基于 java 或 python 或 php 中的任何一个都可以)?)
                  KeywordAnalyzer and LowerCaseFilter/LowerCaseTokenizer(KeywordAnalyzer 和 LowerCaseFilter/LowerCaseTokenizer)
                  How to search between dates (Hibernate Search)?(如何在日期之间搜索(休眠搜索)?)
                  How to get positions from a document term vector in Lucene?(如何从 Lucene 中的文档术语向量中获取位置?)
                  Java Lucene 4.5 how to search by case insensitive(Java Lucene 4.5如何按不区分大小写进行搜索)

                      <bdo id='nbNCo'></bdo><ul id='nbNCo'></ul>

                        1. <i id='nbNCo'><tr id='nbNCo'><dt id='nbNCo'><q id='nbNCo'><span id='nbNCo'><b id='nbNCo'><form id='nbNCo'><ins id='nbNCo'></ins><ul id='nbNCo'></ul><sub id='nbNCo'></sub></form><legend id='nbNCo'></legend><bdo id='nbNCo'><pre id='nbNCo'><center id='nbNCo'></center></pre></bdo></b><th id='nbNCo'></th></span></q></dt></tr></i><div id='nbNCo'><tfoot id='nbNCo'></tfoot><dl id='nbNCo'><fieldset id='nbNCo'></fieldset></dl></div>
                        2. <small id='nbNCo'></small><noframes id='nbNCo'>

                        3. <tfoot id='nbNCo'></tfoot>

                            <legend id='nbNCo'><style id='nbNCo'><dir id='nbNCo'><q id='nbNCo'></q></dir></style></legend>

                              <tbody id='nbNCo'></tbody>