子条件查询:(特定字段查询所指特定值)
- Query Context: 在查询的过程中,除了判断文档是否满足条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件的匹配有多好。
 - 复合条件查询:(以一定的逻辑组合子条件查询)
 
查询的模拟文档如下:

一、Query Context:
1.全文本查询:
针对文本类型数据
(1)模糊匹配查询:
- 查询作者为李歘歘的书籍:
 
输入:
{
    "query": {
        "match": {
            "author": "李歘歘"
        }
    }
}
查询结果:

(2)短语匹配查询:
- 查询包含短语“elasticsearch入门”的书籍:
 
输入:
{
    "query": {
        "match_phrase": {
            "title": "elasticsearch入门"
        }
    }
}
查询结果:

(3)多个字段的匹配查询:
- 查询作者和标题包含“李歘歘”的书籍:
 
输入:
{
    "query": {
        "multi_match": {
            "query": "李歘歘",
            "fields": ["author","title"]
        }
    }
}
查询结果:
(3)语法查询:
- 查询包含“elasticsearch”和“入门”或者“java”的书籍:
 
输入:
{
    "query": {
        "query_string": {
            "query": "(elasticsearch AND 入门) OR java "
        }
    }
}
查询结果:

(3)语法查询:
- 查询title和author中包含“elasticsearch”和“李歘歘”的书籍:
 
输入:
{
    "query": {
        "query_string": {
            "query": "elasticsearch OR 李歘歘 ",
            "fields": [
                "title",
                "author"
            ]
        }
    }
}
查询结果:
 
2.字段级别的查询:
针对结构化数据,如数字、日期等
- 查询字数在1000的书籍:
 
输入:
{
    "query": {
        "term": {
            "word_count": 1000
        }
    }
} 
查询结果:

- 字数在1000到2000的书籍:
 
输入:
{
    "query": {
        "range": {
            "word_count": {
                "gte": 1000,
                "lte": 2000
            }
        }
    }
}
gte和lte中‘e’代表等于
查询结果:

- 查询字数在1999-01-01到2000-01-01的书籍:
 
输入:
{
    "query": {
        "range": {
            "publish_date": {
                "gte": "1999-01-01",
                "lte": "2000-01-01"
            }
        }
    }
}
查询结果:

二、Filter Context
在查询过程中,只判断该文档是否满足条件,只有YES或者NO
查询字数是1000的书:
{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "word_count": 1000
                }
            }
        }
    }
} 
查询结果:

三、复合条件查询
1.固定分数查询:
查询title包含“elasticsearch”并且评分在3的书籍
输入:
{
    "query": {
       "constant_score": {
        "filter": {
            "match": {
                "title": "elasticsearch"
            }
        },
        "boost": 3
       }
    }
}
查询结果:

2.布尔查询:
(1)查询title包含“elasticsearch”或者author包含“李歘歘”的书籍
输入:
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ]
        }
    }
}
运行结果:

(2)查询title包含“elasticsearch”并且author包含“李歘歘”的书籍
输入:
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ]
        }
    }
}
运行结果:

(3)查询title包含“elasticsearch”并且author包含“李歘歘”并且字数是1000的书籍
输入:
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ],
            "filter": {
                "term": {
                    "word_count": 1000
                }
            }
        }
    }
}
运行结果:

(3)查询author不包含“李歘歘”的书籍
输入:
{
    "query": {
        "bool": {
            "must_not": {
                "term": {
                    "author": "李歘歘"
                }
            }
        }
    }
}
运行结果:


作者:李歘歘
                











