gpt4 book ai didi

python - 如何在具有属性层次结构的DSL查询中增加权重

转载 作者:行者123 更新时间:2023-12-02 23:07:43 24 4
gpt4 key购买 nike

  • 我需要搜索更多比重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*)"
    }
    }
    }
    我的输出将第二个文档显示为第一个文档,因为它在描述中包含“Thomas” 4次
  • 我需要给professor.name更大的权重,它应该首先显示是否检查,然后检查“professor.email”,然后检查其他属性

  • 预期输出的是 1,3,2 1,因为名称,3由于电子邮件,2由于描述
    python es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})

    最佳答案

    添加带有索引数据,搜索查询和搜索结果的工作示例
    boost中所述,在创建索引的同时,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:
    要搜索bor和 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"
    }
    }
    }

    关于python - 如何在具有属性层次结构的DSL查询中增加权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63936967/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com