如何在dsl查询搜索字符串中为一个属性赋予更多权重

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

以下是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 "
   }

下面是查询

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

我的输出将显示第二个文档作为第一个,因为它在描述中包含4次“thomas”
我需要给你更多的重量 professor.name 它应该先显示check,如果没有,然后选择professor.email,然后选择其他属性
python es.search(index="data", body={"query": {"query_string": {"query": "(*Thomas*)"}}})

r1wp621o

r1wp621o1#

不建议使用 query_string ,如es官方文件所述:
因为对于任何无效的语法,它都会返回一个错误,所以我们不建议对搜索框使用query\u string查询。
如果不需要支持查询语法,请考虑使用匹配查询。如果您需要查询语法的特性,请使用简单的\u查询\u字符串查询,它不太严格。
你可以在哪里使用boost
单个字段可以自动增强 — 在相关性得分中计算更多 — 在查询时
添加索引Map、搜索查询和搜索结果的工作示例
索引Map:

{
    "mappings": {
        "properties": {
            "professor": {
                "properties": {
                    "name": {
                        "type": "text",
                        "boost": 2
                    }
                }
            }
        }
    }
}

搜索查询:

{
  "query": {
    "multi_match" : {
      "query": "Thomas", 
      "fields": [ "course_description", "professor.name" ] 
    }
  }
}

搜索结果:

"hits": [
            {
                "_index": "stof_63933144",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.3862942,     <-- note this
                "_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": "2",
                "_score": 1.1090355,   <-- note this
                "_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 或者 Sachin ```
{
"query": {
"multi_match" : {
"query": "(Thomas) OR (Sachin)",
"fields": [ "course_description", "professor.name" ]
}
}
}

更新2:
多匹配查询使用 `"operator":"OR"` ```
{
  "query": {
    "multi_match" : {
      "query": "Thomas Sachin", 
      "fields": [ "course_description", "professor.name" ] ,
      "operator":"OR",
      "type":"cross_fields"
    }
  }
}

相关问题