es查询值dsl复合使用

1.搜索消息体如下:

{
  'index': 'dev_jobs',
  'query': {
    'bool': {
      'must': [
        {
          'bool': {
            'should': [
              {
                'query_string': {
                  'query': '("nic\'s gourmet desserts, inc.")^100 OR (nic\'s~1 gourmet~2 desserts,~2 inc.~1)^10',
                  'fields': [
                    'job_title',
                    'company_name']
                }
              },
              {
                'match': {
                  'job_title.keyword': {
                    'query': "NIC's Gourmet Desserts, Inc.",
                    'boost': 2000
                  }
                }
              },
              {
                'multi_match': {
                  'query': "NIC's Gourmet Desserts, Inc.",
                  'fields': [
                    'function_job_title_translation.*'],
                  'type': 'most_fields'
                }
              },
              {
                'match': {
                  'company_name.keyword': {
                    'query': "NIC's Gourmet Desserts, Inc.",
                    'boost': 1000
                  }
                }
              }]
          }
        }],
      'filter': [
        {
          'term': {
            'recruiter_status_key.keyword': 'active'
          }
        },
        {
          'term': {
            'status_key.keyword': 'active'
          }
        },
        {
          'bool': {
            'must': {
              'bool': {
                'should': [
                  {
                    'bool': {
                      'must': [
                        {
                          'terms': {
                            'work_arrangement_id': [
                              1,
                              2]
                          }
                        },
                        {
                          'term': {
                            'country_id': 167
                          }
                        }]
                    }
                  },
                  {
                    'bool': {
                      'must': [
                        {
                          'term': {
                            'work_arrangement_id': 3
                          }
                        },
                        {
                          'term': {
                            'is_remote_worldwide': True
                          }
                        }]
                    }
                  },
                  {
                    'bool': {
                      'must': [
                        {
                          'term': {
                            'work_arrangement_id': 3
                          }
                        },
                        {
                          'term': {
                            'is_remote_worldwide': False
                          }
                        },
                        {
                          'terms': {
                            'remote_countries.id': [
                              167]
                          }
                        }]
                    }
                  }]
              }
            }
          }
        }],
      'must_not': [
        {
          'term': {
            'is_hidden': True
          }
        }]
    }
  },
  'source': [
    'id',
    'job_title',
    'salary_range_from',
    'salary_range_to',
    'job_url',
    'status_key',
    'job_type_id',
    'job_type',
    'job_location_id',
    'job_location',
    'job_region_id',
    'job_region',
    'country_id',
    'job_country_key',
    'job_country',
    'degree_id',
    'degree',
    'xp_lvl_id',
    'xp_lvl',
    'work_arrangement_id',
    'work_arrangement',
    'job_skills',
    'job_benefits',
    'remote_countries',
    'languages',
    'timezones',
    'is_featured',
    'highlighted',
    'external_apply_url',
    'is_salary_hidden',
    'is_remote_worldwide',
    'function_job_title_id',
    'function_job_title',
    'main_job_function_id',
    'main_function',
    'job_function_id',
    'sub_function',
    'currency_id',
    'salary_type_id',
    'salary_type',
    'refreshed_at',
    'expired_at',
    'updated_at',
    'company_id',
    'company_name',
    'company_logo',
    'company_url',
    'company_address',
    'company_location_id',
    'company_location',
    'company_country_id',
    'company_country',
    'company_industry_id',
    'company_industry',
    'company_size_id',
    'company_size',
    'company_financing_stage_id',
    'company_financing_stage',
    'is_company_verify',
    'recruiter_id',
    'recruiter_full_name',
    'recruiter_job_title',
    'recruiter_avatar',
    'recruiter_last_active_at',
    'recruiter_is_online'],
  'sort': [
    {
      '_script': {
        'type': 'number',
        'order': 'desc',
        'script': {
          'source': 'doc[\'job_title.keyword\'].value.toLowerCase() == "NIC\'s Gourmet Desserts, Inc.".toLowerCase() ? 1 : 0'
        }
      }
    },
    {
      '_script': {
        'type': 'number',
        'order': 'desc',
        'script': {
          'source': "doc.job_location_id.empty ? 0 : [].contains((int)doc['job_location_id'].value) ? 1 : 0"
        }
      }
    },
    {
      'published_at.keyword': 'desc'
    },
    '_score',
    {
      'activity_score': 'desc'
    },
    {
      'salary_range_to': 'desc'
    },
    {
      'salary_range_from': 'desc'
    }],
  'from_': 0,
  'size': 10,
  'search_type': 'dfs_query_then_fetch',
  'track_total_hits': True,
  'collapse': {
    'field': 'company_id'
  },
  'aggs': {
    'total': {
      'cardinality': {
        'field': 'company_id'
      }
    }
  }
}

 

2搜索示例:

