Elasticsearch 5.1.1搜索高亮及Java API实现

5.1.1的搜索高亮和2.X有所变化,但是变化不大。下面分四步来介绍:创建索引(设置mapping/IK分词)、索引文档、REST API的搜索高亮、JAVA API的搜索高亮。
注:从这篇博客开始,采用简写的代码风格,也就是Sence插件或者kibana的dev tools中采用的风格。(温馨提示:安装kibana 5.1.1,在dev tools中直接可以使用简单格式命令。)

一、创建索引

文档结构为blog/article/id。先创建一个空的index:

PUT blog

创建mapping:

POST blog/article/_mapping
{
  "article": {

    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "boost": 8
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word",
        "boost": 4
      }
    }
  }
}

二、索引测试文档

加入三条文档到blog索引。
文档1:

POST blog/article/1
{
  "title":"java编程思想",
  "content":"《Java编程思想》这本书赢得了全球程序员的广泛赞誉"
}

文档2:

POST blog/article/2
{
  "title":"手把手教你使用Git",
  "content":"这是一个非常容易上手的GIt详细教程"
}

文档3:

POST blog/article/3
{
  "title":"java从入门到精通",
  "content":"《java从入门到精通》非常适合java初学"
}

三、REST高亮API

我们查询title中含有java的文档,并用自定义高亮片段标记出来。

POST blog/_search
{
  "query": {
    "match": {
      "title": "java"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": "",
        "post_tags": ""
      }
    }
  }
}

查询结果:

    "total": 2,
    "max_score": 2.3014567,
    "hits": [
      {
        "_index": "blog",
        "_type": "article",
        "_id": "3",
        "_score": 2.3014567,
        "_source": {
          "title": "java从入门到精通",
          "content": "《java从入门到精通》非常适合java初学"
        },
        "highlight": {
          "title": [
            "java从入门到精通"
          ]
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": 2.025282,
        "_source": {
          "title": "java编程思想",
          "content": "《Java编程思想》这本书赢得了全球程序员的广泛赞誉"
        },
        "highlight": {
          "title": [
            "java编程思想"
          ]
        }
      }
    ]
  }
}

Elasticsearch 5.1.1搜索高亮及Java API实现_第1张图片

四、搜索高亮的java api实现

jar包导入请参考前面的的一篇博客Elasticsearch 5.X下JAVA API使用指南
写一个测试类:

package esjavapia5;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class Es5Highlight {
    public static void main(String[] args) throws UnknownHostException {
        // TODO Auto-generated method stub
        // 设置集群名称
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch").build();
        // 创建client
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(
                        InetAddress.getByName("127.0.0.1"), 9300));
        QueryBuilder matchQuery = QueryBuilders.matchQuery("title", "编程");
        HighlightBuilder hiBuilder=new HighlightBuilder();
        hiBuilder.preTags("

"); hiBuilder.postTags("

"
); hiBuilder.field("title"); // 搜索数据 SearchResponse response = client.prepareSearch("blog") .setQuery(matchQuery) .highlighter(hiBuilder) .execute().actionGet(); //获取查询结果集 SearchHits searchHits = response.getHits(); System.out.println("共搜到:"+searchHits.getTotalHits()+"条结果!"); //遍历结果 for(SearchHit hit:searchHits){ System.out.println("String方式打印文档搜索内容:"); System.out.println(hit.getSourceAsString()); System.out.println("Map方式打印高亮内容"); System.out.println(hit.getHighlightFields()); System.out.println("遍历高亮集合,打印高亮片段:"); Text[] text = hit.getHighlightFields().get("title").getFragments(); for (Text str : text) { System.out.println(str.string()); } } } }

运行结果:

no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
共搜到:1条结果!
String方式打印文档搜索内容:
{
  "title":"java编程思想",
  "content":"《Java编程思想》这本书赢得了全球程序员的广泛赞誉"
}

Map方式打印高亮内容
{title=[title], fragments[[java

编程

思想]]} 遍历高亮集合,打印高亮片段: java编程思想

截图如下:

Elasticsearch 5.1.1搜索高亮及Java API实现_第2张图片

你可能感兴趣的:(Elasticsearch,Elasticsearch专栏)