Lucene多字段搜索

http://matieli.iteye.com/blog/1211464

Lucene多字段搜索

博客分类:
最近在学习Lucene的过程中遇到了需要多域搜索并排序的问题,在网上找了找,资料不是很多,现在都列出来,又需要的可以自己认真看看,都是从其他网站粘贴过来的,所以比较乱,感谢原创的作者们!
    使用MultiFieldQueryParser类即可。

示例代码:
Java代码 复制代码 收藏代码
  1. package com.lucene.search;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException; 54com.cn   
  5.   
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  7. import org.apache.lucene.queryParser.MultiFieldQueryParser;   
  8. import org.apache.lucene.search.BooleanClause;   
  9. import org.apache.lucene.search.Hits;   
  10. import org.apache.lucene.search.IndexSearcher;   
  11. import org.apache.lucene.search.Query;   
  12. import org.apache.lucene.store.Directory;   
  13. import org.apache.lucene.store.FSDirectory;   
  14.   
  15. public class Searcher {   
  16.   
  17. feedom.net   
  18.     public static void main(String[] args) throws Exception {   
  19.         File indexDir = new File("C:\target\index\book");   
  20.         if (!indexDir.exists() || !indexDir.isDirectory()) {   
  21.             throw new IOException();   
  22.         }   
  23.         search(indexDir);   
  24.     }   
  25.   
  26.     public static void search(File indexDir) throws Exception {   
  27.   
  28.         Directory fsDir = FSDirectory.getDirectory(indexDir);   
  29.         IndexSearcher searcher = new IndexSearcher(fsDir);   
  30.   
  31.         String[] queries = { "中文版", "8*" };   
  32.         String[] fields = { "name", "isbn" };   
  33.         BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };   
  34.         Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());   
  35.   
  36.         Hits hits = searcher.search(query);   
  37.         System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");   
  38.         for (int i = 0; i < hits.length(); i++) {   
  39.             int DocId = hits.id(i);   
  40.             String DocName = hits.doc(i).get("name");   
  41.             String DocIsbn = hits.doc(i).get("isbn");   
  42.             String DocPblDt = hits.doc(i).get("pbl_dt");   
  43.             System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);   
  44.         }   
  45.     }   
  46. }  
package com.lucene.search;

import java.io.File;
import java.io.IOException; 54com.cn

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {

feedom.net
    public static void main(String[] args) throws Exception {
        File indexDir = new File("C:\target\index\book");
        if (!indexDir.exists() || !indexDir.isDirectory()) {
            throw new IOException();
        }
        search(indexDir);
    }

    public static void search(File indexDir) throws Exception {

        Directory fsDir = FSDirectory.getDirectory(indexDir);
        IndexSearcher searcher = new IndexSearcher(fsDir);

        String[] queries = { "中文版", "8*" };
        String[] fields = { "name", "isbn" };
        BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
        Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());

        Hits hits = searcher.search(query);
        System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
        for (int i = 0; i < hits.length(); i++) {
            int DocId = hits.id(i);
            String DocName = hits.doc(i).get("name");
            String DocIsbn = hits.doc(i).get("isbn");
            String DocPblDt = hits.doc(i).get("pbl_dt");
            System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
        }
    }
}


Java代码 复制代码 收藏代码
  1. package com.lucene.search;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  7. import org.apache.lucene.queryParser.MultiFieldQueryParser;   
  8. import org.apache.lucene.search.BooleanClause;   
  9. import org.apache.lucene.search.Hits;   
  10. import org.apache.lucene.search.IndexSearcher;   
  11. import org.apache.lucene.search.Query;   
  12. import org.apache.lucene.store.Directory;   
  13. import org.apache.lucene.store.FSDirectory;   
  14.   
  15. public class Searcher {   
  16.   
  17.     public static void main(String[] args) throws Exception {   
  18.         File indexDir = new File("C:\target\index\book");   
  19.         if (!indexDir.exists() || !indexDir.isDirectory()) {   
  20.             throw new IOException();   
  21.         }   
  22.         search(indexDir);   
  23.     }   
  24.   
  25.     public static void search(File indexDir) throws Exception {   
  26.   
  27.         Directory fsDir = FSDirectory.getDirectory(indexDir);   
  28.         IndexSearcher searcher = new IndexSearcher(fsDir);   
  29.   
  30.    String[] queries = { "中文版", "8*" };   
  31.    String[] fields = { "name", "isbn" };   
  32.    BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };   
  33.         Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());   
  34.   
  35.         Hits hits = searcher.search(query);   
  36.         System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");   
  37.         for (int i = 0; i < hits.length(); i++) {   
  38.             int DocId = hits.id(i);   
  39.             String DocName = hits.doc(i).get("name");   
  40.             String DocIsbn = hits.doc(i).get("isbn");   
  41.             String DocPblDt = hits.doc(i).get("pbl_dt");   
  42.             System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);   
  43.         }   
  44.     }   
  45. }  