search_payload = {
    "index": "dev_jobs",
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "('toby\\'s sports')^100 OR (toby's~2 sports~2)^10",
                                    "fields": ["job_title", "company_name"],
                                }
                            },
                            {
                                "match": {
                                    "job_title.keyword": {
                                        "query": "toby\'s sports",
                                        "boost": 2000,
                                    }
                                }
                            },
                            {
                                "multi_match": {
                                    "query": "toby\'s sports",
                                    "fields": ["function_job_title_translation.*"],
                                    "type": "most_fields",
                                }
                            },
                            {
                                "match": {
                                    "company_name.keyword": {
                                        "query": "toby\'s sports",
                                        "boost": 1000,
                                    }
                                }
                            },
                        ]
                    }
                }
            ],
            "filter": [
                {"term": {"recruiter_status_key.keyword": "active"}},
                {"term": {"status_key.keyword": "active"}},
                {
                    "bool": {
                        "must": {
                            "bool": {
                                "should": [
                                    {
                                        "bool": {
                                            "must": [
                                                {
                                                    "terms": {
                                                        "work_arrangement_id": [1, 2]
                                                    }
                                                },
                                                {"term": {"country_id": 167}},
                                            ]
                                        }
                                    },
                                    {
                                        "bool": {
                                            "must": [
                                                {"term": {"work_arrangement_id": 3}},
                                                {"term": {"is_remote_worldwide": True}},
                                            ]
                                        }
                                    },
                                    {
                                        "bool": {
                                            "must": [
                                                {"term": {"work_arrangement_id": 3}},
                                                {
                                                    "term": {
                                                        "is_remote_worldwide": False
                                                    }
                                                },
                                                {
                                                    "terms": {
                                                        "remote_countries.id": [167]
                                                    }
                                                },
                                            ]
                                        }
                                    },
                                ]
                            }
                        }
                    }
                },
            ],
            "must_not": [{"term": {"is_hidden": True}}],
        }
    },
    "source": [
        "id",
        "job_title",
        "salary_range_from",
        "salary_range_to",
        "job_url",
        "status_key",
        "job_type_id",
        "job_type",
        "job_location_id",
        "job_location",
        "job_region_id",
        "job_region",
        "country_id",
        "job_country_key",
        "job_country",
        "degree_id",
        "degree",
        "xp_lvl_id",
        "xp_lvl",
        "work_arrangement_id",
        "work_arrangement",
        "job_skills",
        "job_benefits",
        "remote_countries",
        "languages",
        "timezones",
        "is_featured",
        "highlighted",
        "external_apply_url",
        "is_salary_hidden",
        "is_remote_worldwide",
        "function_job_title_id",
        "function_job_title",
        "main_job_function_id",
        "main_function",
        "job_function_id",
        "sub_function",
        "currency_id",
        "salary_type_id",
        "salary_type",
        "refreshed_at",
        "expired_at",
        "updated_at",
        "company_id",
        "company_name",
        "company_logo",
        "company_url",
        "company_address",
        "company_location_id",
        "company_location",
        "company_country_id",
        "company_country",
        "company_industry_id",
        "company_industry",
        "company_size_id",
        "company_size",
        "company_financing_stage_id",
        "company_financing_stage",
        "is_company_verify",
        "recruiter_id",
        "recruiter_full_name",
        "recruiter_job_title",
        "recruiter_avatar",
        "recruiter_last_active_at",
        "recruiter_is_online",
    ],
    "sort": [
        {
            "_script": {
                "type": "number",
                "order": "desc",
                "script": {
                    "source": "doc['job_title.keyword'].value.toLowerCase() == 'toby\'s sports'.toLowerCase() ? 1 : 0"
                },
            }
        },
        {
            "_script": {
                "type": "number",
                "order": "desc",
                "script": {
                    "source": "doc.job_location_id.empty ? 0 : [].contains((int)doc['job_location_id'].value) ? 1 : 0"
                },
            }
        },
        {"published_at.keyword": "desc"},
        "_score",
        {"activity_score": "desc"},
        {"salary_range_to": "desc"},
        {"salary_range_from": "desc"},
    ],
    "from_": 0,
    "size": 10,
    "search_type": "dfs_query_then_fetch",
    "track_total_hits": True,
    "collapse": {"field": "company_id"},
    "aggs": {"total": {"cardinality": {"field": "company_id"}}},
}


print(search_payload)
response = client.search(**search_payload)
print("response=======", response)


# import re
#
# def escape_single_quote(input_str):
#     # 使用正则表达式进行替换,添加两个反斜杠
#     escaped_str = re.sub(r"(?<=\w)'(?=\w)", r"\\\\'", input_str)
#     return escaped_str
#
# # 示例用法
# original_str = "tobys sports"
# escaped_str = escape_single_quote(original_str)
# print(escaped_str)

 

posted @ 2024-01-02 14:17  Οo白麒麟оΟ  阅读(38)  评论(0编辑  收藏  举报