pubmed-client (Node.js)
    Preparing search index...

    Class SearchQuery

    Builder for constructing PubMed search queries programmatically

    Provides a fluent API for building complex PubMed search queries with support for:

    • Basic search terms
    • Date filtering
    • Article type and language filtering
    • Open access filtering
    • Boolean logic operations (AND, OR, NOT)
    • MeSH terms and author filtering
    const query = new SearchQuery()
    .query("covid-19")
    .publishedInYear(2024)
    .articleType("Clinical Trial")
    .freeFullTextOnly();

    const articles = await client.executeQuery(query);
    Index

    Constructors

    Accessors

    • get limit(): number

      Get the limit for this query

      Returns number

      Maximum number of results (default: 20)

      const query = new SearchQuery().query("cancer").limit(100);
      query.getLimit(); // 100

    Methods

    • Search in article abstracts only

      Parameters

      • text: string

        Abstract text to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .abstractContains("neural networks");
    • Filter by institution/affiliation

      Parameters

      • institution: string

        Institution name to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cardiology")
      .affiliation("Harvard Medical School");
    • Filter by age group

      Parameters

      • ageGroup: string

        Age group to filter by (e.g., "Child", "Adult", "Aged")

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("pediatric medicine")
      .ageGroup("Child");
    • Combine this query with another using AND logic

      Parameters

      Returns SearchQuery

      New SearchQuery with combined logic

      const q1 = new SearchQuery().query("covid-19");
      const q2 = new SearchQuery().query("vaccine");
      const combined = q1.and(q2);
      combined.build(); // "(covid-19) AND (vaccine)"
    • Filter to animal studies only

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("preclinical research")
      .animalStudiesOnly();
    • Filter by a single article type

      Parameters

      • typeName: string

        Article type (case-insensitive) Supported types: "Clinical Trial", "Review", "Systematic Review", "Meta-Analysis", "Case Reports", "Randomized Controlled Trial" (or "RCT"), "Observational Study"

      Returns this

      Self for method chaining

      Error if article type is not recognized

      const query = new SearchQuery()
      .query("cancer")
      .articleType("Clinical Trial");
    • Filter by multiple article types (OR logic)

      Parameters

      • types: string[]

        Array of article type names (case-insensitive)

      Returns this

      Self for method chaining

      Error if any article type is not recognized

      const query = new SearchQuery()
      .query("treatment")
      .articleTypes(["RCT", "Meta-Analysis"]);
    • Filter by author name

      Parameters

      • name: string

        Author name to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("machine learning")
      .author("Williams K");
    • Build the final PubMed query string

      Returns string

      Query string for PubMed E-utilities API

      Error if no search terms have been added

      const query = new SearchQuery()
      .query("covid-19")
      .query("treatment");
      query.build(); // "covid-19 treatment"
    • Add a custom filter

      Parameters

      • filter: string

        Custom filter string in PubMed syntax

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("research")
      .customFilter("humans[mh]");
    • Exclude articles matching the given query

      Parameters

      • excluded: SearchQuery

        SearchQuery representing articles to exclude

      Returns SearchQuery

      New SearchQuery with exclusion logic

      const base = new SearchQuery().query("cancer treatment");
      const exclude = new SearchQuery().query("animal studies");
      const filtered = base.exclude(exclude);
      filtered.build(); // "(cancer treatment) NOT (animal studies)"
    • Filter by first author

      Parameters

      • name: string

        First author name to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cancer")
      .firstAuthor("Smith J");
    • Filter to articles with free full text (open access)

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cancer")
      .freeFullTextOnly();
    • Filter to articles with full text links (including subscription-based)

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("diabetes")
      .fullTextOnly();
    • Filter by grant number

      Parameters

      • grantNumber: string

        Grant number to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .grantNumber("R01AI123456");
    • Add parentheses around the current query for grouping

      Returns SearchQuery

      New SearchQuery wrapped in parentheses

      const query = new SearchQuery()
      .query("cancer")
      .or(new SearchQuery().query("tumor"))
      .group();
      query.build(); // "((cancer) OR (tumor))"
    • Filter to articles that have abstracts

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("genetics")
      .hasAbstract();
    • Filter to human studies only

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("drug treatment")
      .humanStudiesOnly();
    • Filter by journal name

      Parameters

      • name: string

        Journal name to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cancer")
      .journal("Nature");
    • Filter by language

      Parameters

      • lang: string

        Language name (case-insensitive) Supported: "English", "Japanese", "German", "French", "Spanish", etc. Unknown languages are passed through as custom values.

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cancer")
      .language("English");
    • Filter by last author

      Parameters

      • name: string

        Last author name to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("genomics")
      .lastAuthor("Johnson M");
    • Filter by MeSH major topic

      Parameters

      • term: string

        MeSH term to filter by as a major topic

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .meshMajorTopic("Diabetes Mellitus, Type 2");
    • Filter by MeSH subheading

      Parameters

      • subheading: string

        MeSH subheading to filter by

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .meshTerm("Diabetes Mellitus")
      .meshSubheading("drug therapy");
    • Filter by MeSH term

      Parameters

      • term: string

        MeSH term to filter by

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .meshTerm("Neoplasms");
    • Filter by multiple MeSH terms

      Parameters

      • terms: string[]

        Array of MeSH terms to filter by

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .meshTerms(["Neoplasms", "Antineoplastic Agents"]);
    • Negate this query using NOT logic

      Returns SearchQuery

      New SearchQuery with NOT logic

      const query = new SearchQuery().query("cancer").negate();
      query.build(); // "NOT (cancer)"
    • Combine this query with another using OR logic

      Parameters

      Returns SearchQuery

      New SearchQuery with combined logic

      const q1 = new SearchQuery().query("diabetes");
      const q2 = new SearchQuery().query("hypertension");
      const combined = q1.or(q2);
      combined.build(); // "(diabetes) OR (hypertension)"
    • Filter by ORCID identifier

      Parameters

      • orcidId: string

        ORCID identifier to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .orcid("0000-0001-2345-6789");
    • Filter to articles with PMC full text

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("genomics")
      .pmcOnly();
    • Filter to articles published after a specific year

      Parameters

      • year: number

        Year after which articles were published

      Returns this

      Self for method chaining

      Error if year is outside valid range

      const query = new SearchQuery()
      .query("crispr")
      .publishedAfter(2020);
    • Filter to articles published before a specific year

      Parameters

      • year: number

        Year before which articles were published

      Returns this

      Self for method chaining

      Error if year is outside valid range

      const query = new SearchQuery()
      .query("genome")
      .publishedBefore(2020);
    • Filter by publication date range

      Parameters

      • startYear: number

        Start year (inclusive)

      • OptionalendYear: number | null

        End year (inclusive, optional)

      Returns this

      Self for method chaining

      Error if years are outside valid range

      // Filter to 2020-2024
      const query = new SearchQuery()
      .query("cancer")
      .publishedBetween(2020, 2024);

      // Filter from 2020 onwards
      const query2 = new SearchQuery()
      .query("treatment")
      .publishedBetween(2020);
    • Filter to articles published in a specific year

      Parameters

      • year: number

        Year to filter by (must be between 1800 and 3000)

      Returns this

      Self for method chaining

      Error if year is outside valid range

      const query = new SearchQuery()
      .query("covid-19")
      .publishedInYear(2024);
    • Add a search term to the query

      Terms are accumulated and will be space-separated in the final query.

      Parameters

      • term: string

        Search term string

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("covid-19")
      .query("treatment");
      query.build(); // "covid-19 treatment"
    • Set the maximum number of results to return

      Parameters

      • limit: number

        Maximum number of results (clamped to 1-10,000 range)

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .query("cancer")
      .setLimit(50);
    • Set the sort order for search results

      Parameters

      • sortOrder: string

        Sort order (case-insensitive) Supported: "relevance", "pub_date", "author", "journal"

      Returns this

      Self for method chaining

      Error if sort order is not recognized

      const query = new SearchQuery()
      .query("cancer")
      .sort("pub_date");
    • Add multiple search terms at once

      Each term is processed like query(). Empty strings are filtered out.

      Parameters

      • terms: string[]

        Array of search term strings

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .terms(["covid-19", "vaccine", "efficacy"]);
      query.build(); // "covid-19 vaccine efficacy"
    • Search in article titles only

      Parameters

      • text: string

        Title text to search for

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .titleContains("machine learning");
    • Search in both title and abstract

      Parameters

      • text: string

        Text to search for in title or abstract

      Returns this

      Self for method chaining

      const query = new SearchQuery()
      .titleOrAbstract("CRISPR gene editing");