【问题标题】:need elastic search query for the below required scenario需要针对以下所需场景的弹性搜索查询
【发布时间】:2020-09-15 11:15:30
【问题描述】:

要求如下。

我希望结果是这样一种方式,即记录匹配两个技能应该首先出现,然后任何具有至少一项技能的记录应该放在后面,其余的放在后面(不匹配)。如果有“n”个技能给定的查询,我应该得到'n'技能匹配的记录(如果存在)然后(n-1)等等直到0技能匹配

下面是我的索引映射

                            //***myindexmapping***
                        {
                              "skills": {
                                "type": "nested",
                                "dynamic": "true",
                                "properties": {
                                  "id": {
                                    "type": "long"
                                  },
                                  "isHighlight": {
                                    "type": "boolean"
                                  },
                                  "skill": {
                                    "type": "text",
                                    "fields": {
                                      "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 32766,
                                        "normalizer": "lowerasciinormalizer"
                                      }
                                    }
                                  },
                                  "yearsOfExp": {
                                    "type": "float"
                                  }
                                }
                              }
                            }

                    below is my index data
                        ***//myindexdata***
                        {
                              "_index": "tenant1_candidate",
                              "_type": "docs",
                              "_id": "953",
                              "_score": 0.0074906712,
                              "_source": {
                                "record": {
                                  "skills": [
                                    {
                                      "id": 0,
                                      "skill": "Jr.Software Developer",
                                      "yearsOfExp": 2,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "AJAX",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Angular Developer",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "AngularJS",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Apache Subversion",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "API",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Bachelors Degree",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Back End",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Bootstrap",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Cascading Style Sheets",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "CRUD",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "CSS3",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Design Patterns",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Excellent analytical and logical programming skills",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Front End",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Git",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "HTML",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "HTML5",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "JavaScript",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "jQuery",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Jr. UI Developer",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "JSON",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft Access",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft SQL Server",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Microsoft Visual Studio",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "MySQL",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Object Oriented Analysis/Design",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Responsive Web Design",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "REST API",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "RESTful",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Test-Driven Development",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "TypeScript",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Version Control",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "Web Services",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    },
                                    {
                                      "id": 0,
                                      "skill": "XML",
                                      "yearsOfExp": 1,
                                      "isHighlight": false
                                    }
                                  ]
                                },
                                "entityType": "Candidate",
                                "index": "tenant1_candidate",
                                "dbId": "953",
                                "id": "953"
                              }
                            }

                    below is my search query
                        ***//mysearchquery***
                        {
                              "from": "0",
                              "size": "30",
                              "track_scores": true,
                              "query": {
                                "bool": {
                                  "must": [
                                    {
                                      "term": {
                                        "entityType": "candidate"
                                      }
                                    },
                                    {
                                      "nested": {
                                        "path": "record.skills",
                                        "score_mode": "avg",
                                        "query": {
                                          "bool": {
                                            "should": [
                                              {
                                                "bool": {
                                                  "must": [
                                                    {
                                                      "match": {
                                                        "record.skills.skill.keyword": "XML"
                                                      }
                                                    },
                                                    {
                                                      "range": {
                                                        "record.skills.yearsOfExp": {
                                                          "gte": 0
                                                        }
                                                      }
                                                    }
                                                  ]
                                                }
                                              },
                                              {
                                                "bool": {
                                                  "must": [
                                                    {
                                                      "match": {
                                                        "record.skills.skill.keyword": "SQL"
                                                      }
                                                    },
                                                    {
                                                      "range": {
                                                        "record.skills.yearsOfExp": {
                                                          "gte": 0
                                                        }
                                                      }
                                                    }
                                                  ]
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }
                                  ]
                                }
                              }
                            }
                            -------------
              please look into this .

【问题讨论】:

  • 你有没有机会仔细阅读我的回答,期待得到你的反馈:)

标签: elasticsearch


【解决方案1】:

添加一个包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射

{
  "mappings": {
    "properties": {
      "record": {
        "properties": {
          "skills": {
            "type": "nested"
          }
        }
      }
    }
  }
}

索引数据:

{
  "record": {
    "skills": [
      {
        "id": 0,
        "skill": "Back End",
        "yearsOfExp": 1,
        "isHighlight": false
      },
      {
        "id": 0,
        "skill": "SQL",
        "yearsOfExp": 1,
        "isHighlight": false
      },
      {
        "id": 0,
        "skill": "XML",
        "yearsOfExp": 1,
        "isHighlight": false
      },
      {
        "id": 0,
        "skill": "XML SQL",
        "yearsOfExp": 1,
        "isHighlight": false
      }
    ]
  },
  "entityType": "Candidate",
  "index": "tenant1_candidate",
  "dbId": "953",
  "id": "953"
}

搜索查询

{
  "query": {
    "nested": {
      "path": "record.skills",
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "record.skills.skill": "XML"
              }
            },
            {
              "match": {
                "record.skills.skill": "SQL"
              }
            }
          ]
        }
      },
      "inner_hits":{}
    }
  }
}

搜索结果:

"hits": [
  {
    "_index": "test",
    "_type": "_doc",
    "_id": "1",
    "_score": 3.574434,
    "_source": {
      "record": {
        "skills": [
          {
            "id": 0,
            "skill": "Back End",
            "yearsOfExp": 1,
            "isHighlight": false
          },
          {
            "id": 0,
            "skill": "SQL",
            "yearsOfExp": 1,
            "isHighlight": false
          },
          {
            "id": 0,
            "skill": "XML",
            "yearsOfExp": 1,
            "isHighlight": false
          },
          {
            "id": 0,
            "skill": "XML SQL",
            "yearsOfExp": 1,
            "isHighlight": false
          }
        ]
      },
      "entityType": "Candidate",
      "index": "tenant1_candidate",
      "dbId": "953",
      "id": "953"
    },
    "inner_hits": {
      "record.skills": {
        "hits": {
          "total": {
            "value": 3,
            "relation": "eq"
          },
          "max_score": 4.718687,
          "hits": [
            {
              "_index": "test",
              "_type": "_doc",
              "_id": "1",
              "_nested": {
                "field": "record.skills",
                "offset": 3
              },
              "_score": 4.718687,     <-- note this
              "_source": {
                "id": 0,
                "skill": "XML SQL",
                "yearsOfExp": 1,
                "isHighlight": false
              }
            },
            {
              "_index": "test",
              "_type": "_doc",
              "_id": "1",
              "_nested": {
                "field": "record.skills",
                "offset": 1
              },
              "_score": 3.090395,     <-- note this
              "_source": {
                "id": 0,
                "skill": "SQL",    
                "yearsOfExp": 1,
                "isHighlight": false
              }
            },
            {
              "_index": "test",
              "_type": "_doc",
              "_id": "1",
              "_nested": {
                "field": "record.skills",
                "offset": 2
              },
              "_score": 2.9142203,     <-- note this
              "_source": {
                "id": 0,
                "skill": "XML",
                "yearsOfExp": 1,
                "isHighlight": false
              }
            }
          ]
        }
      }
    }
  }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2011-04-21
    • 2017-08-19
    • 2016-10-30
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多