Boolean Query with Elasticsearch

流过昼夜 提交于 2020-03-25 12:31:19

问题


I am currently using the following query -

{
    "_source": [
        "title",
        "bench",
        "id_",
        "court",
        "date",
        "content"
    ],
    "size": 15,
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "the",
                        "fields": [

                            "content"
                        ], "operator": "and"
                    }
                },

            ],
            "should": {
                "multi_match": {
                    "query": "the",
                    "fields": [
                        "content.standard^2"
                    ], "operator": "and"
                }
            }
        }
    }
    ,
    "highlight": {
        "pre_tags": [
            "<tag1>"
        ],
        "post_tags": [
            "</tag1>"
        ],
        "fields": {
            "content": {}
        },
        "fragment_size": 100
    }
}

With the following mapping

{
    "courts_2": {
        "mappings": {
            "properties": {
                "author": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "bench": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "citation": {
                    "type": "text"
                },
                "content": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "court": {
                    "type": "text"
                },
                "date": {
                    "type": "text"
                },
                "id_": {
                    "type": "text"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "verdict": {
                    "type": "text"
                }
            }
        }
    }
}

My analyzer is a Metaphone analyzer. Here is my Aim. I want the exact matches ( standard ) to appear first, followed by phonetic ones. I am able to achieve that with the code. I am pretty sure that there is (are) some unwanted logic in this and would deeply appreciate if someone can point to it.

Additionally what I would like to incorporate is a search logic where a user can enter a search like

Real Madrid AND Barcelona OR Manchester United. Here I want all documents that contain Real Madrid and either Barcelona/Man Utd/ How do I achieve with the current query I have in place (with modifications)?


回答1:


I can't see any sideeffects in your query. Regarding your search logic, the simplest approach would probably by using a query string query instead. It supports boolean operators by default, so your query could look something like this:

{
    "_source": [
        "title",
        "bench",
        "id_",
        "court",
        "date",
        "content"
    ],
    "size": 15,
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "query_string" : {
                        "query" : "Real Madrid AND Barcelona OR Manchester United",
                        "fields" : ["content", "title"],
                        "operator": "and"
                    }
                }
            ],
            "should": {
                "query_string" : {
                    "query" : "Real Madrid AND Barcelona OR Manchester United",
                    "fields" : ["content.standard", "title.standard"],
                    "boost": 2,
                    "operator": "and"
                }
            }
        }
    },
    "highlight": {
        "pre_tags": [
            "<tag1>"
        ],
        "post_tags": [
            "</tag1>"
        ],
        "fields": {
            "content": {}
        },
        "fragment_size": 100
    }
}

Otherwise you would have to parse your query and model the operators with multiple bool queries.



来源:https://stackoverflow.com/questions/60376466/boolean-query-with-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!