转自:http://blog.csdn.net/huaishu/article/details/8543236
本文介绍lucene区分大小的原因,和解决方案.关于lucene大小写敏感问题我总结一下:
1.对于分词的Field且使用了StandardAnalyzer等分析器进行索引,同时利用StandardAnalyzer进行搜索时,lucene不区分大小写.
2.对于不分词的Field是区分大小写的.
一.分词和不分词
为了能使Field字段参与搜索,那么该Field就必须被索引.Field的Index类型必须是:(ANALYZED或TOKENIZED)和(NOT_ANALYZED或UN_TOKENIZED).区别在于:前者表示分词,后者表示不分词.例如:"中国人",使用StandardAnalyzer分析器分词结果是:"中","国","人".而不分词是把"中国人"作为整体建索引.
二.StandardAnalyzer底层原理
- public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
- {
- TokenStream result = new StandardTokenizer(reader);
- result = new StandardFilter(result);
- result = new LowerCaseFilter(result);
- result = new StopFilter(result, stopSet);
- return result;
- }
这是StandardAnalyzer类的一段代码.LowerCaseFilter可知StandardAnalyzer在分词时会有转小写的操作.
建索引且分词时会被转小写.
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- QueryParser parser = new QueryParser("title", new StandardAnalyzer());
- Query query = parser.Parse(string.Format("title:{0}", key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
这是段利用QueryParser和StandardAnalyzer的搜索,同样有转小写的操作.
由于建索引是底层小写,搜索也是被小写化了.故使用这种方式从外观接口的角度来说是不区分大小写的.
三.不分词和TermQuery查询
由于Field没有分词,所以建索引时数据会保持原始大小写.
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- TermQuery query = new TermQuery(new Term("name", key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
这是一段使用TermQuery查询的方式.同样查询关键字是大写就大写,是小写就小写.
在这种使用情况下就会区分大小写.比如索引"abc",查询"Abc"就查不出来.
我的解决方案是:
建索引时小写化保存能,搜索时关键字小写化查询.
四.分词,不分词,StandardAnalyzer,TermQuery组合.
1.不一定建索引时使用StandardAnalyzer,搜索时也时用StandardAnalyzer或不分词和TermQuery查询.其实有很多组合.
2.不仅StandardAnalyzer底层小写化,还有别的分析器也是这样的.或者可以自定义分析器.
五.lucene区分大小写示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Lucene.Net.Documents;
- using Lucene.Net.Index;
- using Lucene.Net.Search;
- using Lucene.Net.Analysis;
- using Lucene.Net.Analysis.Standard;
- using Lucene.Net.QueryParsers;
- namespace IndexTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- createIndex();
- searchNameByTermQuery("abc");
- searchTitleByTermQuery("abc");
- searchNameByTermQuery("ABC");
- searchTitleByTermQuery("ABC");
- searchNameByQueryParser("ABC");
- searchTitleByQueryParser("ABC");
- //修改后的解决方案
- createIndex2();
- searchNameByTermQuery2("ABC");
- Console.ReadLine();
- }
- public static void createIndex()
- {
- Document doc1 = new Document();
- Field field = null;
- field = new Field("name", "abc", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc1.Add(field);
- field = new Field("title", "abc", Field.Store.YES, Field.Index.TOKENIZED);
- doc1.Add(field);
- field = new Field("id", "1", Field.Store.YES, Field.Index.NO);
- doc1.Add(field);
- Document doc2 = new Document();
- field = new Field("name", "Abc", Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc2.Add(field);
- field = new Field("title", "Abc", Field.Store.YES, Field.Index.TOKENIZED);
- doc2.Add(field);
- field = new Field("id", "2", Field.Store.YES, Field.Index.NO);
- doc2.Add(field);
- IndexWriter writer = new IndexWriter("c:\java\index", new StandardAnalyzer(), true);
- writer.AddDocument(doc1);
- writer.AddDocument(doc2);
- writer.Close();
- }
- public static void searchNameByTermQuery(string key)
- {
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- TermQuery query = new TermQuery(new Term("name", key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
- }
- public static void searchTitleByTermQuery(string key)
- {
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- TermQuery query = new TermQuery(new Term("title", key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
- }
- public static void searchNameByQueryParser(string key)
- {
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- QueryParser parser = new QueryParser("name", new StandardAnalyzer());
- Query query = parser.Parse(string.Format("name:{0}",key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
- }
- public static void searchTitleByQueryParser(string key)
- {
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- QueryParser parser = new QueryParser("title", new StandardAnalyzer());
- Query query = parser.Parse(string.Format("title:{0}", key));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
- }
- public static void createIndex2()
- {
- Document doc1 = new Document();
- Field field = null;
- field = new Field("name", "abc".ToLower(), Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc1.Add(field);
- field = new Field("title", "abc", Field.Store.YES, Field.Index.TOKENIZED);
- doc1.Add(field);
- field = new Field("id", "1", Field.Store.YES, Field.Index.NO);
- doc1.Add(field);
- Document doc2 = new Document();
- field = new Field("name", "Abc".ToLower(), Field.Store.YES, Field.Index.UN_TOKENIZED);
- doc2.Add(field);
- field = new Field("title", "Abc", Field.Store.YES, Field.Index.TOKENIZED);
- doc2.Add(field);
- field = new Field("id", "2", Field.Store.YES, Field.Index.NO);
- doc2.Add(field);
- IndexWriter writer = new IndexWriter("c:\java\index", new StandardAnalyzer(), true);
- writer.AddDocument(doc1);
- writer.AddDocument(doc2);
- writer.Close();
- }
- public static void searchNameByTermQuery2(string key)
- {
- Hits hits = null;
- IndexSearcher searcher = new IndexSearcher("c:\java\index");
- TermQuery query = new TermQuery(new Term("name", key.ToLower()));
- hits = searcher.Search(query);
- printResult(hits, query.ToString());
- }
- public static void printResult(Hits hits, String key)
- {
- Console.WriteLine("查询 " + key);
- if (hits != null)
- {
- if (hits.Length() == 0)
- {
- Console.WriteLine("没有找到任何结果");
- }
- else
- {
- Console.WriteLine("找到" + hits.Length() + "个结果");
- for (int i = 0; i < hits.Length(); i++)
- {
- Document d = hits.Doc(i);
- String id = d.Get("id");
- Console.WriteLine(id.ToString() + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
- }
- 上一篇Redis 命令手册
- 下一篇Lucene query使用总结
- 顶