简体   繁体   English

弹性搜索完全匹配查询

[英]Elastic Search exact match query

I want to construct a query which would be exactly match the attribute and accept multiple value for searching against the attribute. 我想构造一个与属性完全匹配的查询,并接受针对该属性搜索的多个值。

Currently I'm using following query to get data for single document. 目前,我正在使用以下查询来获取单个文档的数据。

query = {
    "match_phrase" : {
      "id" : "abc"
    }
};

I want to match multiple values against the id attributes. 我想针对id属性匹配多个值。 Something like 就像是

query = {
    "match_phrase" : {
      "id" : ["abc", "xyz"]
    }
};

I would like to know what is working equivalent of above query. 我想知道什么是等效的上述查询。

{
  "abc    " : {
    "mappings    " : {
      "keywordsearches    " : {
        "properties    " : {
          "keyword    " : {
            "type    " : "string    "
          },
          "module_name    " : {
            "type    " : "string    "
          },
          "timestamp    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          }
        }
      },
      "npmmodules    " : {
        "properties    " : {
          "author    " : {
            "type    " : "string    "
          },
          "contributors    " : {
            "type    " : "string    "
          },
          "created_at    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          },
          "curatedcontents    " : {
            "properties    " : {
              "description    " : {
                "type    " : "string    "
              },
              "link    " : {
                "type    " : "string    "
              }
            }
          },
          "curatedcontents_count    " : {
            "type    " : "long    "
          },
          "datachanged    " : {
            "type    " : "boolean    "
          },
          "dependencies    " : {
            "properties    " : {
              "name    " : {
                "type    " : "string    "
              },
              "version    " : {
                "type    " : "string    "
              }
            }
          },
          "description    " : {
            "type    " : "string    "
          },
          "downloads    " : {
            "properties    " : {
              "count    " : {
                "type    " : "long    "
              },
              "day    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              }
            }
          },
          "downloads_meta    " : {
            "properties    " : {
              "last_updated    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              }
            }
          },
          "fork    " : {
            "type    " : "boolean    "
          },
          "full_name    " : {
            "type    " : "string    "
          },
          "gh_meta    " : {
            "properties    " : {
              "blacklist    " : {
                "properties    " : {
                  "repo    " : {
                    "type    " : "string    "
                  },
                  "user    " : {
                    "type    " : "string    "
                  }
                }
              },
              "etag    " : {
                "type    " : "string    "
              },
              "last-modified    " : {
                "type    " : "string    "
              }
            }
          },
          "git_url    " : {
            "type    " : "string    "
          },
          "homepage    " : {
            "type    " : "string    "
          },
          "html_url    " : {
            "type    " : "string    "
          },
          "id    " : {
            "type    " : "string    "
          },
          "keywords    " : {
            "type    " : "string    "
          },
          "latest    " : {
            "type    " : "string    "
          },
          "licenses    " : {
            "properties    " : {
              "MIT    " : {
                "type    " : "string    "
              },
              "covers    " : {
                "type    " : "string    "
              },
              "description    " : {
                "type    " : "string    "
              },
              "file    " : {
                "type    " : "string    "
              },
              "license    " : {
                "type    " : "string    "
              },
              "link    " : {
                "type    " : "string    "
              },
              "name    " : {
                "type    " : "string    "
              },
              "type    " : {
                "type    " : "string    "
              },
              "type:    " : {
                "type    " : "string    "
              },
              "ur    " : {
                "type    " : "string    "
              },
              "url    " : {
                "type    " : "string    "
              },
              "version    " : {
                "type    " : "string    "
              }
            }
          },
          "maintainers    " : {
            "type    " : "string    "
          },
          "name    " : {
            "type    " : "string    "
          },
          "npm_meta    " : {
            "properties    " : {
              "etag    " : {
                "type    " : "string    "
              },
              "last-modified    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              }
            }
          },
          "npm_stars    " : {
            "type    " : "long    "
          },
          "open_issues_count    " : {
            "type    " : "long    "
          },
          "pushed_at    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          },
          "rank    " : {
            "type    " : "double    "
          },
          "readme    " : {
            "type    " : "string    "
          },
          "releases    " : {
            "properties    " : {
              "time    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              },
              "version    " : {
                "type    " : "string    "
              }
            }
          },
          "repository    " : {
            "properties    " : {
              "author    " : {
                "type    " : "string    "
              },
              "bugs    " : {
                "type    " : "string    "
              },
              "commit_date    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              },
              "description    " : {
                "type    " : "string    "
              },
              "dist    " : {
                "type    " : "string    "
              },
              "git    " : {
                "type    " : "string    "
              },
              "github    " : {
                "type    " : "string    "
              },
              "handle    " : {
                "type    " : "string    "
              },
              "homepage    " : {
                "type    " : "string    "
              },
              "id_string    " : {
                "type    " : "string    "
              },
              "job    " : {
                "type    " : "string    "
              },
              "main    " : {
                "type    " : "string    "
              },
              "path    " : {
                "type    " : "string    "
              },
              "private    " : {
                "type    " : "string    "
              },
              "repository    " : {
                "type    " : "string    "
              },
              "revision    " : {
                "type    " : "string    "
              },
              "static    " : {
                "type    " : "string    "
              },
              "title    " : {
                "type    " : "string    "
              },
              "tyep    " : {
                "type    " : "string    "
              },
              "typ    " : {
                "type    " : "string    "
              },
              "type    " : {
                "type    " : "string    "
              },
              "type:    " : {
                "type    " : "string    "
              },
              "update    " : {
                "type    " : "string    "
              },
              "ur    " : {
                "type    " : "string    "
              },
              "url    " : {
                "type    " : "string    "
              },
              "web    " : {
                "type    " : "string    "
              },
              "web     " : {
                "type    " : "string    "
              }
            }
          },
          "rev    " : {
            "type    " : "string    "
          },
          "stargazers_count    " : {
            "type    " : "long    "
          },
          "subscribers_count    " : {
            "type    " : "long    "
          },
          "updated_at    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          },
          "userreviews    " : {
            "properties    " : {
              "review    " : {
                "type    " : "string    "
              },
              "timestamp    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              }
            }
          },
          "version    " : {
            "type    " : "string    "
          },
          "watchers_count    " : {
            "type    " : "long    "
          }
        }
      },
      "npmusers    " : {
        "properties    " : {
          "blog    " : {
            "type    " : "string    "
          },
          "created_at    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          },
          "email    " : {
            "type    " : "string    "
          },
          "followers    " : {
            "type    " : "long    "
          },
          "following    " : {
            "type    " : "long    "
          },
          "gh_meta    " : {
            "properties    " : {
              "blacklist    " : {
                "properties    " : {
                  "user    " : {
                    "type    " : "string    "
                  }
                }
              },
              "etag    " : {
                "type    " : "string    "
              },
              "last-modified    " : {
                "type    " : "string    "
              }
            }
          },
          "github    " : {
            "type    " : "string    "
          },
          "html_url    " : {
            "type    " : "string    "
          },
          "id    " : {
            "type    " : "string    "
          },
          "name    " : {
            "type    " : "string    "
          },
          "npm_meta    " : {
            "properties    " : {
              "etag    " : {
                "type    " : "string    "
              },
              "last-updated    " : {
                "type    " : "date    ",
                "format    " : "dateOptionalTime    "
              }
            }
          },
          "public_gists    " : {
            "type    " : "long    "
          },
          "public_repos    " : {
            "type    " : "long    "
          },
          "type    " : {
            "type    " : "string    "
          },
          "updated_at    " : {
            "type    " : "date    ",
            "format    " : "dateOptionalTime    "
          }
        }
      }
    }
  }
}

You could use boolean query. 您可以使用布尔查询。

{
    "bool" : {            
        "should" : [
            {"match_phrase" : {"id" : "abc" }},
            {"match_phrase" : {"id" : "xyz" }}
        ],
        "minimum_should_match" : 1            
    }
}

Use Terms Query: 使用条款查询:

 {
  "query": {
 "terms": {
  "id": [
    "abc",
    "xyz"
   ]
  }
 } 
}

HOpe it helps! 希望能帮助到你!

Finally I ended up using mget which provides a exact match for any attribute in the document 最后,我最终使用了mget,它为文档中的任何属性提供了精确匹配

{
    "index" : "index",
    "type" : "type",
    "body" : {
        "attribute_name" : "attribute_value"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM