ES :有两种常用查询  

        must   必须满足查询条件

        should 非必须满足查询条件

 如果想实现类似与mysql中的or 查询效果,必须使用should查询。但是shuold 会查询出不满足条件的数据 ,这必须加一个属性 "minimum_should_match": "1" 必须满足should条件中的一个查询条件 这样就能达到 or查询的结果了。

"minimum_should_match": "1" 这个属性不能乱用,如果没有should查询的条件不要加个这个属性

提供参考原生es查询条件:

{
	"from": 0,
	"size": 30,
	"query": {
		"bool": {
			"must": [
				{
					"query_string": {
						"query": "status:0",
						"fields": [],
						"type": "best_fields",
						"default_operator": "or",
						"max_determinized_states": 10000,
						"enable_position_increments": true,
						"fuzziness": "AUTO",
						"fuzzy_prefix_length": 0,
						"fuzzy_max_expansions": 50,
						"phrase_slop": 0,
						"analyze_wildcard": true,
						"escape": false,
						"auto_generate_synonyms_phrase_query": true,
						"fuzzy_transpositions": true,
						"boost": 1.0
					}
				},
				{
					"terms": {
						"saleableRange": [
							"0",
							"2"
						],
						"boost": 1.0
					}
				},
				{
					"query_string": {
						"query": "entity:Product",
						"fields": [],
						"type": "best_fields",
						"default_operator": "or",
						"max_determinized_states": 10000,
						"enable_position_increments": true,
						"fuzziness": "AUTO",
						"fuzzy_prefix_length": 0,
						"fuzzy_max_expansions": 50,
						"phrase_slop": 0,
						"escape": false,
						"auto_generate_synonyms_phrase_query": true,
						"fuzzy_transpositions": true,
						"boost": 1.0
					}
				}
			],
			"should": [
				{
					"query_string": {
						"query": "shortName.keyword:*1011838*",
						"fields": [],
						"type": "best_fields",
						"default_operator": "or",
						"max_determinized_states": 10000,
						"enable_position_increments": true,
						"fuzziness": "AUTO",
						"fuzzy_prefix_length": 0,
						"fuzzy_max_expansions": 50,
						"phrase_slop": 0,
						"analyze_wildcard": true,
						"escape": false,
						"auto_generate_synonyms_phrase_query": true,
						"fuzzy_transpositions": true,
						"boost": 1.0
					}
				},
				{
					"query_string": {
						"query": "code.keyword:*1011838*",
						"fields": [],
						"type": "best_fields",
						"default_operator": "or",
						"max_determinized_states": 10000,
						"enable_position_increments": true,
						"fuzziness": "AUTO",
						"fuzzy_prefix_length": 0,
						"fuzzy_max_expansions": 50,
						"phrase_slop": 0,
						"analyze_wildcard": true,
						"escape": false,
						"auto_generate_synonyms_phrase_query": true,
						"fuzzy_transpositions": true,
						"boost": 1.0
					}
				}
			],
			"adjust_pure_negative": true,
			"minimum_should_match": "1",
			"boost": 1.0
		}
	},
	"sort": [
		{
			"sortNum": {
				"order": "desc"
			}
		}
	]
}

这个查询满足的条件是 shortName的查询条件 或 code 查询条件的结果

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