Need to know how to search in ES using c# searching in arrays

Viewed 257

Hello I am a newbie on ElasticSearch and need help. I'm working with c# (thought I could use a QueryRaw in String I think...). Below the scenario:

Data

{
    "id": "1",
    "title": "Small cars",
    "tagsColours": ["grey",
    "black",
    "white"],
    "tagsCars": ["Suzuki",
    "Ford"],
    "tagsKeywords": []
},
{
    "id": "2",
    "title": "Medium cars",
    "tagsColours": [],
    "tagsCars": ["VW",
    "Audi",
    "Peugeot"],
    "tagsKeywords": ["Sedan"]
},
{
    "id": "3",
    "title": "Big cars",
    "tagsColours": ["red",
    "black"],
    "tagsCars": ["Jeep",
    "Dodge"],
    "tagsKeywords": ["Van",
    "Big"]
}

Objective

Id' like to apply filters on tags columns based on users' selection. the values will be populated in the tagsXXX array columns withselected values.

  • if parameter array value is not empty then the result should contain at least one instance. Same for every parameter array. the more the parameters have values, the more specific search should be done
  • if at least there's one value coming from the parameter that matches amongst all values in any document's tag column array, then get that document. but if there's another value on another tagsXXX array then it should take it into account.
  • if the tag parameter array has no values, then disregard that filter

Desired responses

A) If user select only 1 tag Color (i.e= black) as formatted below:

{
    id: "",
    title: "",
    tagsColours: ["black"],
    tagsCars: [],
    tagsKeywords: []
}

I'd like to get documents with Id=2 and id=3 since they have black in their tagsColours and disregard tagsCars and tagsKeywords since there are no values on the parameters

B) If user select only 2 diff tags (i.e= colour=black and cars= audi, and mercedez benz) as formatted below:

{
    id: "",
    title: "",
    tagsColours: ["black",
    "yellow"],
    tagsCars: ["Audi",
    "Mercedes Benz"],
    tagsKeywords: []
}

I'd like to get documents with id=2 since it found black on tagsColours and it found Audi in tagsCars, AND it should not pull document id=1 because even when black is on tagsColours, none of paramters values (audi, mercedez benz) is on its tagsCars values

Hello everyone, I'm having issues when trying to search on ElasticSearch and look for in arrays with values, and when parameters have no values. If anyone could helpe me on this I'd appreciatte. I did this:

termsQuery = Query<StructuredData>.Terms(t => t.Field(f =>f.TagsColours).Terms(dataToSearch.TagsColours));
termsQuery = termsQuery && Query<StructuredData>.Terms(t => t.Field(f =>f.TagsCars).Terms(dataToSearch.TagsCars));

and I stopped here (did not add third filter) because I could not mix two filters together dataToSearch has the values from parameters (same structure object, cause .Search makes me do that here .Search()

var settings = new ConnectionSettings(node);

var response = new ElasticClient(settings)
.Search<StructuredData>(
s => s.AllIndices()
.AllTypes()
.From(0)
.Size(50)
.Query(_ => termsQuery)
);

But I'm having problems when using more than 1 filter.. any ideas? is ".Terms" the correct property?

1 Answers
Related