如何在具有属性层次结构的DSL查询中增加权重 [英] How to add weightage in DSL query with hierarchy of attributes

查看:100
本文介绍了如何在具有属性层次结构的DSL查询中增加权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我需要搜索更多权重到 professor.name

  • 然后使用下一个属性来获取权重是 professor.email

  • 检查搜索字符串所在的其他字段

以下是Elasticsearch中的样本数据

Below is the sample data in elasticsearch

   PUT /data/test/1
 {
       "id": "Accounting 101",
       "room": "E3",
       "professor": {
           "name": "Thomas Baszo",
           "email": "baszot@onuni.com"
           },
       "students_enrolled": 27,
       "course_description": " financial statements"
   }
   
   PUT /data/test/2
   {
       "name": "Accounting 101",
       "room": "E3",
       "professor": {
           "name": "Sachin Baszo",
           "email": "baszot@onuni.com"
           },
       "students_enrolled": 27, 
       "course_description": "Thomas  Thomas Thomas Thomas "
   }

   PUT /data/test/3
   {
       "name": "Accounting 101",
       "room": "E3",
       "professor": {
           "name": "Sachin Baszo",
           "email": "Thomas@onuni.com"
           },
       "students_enrolled": 27, 
       "course_description": "Nothing"
   }

以下是查询

GET /_search
{
  "query": {
    "query_string": {
      "query": "(*Thomas*)"
    }
  }
}

我的输出将第二个文档显示为第一个包含托马斯描述中有4次

My output will show second document as first as it contains "Thomas" 4 times in the description


  • 我需要给 professor.name 赋予更多权重检查是否存在,然后检查 professor.email。然后检查其他属性

  • I need to give more weightage to professor.name it should show first check if not then check "professor.email" then check other attributes

预期是 1,3,2 1是因为名称,3是因为电子邮件和2由于描述原因

Expected out is 1,3,2 1 because name, 3 because of email and 2 because of description

Python

es.search(index = data,body = { " query" ;: {" query_string" ;: {" query" ;:"(* Thomas *)"}}})

推荐答案

添加包含索引数据,搜索查询和搜索结果的有效示例

Adding a working example with index data, search query, and search result

boost 也可以在创建索引时应用于各个字段,如下所述在上一个答案

The boost can be applied to the individual fields as well while creating the index as explained in the previous answer

索引数据:

{
    "id": "Accounting 101",
    "room": "E3",
    "professor": {
        "name": "Thomas Baszo",
        "email": "baszot@onuni.com"
    },
    "students_enrolled": 27,
    "course_description": " financial statements"
}
{
    "name": "Accounting 101",
    "room": "E3",
    "professor": {
        "name": "Sachin Baszo",
        "email": "baszot@onuni.com"
    },
    "students_enrolled": 27,
    "course_description": "Thomas  Thomas Thomas Thomas "
}
{
    "name": "Accounting 101",
    "room": "E3",
    "professor": {
        "name": "Baszo",
        "email": "Thomas@onuni.com"
    },
    "students_enrolled": 27,
    "course_description": "Nothing"
}

搜索查询:

{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "Thomas",
          "fields": [
            "professor.name^16",
            "professor.email^8",
            "course_description^4"
          ]
        }
      },
      "boost_mode": "multiply"
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_63933144",
        "_type": "_doc",
        "_id": "1",
        "_score": 14.506382,
        "_source": {
          "id": "Accounting 101",
          "room": "E3",
          "professor": {
            "name": "Thomas Baszo",
            "email": "baszot@onuni.com"
          },
          "students_enrolled": 27,
          "course_description": " financial statements"
        }
      },
      {
        "_index": "stof_63933144",
        "_type": "_doc",
        "_id": "3",
        "_score": 7.846633,
        "_source": {
          "name": "Accounting 101",
          "room": "E3",
          "professor": {
            "name": "Baszo",
            "email": "Thomas@onuni.com"
          },
          "students_enrolled": 27,
          "course_description": "Nothing"
        }
      },
      {
        "_index": "stof_63933144",
        "_type": "_doc",
        "_id": "2",
        "_score": 5.9089565,
        "_source": {
          "name": "Accounting 101",
          "room": "E3",
          "professor": {
            "name": "Sachin Baszo",
            "email": "baszot@onuni.com"
          },
          "students_enrolled": 27,
          "course_description": "Thomas  Thomas Thomas Thomas "
        }
      }
    ]

更新1:

要同时搜索 Thomas OR Sachin 的bor,可以使用以下查询:

To search for bor both Thomas OR Sachin, you can use the below query:

{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query": "Thomas Sachin",
          "fields": [
            "professor.name^16",
            "professor.email^8",
            "course_description^4"
          ],
          "operator": "OR",
          "type": "cross_fields"
        }
      },
      "boost_mode": "multiply"
    }
  }
}

这篇关于如何在具有属性层次结构的DSL查询中增加权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