package com.lucene.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {

    public static void main(String[] args) throws Exception {
        File indexDir = new File("C:\target\index\book");
        if (!indexDir.exists() || !indexDir.isDirectory()) {
            throw new IOException();
        }
        search(indexDir);
    }

    public static void search(File indexDir) throws Exception {

        Directory fsDir = FSDirectory.getDirectory(indexDir);
        IndexSearcher searcher = new IndexSearcher(fsDir);

   String[] queries = { "中文版", "8*" };
   String[] fields = { "name", "isbn" };
   BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
        Query query = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer());

        Hits hits = searcher.search(query);
        System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length() + "条");
        for (int i = 0; i < hits.length(); i++) {
            int DocId = hits.id(i);
            String DocName = hits.doc(i).get("name");
            String DocIsbn = hits.doc(i).get("isbn");
            String DocPblDt = hits.doc(i).get("pbl_dt");
            System.out.println(DocId + ":" + DocName + " ISBN:" + DocIsbn + " PBLDT:" + DocPblDt);
        }
    }
}

注意:BooleanClause.Occur[]数组,它表示多个条件之间的关系:

BooleanClause.Occur.MUST表示and, feedom.net

BooleanClause.Occur.MUST_NOT表示not, 54com.cn

BooleanClause.Occur.SHOULD表示or.

---------------------------------------------------------------------------------------------------------
多个关键字直接的关系是或,所以直接使用多域搜索对象查询出来的结果就是这样。
更灵活的控制方式为:

Java代码 复制代码 收藏代码
  1. BooleanQuery booleanQuery = new BooleanQuery();    
  2.   
  3. QueryParser parser = new QueryParser("title",分词器);    
  4. Query titleQuery = parser .parser("中国人民共和国");   
  5. booleanQuery.add(titleQuery,....SHOULD);   
  6.   
  7. QueryParser parser = new QueryParser("content",分词器);    
  8. Query contentQuery = parser .parser("中国人民共和国");   
  9. booleanQuery.add(contentQuery ,....SHOULD);   
BooleanQuery booleanQuery = new BooleanQuery(); 

QueryParser parser = new QueryParser("title",分词器); 
Query titleQuery = parser .parser("中国人民共和国");
booleanQuery.add(titleQuery,....SHOULD);

QueryParser parser = new QueryParser("content",分词器); 
Query contentQuery = parser .parser("中国人民共和国");
booleanQuery.add(contentQuery ,....SHOULD); 

