es of advanced query

1. complex queries

Composite query is to put together some simple queries more complex query requirements, in addition to complex queries can also control the behavior of another query.

 

1.1 constant_score

constant_score query may be other types of packaging a query and returns the query conditions that match the filter with the same score in the document. When we are not concerned about the impact on the frequency of search terms ranked search results, you can use constant_score the query query filter or filter statements packaged.

 

Examples are as follows:

 

{

"query":{

"constant_score":{

"filter":{

"term":{

"City": "Beijing"

}

}

}

}

}

java examples are as follows:

 

MatchQueryBuilder query = QueryBuilders.matchQuery("title", "新闻").minimumShouldMatch("90%");

QueryBuilders.constantScoreQuery(query).boost(3.0f);

 

1.2 bool query

bool query can be any number of simple queries together, use must, should, must_not, filter options represent logical connections between simple queries, each option can appear anywhere from zero to many times their meanings are:

 

must: document must match the query in option MUST phase block in the logical operators AND.

should: document matches the query condition Should the option may not match the phase of the block to an OR logic operation.

must_not: On the contrary, the document that match the query conditions of the option will not be returned and must.

filter: the same and must match the query conditions in the filter options document will be returned, but the filter does not score, just play filtering.

Examples are as follows:

 

{

"query":{

"bool":{

"must":[

{

"match":{

"Title": "Sharing Bicycle"

}

},

{

"term":{

"District": "Changping District"

}

}

],

"should":[

{

"match":{

"Address": "Chaoyang District"

}

}

]

}

}

}

 

1.3 dis_max query

dis_max query have some contact with bool query also have some differences, dis_max query support multiple concurrent queries can return any type of document and query clause match. Bool query may be different with all the scores match the query of a combination of, dis_max query using only the best score of the query.

 

{

"query":{

"dis_max":{

"tie_breaker":0.7,

"boost":1.2,

"queries":[

{

"Match": { "address": "Beijing Chaoyang District"}

},

{

"match":{

"Title": "Beijing Chaoyang District."

}

}

]

}

}

}

1.4 function_score query

function_score query can modify the document query score, the query is very useful in some cases, such as the document is calculated by scoring function score higher costs, you can use filters plus custom scoring function way to replace the traditional scoring method.

 

Use function_score query, the user needs to define a query and a multiple scoring function scoring function will query to calculate the score of each document separately.

 

{

"query": {

"function_score": {

"query": {

"function_score": {

"query": {

"match": {

"title": "java编程"

}

},

"functions": [

{

"field_value_factor": {

"field": "price",

"factor": 0.1,

"modifier": "ln1p"

}

}

],

"score_mode": "multiply",

"max_boost": 10,

"boost": 1

}

},

"functions": [],

"score_mode": "multiply",

"boost_mode": "sum",

"max_boost": 10,

"boost": 1

}

}

}

java examples are as follows:

 

MatchPhraseQueryBuilder titleQuery = QueryBuilders.matchPhraseQuery("title", "java编程");

FieldValueFactorFunctionBuilder factor = ScoreFunctionBuilders.fieldValueFactorFunction("price").modifier(FieldValueFactorFunction.Modifier.LN1P).factor(0.1f);

FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = {

new FunctionScoreQueryBuilder.FilterFunctionBuilder(factor)

};

FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(titleQuery,filterFunctionBuilders);

FunctionScoreQueryBuilder query = QueryBuilders.functionScoreQuery(functionScoreQuery).boostMode(CombineFunction.SUM);

1

2

3

4

5

6

7

Detailed scoring function

 

1.5 boosting query

boosting the need for two query scenarios for adjusting query score, boosting the query will decrease and packaged together two queries one of the query score.

 

boosting query includes positive, negative and negative_boost three parts, positive query score remained unchanged, negative queries will reduce the document score, negative_boost indicate negative weights in the decrease.

 

Java query title for the book, published in 2018 in the time at the back.

 

{

"query":{

"boosting":{

"positive":{

"match":{

"title":"java"

}

},

"negative":{

"range":{

"publishAt":{

"lte":"2018-01-01"

}

}

"negative_boost":0.2

}

}

}

13

java examples are as follows:

 

MatchPhraseQueryBuilder titleQuery = QueryBuilders.matchPhraseQuery("title", "java");

RangeQueryBuilder publishAt = QueryBuilders.rangeQuery("publishAt").lte("2018-01-01");

QueryBuilders.boostingQuery(titleQuery,publishAt).negativeBoost(0.2f);

2. nested query

2.1 Sons inquiry

Here to shops and commodities, for example, they belong to different types, the equivalent of two tables in the database, if you want to run the shop and merchandise associate you need to tell a parent-child relationship between the ES document, specify a field here store2product to maintain this relationship, the type of the field to join, and specify their relationship.

 

2.1.1 Create a parent-child relationship index

PUT store

{

"mappings": {

"properties": {

"name": {

"type": "text",

"analyzer": "ik_max_word",

"search_analyzer": "ik_smart"

},

"store2product": {

"type": "join",

"relations": {

"storeid": "productid"

}

}

}

}

}

 

2.2.2 Adding relations documents

Add the parent document (shops), and specify the parent field

 

PUT store/1

{

"Name": "Real food franchise record shop"

"store2product":"storeid"

}

Add a child document (commodity), and require the child document and parent document in the same slice, you need to specify the parent id route id

 

PUT store/2?routing=1

{

"Name": "create a real flavor of cranberry powder plum juice instant brewed into tea, juice drinks 1000g sour plum raw"

"store2product":{

"name":"productid",

"parent":"1"

}

}

2.2.3 Query merchandise at the store

Use has_parent query, and specify parent_type as storeid

 

GET store/_search

{

"query": {

"has_parent": {

"parent_type": "storeid",

"query": {

"match": {

"Name": "Food shops"

}

}

}

}

}

java examples are as follows:

 

MatchQueryBuilder nameQuery = QueryBuilders.matchQuery ( "name", "Food shops");

HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder("storeid",nameQuery,true);

2.2.4 Queries belong to which goods shop

Use has_child query, and specify a type field productid

 

GET store/_search

{

"query": {

"has_child": {

"type":"productid",

"query": {

"match": {

"Name": "sour plum flavor"

}

}

}

}

}

java examples are as follows:

 

MatchQueryBuilder nameQuery = QueryBuilders.matchQuery ( "name", "plum juice flavor");

HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder("productid",nameQuery, ScoreMode.None);

 

Source: https://blog.csdn.net/dwjf321/article/details/103934964

Published 277 original articles · won praise 65 · views 380 000 +

Guess you like

Origin blog.csdn.net/ailiandeziwei/article/details/104709970