Specialized queries

  • more_like_this query(相似度查询)


查看More Like This Query

String[] fields = {"name.first", "name.last"};                 //fields
String[] texts = {"text like this one"};                       //text
Item[] items = null;

QueryBuilder qb = moreLikeThisQuery(fields, texts, items)
    .minTermFreq(1)                                            //ignore threshold
    .maxQueryTerms(12);                                        //max num of Terms in generated queries
  • script query

该查询允许脚本充当过滤器。 另请参阅 function_score query

查看Script Query

QueryBuilder qb = scriptQuery(
    new Script("doc['num1'].value > 1") //inlined script

如果已经在每个数据节点上存储名为 `myscript.painless 的脚本,请执行以下操作:

doc['num1'].value > params.param1


QueryBuilder qb = scriptQuery(
    new Script(
        ScriptType.FILE,                       //脚本类型 ScriptType.FILE, ScriptType.INLINE, ScriptType.INDEXED
        "painless",                            //Scripting engine 脚本引擎
        "myscript",                            //Script name 脚本名
        Collections.singletonMap("param1", 5)) //Parameters as a Map of <String, Object>
  • Percolate Query

查看Percolate Query

Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(InetAddresses.forString(""), 9300)));


// create an index with a percolator field with the name 'query':
                        .addMapping("query", "query", "type=percolator")
                        .addMapping("docs", "content", "type=text")

//This is the query we're registering in the percolator
QueryBuilder qb = termQuery("content", "amazing");

//Index the query = register it in the percolator
client.prepareIndex("myIndexName", "query", "myDesignatedQueryName")
            .field("query", qb) // Register the query
    .setRefreshPolicy(RefreshPolicy.IMMEDIATE) // Needed when the query shall be available immediately

在上面的index中query名为 myDesignatedQueryName


//Build a document to check against the percolator
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("content", "This is amazing!");
docBuilder.endObject(); //End of the JSON root object

PercolateQueryBuilder percolateQuery = new PercolateQueryBuilder("query", "docs", docBuilder.bytes());

// Percolate, by executing the percolator query in the query dsl:
SearchResponse response = client().prepareSearch("myIndexName")
//Iterate over the results
for(SearchHit hit : response.getHits()) {
    // Percolator queries as hit

results matching ""

    No results matching ""