简体   繁体   中英

Elastic query to match any of provided conditions in BoolQuery

I made elastic.js build dynamic query like this:

{
    "size": 15,
    "query": {
        "bool": {
            "must": [
                { "term": { "tag": { "term": "rocket" } } },
                { "term": { "name": { "term": "space" } } }
            ],
            "should": [
                { "wildcard": { "place": { "value": "Moo*" } } }
            ]
        }
    }
}

Elastic.js code for this is ( Translator ):

ejs.Request()
    .size(15)
    .query(
      ejs.BoolQuery()
        .must(ejs.TermQuery('tag', 'rocket'))
        .must(ejs.TermQuery('name', 'space'))
        .should(ejs.WildcardQuery('place', 'Moo*'))
)

It works great, I am happy and I would like it to be like that. But I realized that I need to add an option to perform this query with OR condition, instead of AND as is now.

It should find documents that match any of provided conditions.

And that task leaves me with no clues (even about DSL that will complete this purpose).

I have looked at OrFilter but I have no idea on how to use it right (especially with elasticJS). Any solid source of information on elastic/js will be greatly appreciated

You can put all your clauses in the should section of your bool query .

Regarding elastic.js I personally think it's a great piece of software, really well documented as well. Have a look at its API documentation .

var r = ejs.Request({indices: index, types: type}),
    boolQ = ejs.BoolQuery();

boolQ.should(ejs.FieldQuery("DEAL_NAME","value")).defaultOperator("AND"));
boolQ.should(ejs.TermQuery( "DEAL_MOTTO","value"));                     
boolQ.should(ejs.FieldQuery("dealaddresses.CITY","value").defaultOperator("AND"));

r.query(boolQ);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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