python—如何在具有属性层次结构的dsl查询中添加权重

iyr7buue  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(399)

我需要寻找更多的重量 professor.name 那么下一个得到权重的属性是 professor.email 检查搜索字符串所在的其他字段
以下是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次“thomas”
我需要给你更多的重量 professor.name 它应该先显示check,如果没有,然后选择professor.email,然后选择其他属性
预计出局为 1,3,2 1是因为名字,3是因为电子邮件,2是因为描述
python es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})

fruv7luv

fruv7luv1#

添加索引数据、搜索查询和搜索结果的工作示例
如前一个答案所述,在创建索引时,boost也可以应用于各个字段
索引数据:

{
    "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 ,您可以使用以下查询:

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

相关问题