项目作者: sing1ee

项目描述 :
custom lucene query and elasticsearch query plugin
高级语言: Java
项目地址: git://github.com/sing1ee/lucene-custom-query.git
创建时间: 2017-11-01T06:24:55Z
项目社区:https://github.com/sing1ee/lucene-custom-query

开源协议:MIT License

下载


lucene-custom-query

a b seq/x c seq/x d seq/x e f

分支 tag elasticsearch版本 Release Link
0.1 tag v0.1 v0.1 Download: v0.1
  1. try {
  2. IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
  3. Directory dir = FSDirectory.open(new File("/Users/zhangcheng/Downloads/idx").toPath());
  4. IndexWriter writer = new IndexWriter(dir, conf);
  5. {
  6. Document doc = new Document();
  7. doc.add(new TextField("field", "b c d e f", Field.Store.YES));
  8. writer.addDocument(doc);
  9. }
  10. {
  11. Document doc = new Document();
  12. doc.add(new TextField("field", "x b c d x e f", Field.Store.YES));
  13. writer.addDocument(doc);
  14. }
  15. writer.commit();
  16. IndexReader reader = DirectoryReader.open(writer);
  17. IndexSearcher searcher = new IndexSearcher(reader);
  18. SeqSpanQuery ssQuery = new SeqSpanQuery("field", "b", "f", new String[]{"c", "d", "e"}, 3);
  19. TopScoreDocCollector collector = TopScoreDocCollector.create(10);
  20. searcher.search(ssQuery, collector);
  21. Stream.of(collector.topDocs().scoreDocs).forEach(x -> {
  22. System.out.println(x.doc + " : " + x.score);
  23. });
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }

ES插件使用

兼容版本

  • lucene 6.6.0
  • elasticsearch 5.5.1

打包插件

  1. ./gradlew clean build pz
  2. # build/distributions/lucene-custom-query.zip

使用插件

  1. 创建索引:POST http://localhost:9200/index
  2. 配置索引:POST http://localhost:9200/index/fulltext/_mapping

    1. # body
    2. {
    3. "fulltext": {
    4. "_all": {
    5. "analyzer": "standard",
    6. "search_analyzer": "standard",
    7. "term_vector": "no",
    8. "store": "false"
    9. },
    10. "properties": {
    11. "content": {
    12. "type": "text",
    13. "store": "no",
    14. "term_vector": "with_positions_offsets",
    15. "analyzer": "standard",
    16. "search_analyzer": "standard",
    17. "include_in_all": "true",
    18. "boost": 8
    19. }
    20. }
    21. }
    22. }
  3. 创建文档 POST http://localhost:9200/index/fulltext/

    1. # http://localhost:9200/index/fulltext/1
    2. {"content":"a b c d e f"}
    3. # http://localhost:9200/index/fulltext/2
    4. {"content":"b c d e f g"}
  4. 查询 POST http://localhost:9200/index/fulltext/_search
    1. {
    2. "query": {
    3. "seq_span": {
    4. "field": "content",
    5. "start_term": "b",
    6. "end_term": "g",
    7. "seq_term": "c d e f",
    8. "max_span": 8
    9. }
    10. }
    11. }
    Result:
    1. {
    2. "took": 101,
    3. "timed_out": false,
    4. "_shards": {
    5. "total": 5,
    6. "successful": 5,
    7. "failed": 0
    8. },
    9. "hits": {
    10. "total": 1,
    11. "max_score": 0,
    12. "hits": [
    13. {
    14. "_index": "index",
    15. "_type": "fulltext",
    16. "_id": "2",
    17. "_score": 0,
    18. "_source": {
    19. "content": "b c d e f g"
    20. }
    21. }
    22. ]
    23. }
    24. }