elasticsearch - 需要 Elasticsearch 查询以下所需情况

标签 elasticsearch

要求如下。
我希望结果以这样一种方式进行记录:首先要匹配两个技能的记录,然后是至少具有一个技能的记录,然后是其余的(不匹配)。如果给定查询中有'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 .

最佳答案

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

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

关于elasticsearch - 需要 Elasticsearch 查询以下所需情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63900682/

相关文章:

node.js - Node + Elasticsearch : Sending a body on a GET request?

elasticsearch - 如何在 elasticsearch 中配置每个集群的分片数

elasticsearch - 使用Logstash通过单个配置文件将数据馈送到输出插件中的多个elasticsearch “clusters”

json - 在node.js中获取json输出(使用elasticsearch和express作为Web框架)

optimization - Elasticsearch查询优化

elasticsearch 无法解析主机 [elasticsearch-discovery]

php - 如何在PHP中使用Elasticsearch搜索 "randomize"结果?

python - Scrapy - 数据库选择

elasticsearch - Elasticsearch 中的聚合计数/总和

java - 使用 Java 检查 Elasticsearch 部署当前是否可用