Query with multiple values


(Charlene Ellul) #1

Hi,
I am very new to elastic search...I am trying to search 1 field with multiple values that are stored in a list...I am using a loop but it is not returning any values...any help pls?

the list contains 49846 elements

import elasticsearch
from elasticsearch import Elasticsearch
import elasticsearch.helpers

ES_HOST = {
    "host": "localhost",
    "port": 9200
}
ES_INDEX = "reviews"
ES_TYPE = "review"

es = Elasticsearch(hosts=[ES_HOST], )

for i in range (0, len(test)):
    print test[i]
    results_gen = elasticsearch.helpers.scan(
        es,
        query={'query': {'match': { 'categories' : test[i]}}},
        index=ES_INDEX,
        doc_type=ES_TYPE
    )

    results = list(results_gen)
    df11 = buildReviewDataFrame(rdf1,results)

(Luiz Santos) #2

Hi @Charlene_Ellul,

You can combine multiples categories in a should clause (meaning your query will act as an OR). For example, if you have this two documents:

POST review/review/1
{
  "categories": "clothes"
}

POST review/review/2
{
  "categories": "accessories"
}

You can use this query to search them:

GET review/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "categories": "clothes"
          }
        },
        {
          "match": {
            "categories": "accessories"
          }
        }
      ]
    }
  }
}

In python, you would end up in something like this:

import json
from elasticsearch import Elasticsearch, helpers


class Review(object):

    def __init__(self, index, doc_type):
        self.elasticsearch = Elasticsearch()
        self.index = index
        self.doc_type = doc_type

    def search(self, categories):
        query = {
            "query": {
                "bool": {
                    "should": []
                }
            }
        }
        for categorie in categories:
            query["query"]["bool"]["should"].append({
                "match": {
                    "categories": categorie
                }
            })
        results_gen = helpers.scan(self.elasticsearch, query=query, index=self.index, doc_type=self.doc_type)
        results = list(results_gen)
        print json.dumps(results, sort_keys=True, indent=4)


if __name__ == '__main__':
    Review(index="review", doc_type="review").search(categories=["clothes","accessories"])

Hope it helps.

Cheers,
LG


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.