Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of should failure when elasticsearch should and must coexist

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to solve the problem of should failure when elasticsearch should and must coexist". In daily operation, I believe that many people have doubts about how to solve the problem of should failure when elasticsearch should and must coexist. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the question of "how to solve the problem of should failure when elasticsearch should and must coexist". Next, please follow the editor to study!

When you use a mixed query of must and should, you find that should doesn't work.

For example, when searching for bread1 or bread2 data, according to the logic of the programming language, bread1 or bread2 must be satisfied under the condition of axiom 1.

So the parallelism of must and should is wrong.

Pay attention to the wrong writing

According to the search results, we can find that should does not work.

Correct writing method

$params = ['index' = >' news', 'type' = >' _ doc', 'body' = > [' query' = > ['bool' = > [' must' = > [['match' = > [' age' = > 50] ['bool' = > [' should' = > [['match' = > [' content' = > 'tomato']] ['match' = > [' content' = > 'China and the United States'] $result = $this- > es- > search ($params); var_dump ($result)

Search results

For example, in the data of axi1 and bicon2, Find out the data of cantilever 1 or dumb2: {"query": {"bool": {"must": [{"term": {"a": "1"}}, {"term": {"b": "2"}}], "should": [{"term": {"c": "1"}} {"term": {"d": "2"}]} when you write this, should is useless. This is one of the mistakes that beginners may make. When writing query conditions, you cannot write them with verbal logic, but you have to replace them with mathematical logic before they can be executed (the same is true for databases). As in the above example, the mathematical logic should be (a==1&&b==2&&c==1) | | (a==1&&b==2&&d==2), such a structure to query. {"query": {"bool": {"should": [{"term": {"a": "1"}}, {"term": {"b": "2"}}, {"term": {"c": "1"}], "should": [{"term": {"a": "1"}} {"term": {"b": "2"}}, {"term": {"d": "2"}]} the train of thought is just like that. There are two specific ways to write it: {"query": {"bool": {"should": [{"bool": {"must": [{"term": {"a": "1"}}, {"term": {"b": "2"}}) {"term": {"c": "1"}]}, {"bool": {"must": [{"term": {"a": "1"}, {"term": {"b": "2"}} {"term": {"d": "2"}]}]}}, "sort": {"time": {"order": "desc"}} "size": 100} or {"query": {"bool": {"must": [{"term": {"a": "1"}, {"term": {"b": "2"}} {"bool": {"should": [{"term": {"c": "1"}} {"term": {"d": "2"}]}, "sort": {"time": {"order": "desc"}}, "size": 100} so far The study on "how to solve the problem of should failure when elasticsearch should and must coexist" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report