【问题标题】:malformed query in elastic search弹性搜索中的格式错误的查询
【发布时间】:2016-07-08 11:14:36
【问题描述】:

我需要在 Elastic Search 中编写一个非常复杂的查询。

查询存储在一个字符串query中,想法是对字段titlecontent进行“布尔”匹配,然后对诸如“java”之类的词赋予特殊权重或“蟒蛇”。

我的尝试是

{
    "fields": ["title","content","id"],
     "query": {
       "function_score": {
       "query":
         {
                "bool":
                {
                    "should":
                    [
                        {"match": {"title": {"query": query, "boost": 15}}},
                        {"match": {"content": {"query": query, "boost": 9}}}
                    ]
                }
            }
         },
         "functions": [
            {
            "filter": {
               "match": {
                 "title": "java"
            }},
            "weight": 2 },
            {
            "filter": {
               "match": {
                 "title": "python"
            }},
            "weight": 2 }
        ],
         "boost_mode": "multiply"
       }
} 

但我收到一条错误消息,指出查询格式错误。如果这是一个幼稚的问题,我很抱歉,但我不知道还能去哪里。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    试试这个:

    {
      "fields": [
        "title",
        "content",
        "id"
      ],
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "should": [
                {
                  "match": {
                    "title": {
                      "query": query,
                      "boost": 15
                    }
                  }
                },
                {
                  "match": {
                    "content": {
                      "query": query,
                      "boost": 9
                    }
                  }
                }
              ]
            }
          },
          "functions": [
            {
              "filter": {
                "match": {
                  "title": "java"
                }
              },
              "weight": 2
            },
            {
              "filter": {
                "match": {
                  "title": "python"
                }
              },
              "weight": 2
            }
          ],
          "boost_mode": "multiply"
        }
      }
    } 
    

    【讨论】:

    • 我看到如果我不使用任何过滤器(即“过滤器”:[]),我会得到相同的结果,但在普通查询中使用“bool”下的部分时会得到不同的分数.你知道为什么吗?
    • 嗯,当然,这就是这些过滤器的全部意义所在。根据该特定功能(在您的情况下为weight 功能),从与查询匹配的文档中,与过滤器匹配的文档将具有不同的评分。 "boost_mode": "multiply"weight: 2 表示title 匹配javapython 的文档将具有查询产生的正常分数乘以weight 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2021-07-10
    • 2020-09-12
    • 2014-11-09
    相关资源
    最近更新 更多