--------------------------------------------------------------------------------------------------
Java代码 复制代码 收藏代码
  1. package com.lucene.search;   
  2. import org.apache.lucene.analysis.standard.StandardAnalyzer;    
  3. import org.apache.lucene.document.Document;    
  4. import org.apache.lucene.document.Field;   
  5. import org.apache.lucene.index.IndexWriter;   
  6. import org.apache.lucene.queryParser.MultiFieldQueryParser;   
  7. import org.apache.lucene.search.BooleanClause;   
  8. import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher;    
  9. import org.apache.lucene.search.MultiSearcher;    
  10. import org.apache.lucene.search.Query;    
  11. public class Multisearcher {    
  12. private static String INDEX_STORE_PATH1 = "C:\multi\1"; private static String INDEX_STORE_PATH2 = "C:\multi\2";    
  13. public static void main(String[] args) throws Exception {    
  14. Multisearcher.multisearcher();    
  15. }    
  16. public static void multisearcher() throws Exception {    
  17. IndexWriter writer = new IndexWriter(INDEX_STORE_PATH1, new StandardAnalyzer(), true);    
  18. writer.setUseCompoundFile(false);    
  19. Document doc1 = new Document();    
  20. Field f1 = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, Field.Index.TOKENIZED);    
  21. Field f11 = new Field("price", "20.5", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  22. doc1.add(f1); doc1.add(f11);    
  23. Document doc2 = new Document();    
  24. Field f2 = new Field("bookname", "钢铁战士", Field.Store.YES, Field.Index.TOKENIZED);    
  25. Field f22 = new Field("price", "18.4", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  26. doc2.add(f2);    
  27. doc2.add(f22);    
  28. Document doc3 = new Document();    
  29. Field f3 = new Field("bookname", "钢和铁是两种不同的元素", Field.Store.YES, Field.Index.TOKENIZED);    
  30. Field f33 = new Field("price", "7.6", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  31. doc3.add(f3);    
  32. doc3.add(f33);    
  33. writer.addDocument(doc1);    
  34. writer.addDocument(doc2);    
  35. writer.addDocument(doc3);    
  36. writer.close(); //创建第二个索引器;    
  37. IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH2, new StandardAnalyzer(), true);    
  38. writer2.setUseCompoundFile(false);    
  39. Document doc4 = new Document();    
  40. Field f4 = new Field("bookname", "钢要比铁有更多的元素", Field.Store.YES, Field.Index.TOKENIZED);    
  41. Field f44 = new Field("price", "22.5", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  42. doc4.add(f4); doc4.add(f44);    
  43. Document doc5 = new Document();    
  44. Field f5 = new Field("bookname", "钢和铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);    
  45. Field f55 = new Field("price", "15.9", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  46. doc5.add(f5); doc5.add(f55); Document doc6 = new Document();    
  47. Field f6 = new Field("bookname", "钢铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED);    
  48. Field f66 = new Field("price", "19.00", Field.Store.YES, Field.Index.UN_TOKENIZED);    
  49. doc6.add(f6);    
  50. doc6.add(f66);    
  51. writer2.addDocument(doc4);    
  52. writer2.addDocument(doc5);    
  53. writer2.addDocument(doc6);    
  54. writer2.close();    
  55. String query1 = "钢";    
  56. String query2 = "[10 TO 20]";//注意格式:中括号还有关键字TO是大写的    
  57. String[] queries = { query1, query2 }; //指定两个域   
  58. Field String field1 = "bookname";    
  59. String field2 = "price";    
  60. String[] fields = { field1, field2 }; //指定查询字句之间的关系    
  61. BooleanClause.Occur[] clauses = {    
  62. BooleanClause.Occur.MUST, BooleanClause.Occur.MUST    
  63. }; //转成多域查询   
  64. MultiFieldQuery Query q = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer()); //打印Query的内容 System.out.println(q.toString()); //创建两个IndexSearcher,以实现在多个索引目录进行查询    
  65. IndexSearcher searcher1 = new IndexSearcher(INDEX_STORE_PATH1);    
  66. IndexSearcher searcher2 = new IndexSearcher(INDEX_STORE_PATH2);    
  67. IndexSearcher[] searchers = { searcher1, searcher2 }; //使用MultiSearcher进行多域搜索    
  68. MultiSearcher searcher = new MultiSearcher(searchers);    
  69. Hits hits = searcher.search(q);    
  70. for (int i = 0; i < hits.length(); i++) {    
  71. System.out.println(hits.doc(i));    
  72. }    
  73. }   
  74. }  
package com.lucene.search;
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.MultiSearcher; 
import org.apache.lucene.search.Query; 
public class Multisearcher { 
private static String INDEX_STORE_PATH1 = "C:\multi\1"; private static String INDEX_STORE_PATH2 = "C:\multi\2"; 
public static void main(String[] args) throws Exception { 
Multisearcher.multisearcher(); 
} 
public static void multisearcher() throws Exception { 
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH1, new StandardAnalyzer(), true); 
writer.setUseCompoundFile(false); 
Document doc1 = new Document(); 
Field f1 = new Field("bookname", "钢铁是怎样炼成的", Field.Store.YES, Field.Index.TOKENIZED); 
Field f11 = new Field("price", "20.5", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc1.add(f1); doc1.add(f11); 
Document doc2 = new Document(); 
Field f2 = new Field("bookname", "钢铁战士", Field.Store.YES, Field.Index.TOKENIZED); 
Field f22 = new Field("price", "18.4", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc2.add(f2); 
doc2.add(f22); 
Document doc3 = new Document(); 
Field f3 = new Field("bookname", "钢和铁是两种不同的元素", Field.Store.YES, Field.Index.TOKENIZED); 
Field f33 = new Field("price", "7.6", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc3.add(f3); 
doc3.add(f33); 
writer.addDocument(doc1); 
writer.addDocument(doc2); 
writer.addDocument(doc3); 
writer.close(); //创建第二个索引器; 
IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH2, new StandardAnalyzer(), true); 
writer2.setUseCompoundFile(false); 
Document doc4 = new Document(); 
Field f4 = new Field("bookname", "钢要比铁有更多的元素", Field.Store.YES, Field.Index.TOKENIZED); 
Field f44 = new Field("price", "22.5", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc4.add(f4); doc4.add(f44); 
Document doc5 = new Document(); 
Field f5 = new Field("bookname", "钢和铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED); 
Field f55 = new Field("price", "15.9", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc5.add(f5); doc5.add(f55); Document doc6 = new Document(); 
Field f6 = new Field("bookname", "钢铁是两种重要的金属", Field.Store.YES, Field.Index.TOKENIZED); 
Field f66 = new Field("price", "19.00", Field.Store.YES, Field.Index.UN_TOKENIZED); 
doc6.add(f6); 
doc6.add(f66); 
writer2.addDocument(doc4); 
writer2.addDocument(doc5); 
writer2.addDocument(doc6); 
writer2.close(); 
String query1 = "钢"; 
String query2 = "[10 TO 20]";//注意格式:中括号还有关键字TO是大写的 
String[] queries = { query1, query2 }; //指定两个域
Field String field1 = "bookname"; 
String field2 = "price"; 
String[] fields = { field1, field2 }; //指定查询字句之间的关系 
BooleanClause.Occur[] clauses = { 
BooleanClause.Occur.MUST, BooleanClause.Occur.MUST 
}; //转成多域查询
MultiFieldQuery Query q = MultiFieldQueryParser.parse(queries, fields, clauses, new StandardAnalyzer()); //打印Query的内容 System.out.println(q.toString()); //创建两个IndexSearcher,以实现在多个索引目录进行查询 
IndexSearcher searcher1 = new IndexSearcher(INDEX_STORE_PATH1); 
IndexSearcher searcher2 = new IndexSearcher(INDEX_STORE_PATH2); 
IndexSearcher[] searchers = { searcher1, searcher2 }; //使用MultiSearcher进行多域搜索 
MultiSearcher searcher = new MultiSearcher(searchers); 
Hits hits = searcher.search(q); 
for (int i = 0; i < hits.length(); i++) { 
System.out.println(hits.doc(i)); 
} 
}
}

------------------------------------------------------------------------------------------------------------------------------------------
默认情况下,IndexSearcher类的search方法返回查询结果时,是按文档的分值排序的,可以使用重载的search方法对结果排序

IndexSearcher.search(Query,Sort);

new Sort() 和 Sort.RELEVANCE,以及null一样,采用默认排序,要定义排序字段,方法是将字段传入Sort对象
Java代码 复制代码 收藏代码
  1. Sort sort = new Sort(String field);  
Sort sort = new Sort(String field);


也可以对多个字段排序
Java代码 复制代码 收藏代码
  1. Sort sort = new Sort(String[] fields);  
Sort sort = new Sort(String[] fields);


例:

Java代码 复制代码 收藏代码
  1. Sort sort = new Sort(new SortField[]{new SortField(“title”),new SortField(“name”)});   
  2.   
  3. Hits hits=searcher.search(query,Sort);  
Sort sort = new Sort(new SortField[]{new SortField(“title”),new SortField(“name”)});

Hits hits=searcher.search(query,Sort);


多字段查找MultiFieldQueryParser

只在某些Term中查找,不关心在哪个字段

Java代码 复制代码 收藏代码
  1. Query query = new MultiFieldQueryParser.parse(“word”,new String[]{“title”,”content”},analyzer);  
Query query = new MultiFieldQueryParser.parse(“word”,new String[]{“title”,”content”},analyzer);
//在title和content中找word

多字段时默认是OR关系,要改变它,使用以下方法:

Java代码 复制代码 收藏代码
  1. Query query = MultiFieldQueryParser.parse(“word”,new String[]{“title”,”content”},new int[]{MultiFieldQueryParser.REQUIRED_FIELD,MultiFieldQueryParser.PROHIBITED_FIELD},analyzer);  
Query query = MultiFieldQueryParser.parse(“word”,new String[]{“title”,”content”},new int[]{MultiFieldQueryParser.REQUIRED_FIELD,MultiFieldQueryParser.PROHIBITED_FIELD},analyzer);


其中:

REQUIRED_FIELD 表示该条件必须有

PROHIBITED_FIELD 表示必须不含

搜索多个索引文件MultiSearcher

1)       建立多个索引:使用不同的索引目录,实例化不同的IndexWriter

2)       建立多索引搜索器:

Searcher[] searchers = new SEARCHER[2];

Searchers[0] = new IndexSearcher(dir1); //搜索索引目录一

Searchers[1]= new IndexSearcher(dir2);//搜索索引目录二

Searcher searcher = new MultiSearcher(serarchers);

3) 开始查询:Hits hits = searcher.search(query);
---------------------------------------------------------------------------------------------------------------------------------------
Java代码 复制代码 收藏代码
  1. BooleanQuery typeNegativeSearch = new BooleanQuery();   
  2. QueryParser parser = new QueryParser("contents", new Analyzer());   
  3.                 parser.setDefaultOperator(QueryParser.AND_OPERATOR);   
  4.                 query = parser.parse(queryString);   
  5.                 QueryParser parser2 = new QueryParser("adISELL", new Analyzer());     
  6.   
  7.                
  8.                 query2 = parser2.parse(""2"");      
  9. QueryParser parser3 = new QueryParser("adISELL", new Analyzer());                  
  10.                 query3 = parser3.parse(""2"");                 
  11. QueryParser parser4 = new QueryParser("adISELL", new Analyzer());                  
  12.                 query4 = parser4.parse(""2"");                 
  13. QueryParser parser4 = new QueryParser("adISELL", new Analyzer());                  
  14.                 query4 = parser4.parse(""2"");      
  15. 。。。。   
  16.      QueryParser parser..n = new QueryParser("adISELL", new Analyzer());              
  17.   
  18.       
  19.                 query..n = parser..n.parse(""2"");      
  20.                     
  21.                 typeNegativeSearch.add(query,Occur.MUST);   
  22.                 typeNegativeSearch.add(query2,Occur.MUST);   
  23. typeNegativeSearch.add(query3,Occur.MUST);   
  24.                 typeNegativeSearch.add(query4,Occur.MUST);   
  25. .....   
  26. typeNegativeSearch.add(query..n,Occur.MUST);   
  27.   
  28. hits = searcher.search(typeNegativeSearch);  
BooleanQuery typeNegativeSearch = new BooleanQuery();
QueryParser parser = new QueryParser("contents", new Analyzer());
                parser.setDefaultOperator(QueryParser.AND_OPERATOR);
                query = parser.parse(queryString);
                QueryParser parser2 = new QueryParser("adISELL", new Analyzer());  

            
                query2 = parser2.parse(""2"");   
QueryParser parser3 = new QueryParser("adISELL", new Analyzer());               
                query3 = parser3.parse(""2"");              
QueryParser parser4 = new QueryParser("adISELL", new Analyzer());               
                query4 = parser4.parse(""2"");              
QueryParser parser4 = new QueryParser("adISELL", new Analyzer());               
                query4 = parser4.parse(""2"");   
。。。。
     QueryParser parser..n = new QueryParser("adISELL", new Analyzer());           

   
                query..n = parser..n.parse(""2"");   
                 
                typeNegativeSearch.add(query,Occur.MUST);
                typeNegativeSearch.add(query2,Occur.MUST);
typeNegativeSearch.add(query3,Occur.MUST);
                typeNegativeSearch.add(query4,Occur.MUST);
.....
typeNegativeSearch.add(query..n,Occur.MUST);

hits = searcher.search(typeNegativeSearch);


1, 几种span的querySpanTermQuery:检索效果完全同TermQuery,但内部会记录一些位置信息

,供SpanQuery的其它API使用,是其它属于SpanQuery的Query的基础。
SpanFirstQuery:查找方式为从Field的内容起始位置开始,在一个固定的宽度内查找所指定的

词条。
SpanNearQuery:功能类似PharaseQuery。SpanNearQuery查找所匹配的不一定是短语,还有可

能是另一个SpanQuery的查询结果作为整体考虑,进行嵌套查询。
SpanOrQuery:把所有SpanQuery查询结果综合起来,作为检索结果。
SpanNotQuery:从第一个SpanQuery查询结果中,去掉第二个SpanQuery查询结果,作为检索结

果。

2, 多条件索引关系

BooleanClause用于表示布尔查询子句关系的类,包括:BooleanClause.Occur.MUST,

BooleanClause.Occur.MUST_NOT,BooleanClause.Occur.SHOULD。有以下6种组合:
1.MUST和MUST:取得连个查询子句的交集。
2.MUST和MUST_NOT:表示查询结果中不能包含MUST_NOT所对应得查询子句的检索结果。
3.MUST_NOT和MUST_NOT:无意义,检索无结果。
4.SHOULD与MUST、SHOULD与MUST_NOT:SHOULD与MUST连用时,无意义,结果为MUST子句的检索

结果。与MUST_NOT连用时,功能同MUST。
5.SHOULD与SHOULD:表示“或”关系,最终检索结果为所有检索子句的并集。
原文地址:https://www.cnblogs.com/cy163/p/4008833.html