Skip to content
This repository was archived by the owner on Oct 11, 2019. It is now read-only.

Elasticsearch Notes

Dan Ford edited this page Feb 10, 2015 · 3 revisions

Filtering by date: neither 'gte' nor 'lte' can be an empty string. Either has to be there or not:

http://localhost:9200/search/_search/

Does not work:

{
    "filter": {
      "range": {
        "created_date": {
          "gte": "2014-01-01T09:08:29.421-0500",
          "lte": ""
        }
      }
    }
  }

Works:

  {
    "filter": {
      "range": {
        "created_date": {
          "gte": "2014-01-01T09:08:29.421-0500"
        }
      }
    }
  }

The different filter types should each be an AND block. Within the location and type, the checked boxes should be represented inside an OR block for each one.

http://localhost:9200/search/_search/

{
  "filter": {
    "and": [
      {
        "or": [
          {
            "term": {
              "loc": "ghe"
            }
          },
          {
            "term": {
              "loc": "gh"
            }
          }
        ]
      },
      {
        "or": [
          {
            "type": {
              "value": "issue"
            }
          }
        ]
      },
      {
        "range": {
          "created_date": {
            "gte": "2014-01-01T09:08:29.421-0500",
            "lte": "2014-01-11T09:08:29.421-0500"
          }
        }
      },
      {
        "term": {
          "author": "me",
        }
      },
      {
        "term": {
          "path_full": "OH-100"
        }
      },
      {
        "term": {
            "assignee": "mike"
        }
      }
    ]
  }
}

The "term" filters can either be split up as in the previous example, or combined into a single "term" "and":

http://localhost:9200/search/_search/

{
  "filter": {
    "and": [
      {
        "or": [
          {
            "term": {
              "loc": "ghe"
            }
          },
          {
            "term": {
              "loc": "gh"
            }
          }
        ]
      },
      {
        "or": [
          {
            "type": {
              "value": "issue"
            }
          }
        ]
      },
      {
        "range": {
          "created_date": {
            "gte": "2014-01-01T09:08:29.421-0500",
            "lte": "2014-01-11T09:08:29.421-0500"
          }
        }
      },
      {
        "term": {
          "author": "me",
          "assignee": "mike",
          "path_full": "OH-100"
        }
      }
    ]
  }
}

Need to combine with query? Throw "query": { "filtered" } around it and add a "query" to "filtered".

http://localhost:9200/search/_search/

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "or": [
              {
                "term": {
                  "loc": "ghe"
                }
              },
              {
                "term": {
                  "loc": "gh"
                }
              }
            ]
          }
        ]
      },
      "query": {
        "match": {
          "_all": "page"
        }
      }
    }
  }
}

Get all Authors with term aggregations (takes the place of term facets):

http://localhost:9200/search/_search/

{
  "aggs": {
    "authors": {
      "terms": {
        "field": "author"
      }
    }
  }
}

result['aggregations']['authors']['buckets'][i]['key']

Get all paths:

http://localhost:9200/search/_search/

{
  "aggs": {
    "paths": {
      "terms": {
        "field": "path_full"
      }
    }
  }
}

result['aggregations']['paths']['buckets'][i]['key']

No filters, just search?

http://localhost:9200/search/_search/

{
  "query": {
    "filtered": {
      "filter": {
        
      },
      "query": {
        "match": {
          "_all": "content"
        }
      }
    }
  }
}

You can't leave an empty "and" there.

Bulk adding documents:

{operation_name: {metadata}} {actual body of document you want to index}

example:

{"index": {"_type": "path", "_id": "/test/path", "_index": "autocomplete"}}  
{"path": "/test/path"}

curl -s -XPOST 'http://localhost:9200/_bulk' --data-binary @data_file.txt

Autocomplete

To autocomplete the user, use the following filter:

http://localhost:9200/autocomplete/user/_search/

{
  "filter": {
    "term": {
      "user": "search-goes-here"
    }
  }
}

Likewise for path:

http://localhost:9200/autocomplete/path/_search/

{
  "filter": {
    "term": {
      "path": "search-goes-here"
    }
  }
}