【问题标题】:match_query is not working in elasticsearchmatch_query 在弹性搜索中不起作用
【发布时间】:2015-05-19 20:02:27
【问题描述】:

我是新的 ElasticSearch。最近我们开始在我的项目中使用 ElasticSearch。 我正在尝试使用 match_query 获取基于 first_name 和 last_name 的数据。 我这样写查询

{
"query":{
  "multi_match" : {
    "query": "michkel",
    "fields": [ "last_name"] 

  }
}
}

我的索引名称是员工,它包含以下文档

{
   "first_name":"smith",
"last_name":"michkel",
"age":25,
"doj":"2015-05-2"
},
{
   "first_name":"john",
"last_name":"smith",
"age":46,
"doj":"1989-03-25"
}

当运行上述查询时,我得到了总记录,但我的要求是如果我像这样给出“查询”:“michkel”,那么只有一条记录与我的文档匹配。 尽快给我解决方案 还有一个问题 我想根据用户给定的日期进行搜索。我的用户将给出这样的日期 1989-04-15。为此我写如下 { “询问”:{ “ 请求参数” : { "查询": "1989-04-15",

  }
}
}
{"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[du1Tb7U8QLWSnXO2m9ATCg][employee][0]: SearchParseException[[employee][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n\"query\":{\n  \" query_string\" : {\n    \"query\": \"1989-04-15\",\n\n  }\n}\n}]]]; nested: QueryParsingException[[employee] No query registered for [ query_string]]; }]","status":400}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用bool 过滤器代替multi_match,如下所示:

    {
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "last_name": "michkel"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    根据您编辑的问题,您应该在匹配替换中使用与 "doj": "1989-04-15" 相同的内容,如果您想找出结合 last_namedoj 的内容,请使用如下查询:

    {
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "1989-04-15"
                                }
                            }
                        ],
                        "must": [
                            {
                                "match": {
                                    "last_name": "michkel"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 刚刚我更新了我的问题,一旦看到并给我解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2020-01-08
    • 2020-10-15
    相关资源
    最近更新 更多