简体   繁体   English

如何在弹性搜索中应用带有 match_prefix 的术语查询

[英]How to apply term query with match_prefix in elasticsearch

My document is below我的文件在下面

[
{'id':1, 'name': 'sachin messi', 'description': 'football@football.com', 'type': 'football', 'var':'sports'},
{'id':2, 'name': 'lionel messi', 'description': 'messi@fifa.com','type': 'soccer','var':'sports'},
{'id':3, 'name': 'sachin', 'description': 'was', 'type': 'cricket', 'var':'sports'}
]
  • I need to search on if var.keyword = sports我需要搜索 if var.keyword = sports
  • I need to highlight on it我需要强调一下
  • It has to prefix query它必须为查询添加前缀

DSL query is below DSL查询如下

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "var.keyword": [
              "sports"
            ]
          }
        },
        {
          "match_phrase_prefix": {
            "name": {
              "query": "lio"
            }
          },
          "highlight": {
            "fields": {
              "name": {}
            }
          }
        }
      ]
    }
  }
}

Getting error得到错误

RequestError: RequestError(400, 'x_content_parse_exception', '[match_phrase_prefix] malformed query, expected [END_OBJECT] but found [FIELD_NAME]') RequestError: RequestError(400, 'x_content_parse_exception', '[match_phrase_prefix] 格式错误的查询,预期 [END_OBJECT] 但找到 [FIELD_NAME]')

My expected out is only id:2我的预期结果只有 id:2

You are almost there, but you need to have highlight parallel to query in your JSON, not inside the query .您快到了,但您需要在 JSON 中与query并行highlight ,而不是在query中。 Correct query would be正确的查询是

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "var.keyword": [
                            "sports"
                        ]
                    }
                },
                {
                    "match_phrase_prefix": {
                        "name": {
                            "query": "lio"
                        }
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "name": {}
        }
    }
}

Highlight schould be outside query clause and not inside query.突出显示应该在查询子句之外,而不是在查询内。

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "var.keyword": [
              "sports"
            ]
          }
        },
        {
          "match_phrase_prefix": {
            "name": {
              "query": "lio"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "name":{}
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM