如何为DSL查询搜索字符串中的一个属性赋予更多权重 [英] How to give more weightage for one attributes in DSL query search string

查看:84
本文介绍了如何为DSL查询搜索字符串中的一个属性赋予更多权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是查询

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

我的输出将第二个文档显示为第一个文档,因为它包含"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

Python

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

推荐答案

不建议使用 query_string ,如

由于任何无效语法都会返回错误,因此我们不建议您使用query_string查询搜索框.

Because it returns an error for any invalid syntax, we don’t recommend using the query_string query for search boxes.

如果您不需要支持查询语法,请考虑使用匹配项询问.如果您需要查询语法的功能,请使用simple_query_string查询,不那么严格.

If you don’t need to support a query syntax, consider using the match query. If you need the features of a query syntax, use the simple_query_string query, which is less strict.

您可以使用 Boost 其中

各个字段可以自动增强-计入更多相关分数—查询时

Individual fields can be boosted automatically — count more towards the relevance score — at query time

添加带有索引映射,搜索查询和搜索结果的工作示例

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

索引映射:

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

这篇关于如何为DSL查询搜索字符串中的一个属性赋予更多权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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