簡體   English   中英

如何在彈性搜索中應用帶有 match_prefix 的術語查詢

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

我的文件在下面

[
{'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'}
]
  • 我需要搜索 if var.keyword = sports
  • 我需要強調一下
  • 它必須為查詢添加前綴

DSL查詢如下

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

得到錯誤

RequestError: RequestError(400, 'x_content_parse_exception', '[match_phrase_prefix] 格式錯誤的查詢,預期 [END_OBJECT] 但找到 [FIELD_NAME]')

我的預期結果只有 id:2

您快到了,但您需要在 JSON 中與query並行highlight ,而不是在query中。 正確的查詢是

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

突出顯示應該在查詢子句之外,而不是在查詢內。

{
  "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