Elasticsearch 5.1.1 Search Highlighting and Java API implementations

Source: Internet
Author: User
Tags kibana

5.1.1 's search highlighting and 2. X has changed, but not much. Here are four steps to: Create an index (set Mapping/ik participle), index document, search highlighting for REST API, search highlighting for JAVA API.
Note: Starting with this blog, use the shorthand code style, which is the style used in the sence plugin or Kibana dev tools. (Tip: To install Kibana 5.1.1, you can use the simple format command directly in Dev tools.) )

First, create an index

The document structure is blog/article/id. Create an empty index first:

PUT blog

Create mapping:

POST blog/article/_mapping{"article": {"_all": {"Analyzer":"Ik_max_word","Search_analyzer":"Ik_max_word","Term_vector":"No","Store":"false"},"Properties": {"title": {"Type":"Text","Analyzer":"Ik_max_word","Search_analyzer":"Ik_max_word","Include_in_all":"true","Boost":8},"Content": {"Type":"Text","Analyzer":"Ik_max_word","Search_analyzer":"Ik_max_word","Include_in_all":"true","Boost":4}        }    }}
Second, index test document

Add three documents to the blog index.
Document 1:

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

Document 2:

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

Document 3:

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

We query the title containing the Java document and mark it with a custom highlight fragment.

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

Query Result:

    "Total":2,"Max_score":2.3014567,"hits": [      {"_index":"Blog","_type":"article","_id":"3","_score":2.3014567,"_source": {"title":"Java from getting started to mastering","Content":"Java from getting started to mastering" is perfect for Java beginners "},"Highlight": {"title": ["<strong>java</strong> from beginner to proficient"]        }      },      {"_index":"Blog","_type":"article","_id":"1","_score":2.025282,"_source": {"title":"Java Programming ideas","Content":"Java Programming Ideas" is a book that has won wide acclaim from global programmers. "},"Highlight": {"title": ["<strong>java</strong> Programming Ideas"]        }      }    ]  }}

Iv. search for highlighted Java API implementations

Jar Package Import Please refer to the previous blog Elasticsearch 5.X under the Java API Usage guide
Write a test class:

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-ge nerated method Stub//set cluster name Settings Settings = Settings. Builder(). Put("Cluster.name","Elasticsearch"). Build();Create client Transportclient client = new Prebuilttransportclient (settings). Addtransportaddress(New Inetsockettransportaddress (inetaddress. Getbyname("127.0.0.1"),9300));QueryBuilder matchquery = querybuilders. Matchquery("title","Programming");Highlightbuilder hibuilder=new Highlightbuilder ();Hibuilder. Posttags(");Hibuilder. Posttags(");Hibuilder. Field("title");Search Data SearchResponse response = Client. Preparesearch("Blog"). Setquery(Matchquery). Highlighter(Hibuilder). Execute(). Actionget();Get query result set searchhits Searchhits = response. Gethits();System. out. println("Total Search:"+searchhits. Gettotalhits()+"Article Results!");Traversal result for (Searchhit hit:searchhits) {System. out. println("String Print Document Search content:");System. out. println(Hit. getsourceasstring());System. out. println("map mode to print highlighting content");System. out. println(Hit. Gethighlightfields());System. out. println("Iterate through the highlighted collection and print the highlighted clip:");text[] Text = Hit. Gethighlightfields(). Get("title"). Getfragments();for (Text str:text) {System. out. println(str. String());}        }    }}

Operation Result:

No modules loadedloaded 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] Total Search:1Results of the article! Print a document search in string mode: {"title":"Java Programming ideas","Content":"Java Programming Ideas" is a book that has won wide acclaim from global programmers. "}map way to print highlighted content {Title=[title], fragments[[java<em> programming 

As follows:

Elasticsearch 5.1.1 Search Highlighting and Java API implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.