How to get elements matching a partial text - elasticsearch

I'm using NEST to create services, so I can search into a field (label)
Is there a way to get answers from a partial string ?
For example, if I have three labels : "John Doe" , "Dadido" and "Unicorn", if I type "Do", I get the two first ones
For now, I have this :
elasticClient.Search<ESbase>(s => s.Query(q=>q.Regexp(c =>
c.Name("label_query")
.Field(p =>p.Label).Value('*'+label+'*'))));
And when I try it, it doesn't send anything back

match: { text: '.*label.*'}should work
If you want use regex: Value(".*label.*")
I assume you used default maping and in your label string you dont have special character.
Edit: use wildcard work too .Wildcard("*label*")

Related

Cypress - counting number of elements in an array that contain a specific string

Attempting to confirm that of all the schema in the head of a page exactly 3 of them should have a specific string within them. These schemas have no tags or sub classes to differentiate themselves from each other, only the text within them. I can confirm that the text exists within any of the schema:
cy.get('head > script[type="application/ld+json"]').should('contain', '"#type":"Product"')
But what I need is to confirm that that string exists 3 times, something like this:
cy.get('head > script[type="application/ld+json"]').contains('"#type":"Product"').should('have.length', 3)
And I can't seem to find a way to get this to work since .filter, .find, .contains, etc don't filter down the way I need them to. Any suggestions? At this point it seems like I either need to import a custom library or get someone to add ids to these specific schema. Thanks!
The first thing to note is that .contains() always yields a single result, even when many element match.
It's not very explicit in the docs, but this is what it says
Yields
.contains() yields the new DOM element it found.
If you run
cy.get('head > script[type="application/ld+json"]')
.contains('"#type":"Product"')
.then(console.log) // logs an object with length: 1
and open up the object logged in devtools you'll see length: 1, but if you remove the .contains('"#type":"Product"') the log will show a higher length.
You can avoid this by using the jQuery :contains() selector
cy.get('script[type="application/ld+json"]:contains("#type\": \"Product")')
.then(console.log) // logs an object with length: 3
.should('have.length', 3);
Note the inner parts of the search string have escape chars (\) for quote marks that are part of the search string.
If you want to avoid escape chars, use a bit of javascript inside a .then() to filter
cy.get('script[type="application/ld+json"]')
.then($els => $els.filter((index, el) => el.innerText.includes('"#type": "Product"')) )
.then(console.log) // logs an object with length: 3
.should('have.length', 3);

How should I configure elasticsearch mapping in order to get the MySQL "like" like behaviour?

I have a field say "name" which can contain :
Multiple words;
both lower case and upper case;
digits;
special characters: !##$%^&*();
different languages like: english, french, danish and others.
The task is to define the settings of this field so that when I search I can get the desirable results as follows: no matter what I pass in as searched string(ex: '1', 'a', '#1' 'èæ qтчert1') I should get all documents that contain searched sequence.
Note: I use elasticsearch v 5.6;
I believe the TEXT type, should be OK. The best possible way is by testing it.
PUT /language_test/sample/1
{
"sentence" :" 你吃饭了吗?",
"lang" :"chinese"
}
PUT /language_test/sample/2
{
"sentence" :"Var kan jag hitta någon som talar engelska?",
"lang" :"swedish"
}
GET /language_test/_mapping

How to get the first tag containing a word with xpath?

How can I get the first button which has type, class, id or ANYTHING containing text (have a substring equal to) close or Close or CLOSE? I tried this:
//button[contains(text(),'close')]
but it doesn't work.
Your predicate was testing whether any text() nodes contained "close". However, attributes are not text() nodes.
You can adjust your predicate to match on any attribute, then use a predicate on those attributes to test whether it's name() is "type", "class" or "id" and that it's lower-case() value contains "close":
With XPath 2.0 you could use this:
//button[#*[ name() = ('type','class','id') and contains(lower-case(.), 'close') ]]
With XPath 1.0, it takes a little more work. You can translate the upper-case letters into lower-case letters:
//button[
#*[name() = 'type' or name() = 'class' or name() = 'id']
[contains(translate(.,'CLOSE','close'), 'close')]]

How to use regex to match for number and string? [duplicate]

I have some IDs 214001, 214002, 215001, etc...
From a searchbar, I want autocompletion with the ID
"214" should trigger autocompletion for IDs 214001, 214002
Apparently, I can't just do a
scope :by_number, ->(number){
where(:number => /#{number.to_i}/i)
}
with mongoid. Anyone know a working way of matching a mongoid Integer field with a regex ?
This question had some clue, but how can I do this inside Rails ?
EDIT : The context is to be able to find a project by its integer ID or its short description :
scope :by_intitule, ->(regex){
where(:intitule => /#{Regexp.escape(regex)}/i)
}
# TODO : Not working !!!!
scope :by_number, ->(numero){
where(:number => /#{number.to_i}/i)
}
scope :by_name, ->(regex){
any_of([by_number(regex).selector, by_intitule(regex).selector])
}
The MongoDB solution from the linked question would be:
db.models.find({ $where: '/^124/.test(this.number)' })
Things that you hand to find map pretty much one-to-one to Mongoid:
where(:$where => "/^#{numero.to_i}/.test(this.number)")
The to_i call should make string interpolation okay for this limited case.
Keep in mind that this is a pretty horrific thing to do to your database: it can't use indexes, it will scan every single document in the collection, ...
You might be better off using a string field so that you can do normal regex matching. I'm pretty sure MongoDB will be able to use an index if you anchor your regex at the beginning too. If you really need it to be a number inside the database then you could always store it as both an Integer and a String field:
field :number, :type => Integer
field :number_s, :type => String
and then have some hooks to keep :number_s up to date as :number changes. If you did this, your pattern matching scope would look at :number_s. Precomputing and duplicating data like this is pretty common with MongoDB so you shouldn't feel bad about it.
The way to do a $where in mongoid is using Criteria#for_js
Something like this
Model.for_js("new RegExp(number).test(this.int_field)", number: 763)

Rails mongoid regex on an Integer field

I have some IDs 214001, 214002, 215001, etc...
From a searchbar, I want autocompletion with the ID
"214" should trigger autocompletion for IDs 214001, 214002
Apparently, I can't just do a
scope :by_number, ->(number){
where(:number => /#{number.to_i}/i)
}
with mongoid. Anyone know a working way of matching a mongoid Integer field with a regex ?
This question had some clue, but how can I do this inside Rails ?
EDIT : The context is to be able to find a project by its integer ID or its short description :
scope :by_intitule, ->(regex){
where(:intitule => /#{Regexp.escape(regex)}/i)
}
# TODO : Not working !!!!
scope :by_number, ->(numero){
where(:number => /#{number.to_i}/i)
}
scope :by_name, ->(regex){
any_of([by_number(regex).selector, by_intitule(regex).selector])
}
The MongoDB solution from the linked question would be:
db.models.find({ $where: '/^124/.test(this.number)' })
Things that you hand to find map pretty much one-to-one to Mongoid:
where(:$where => "/^#{numero.to_i}/.test(this.number)")
The to_i call should make string interpolation okay for this limited case.
Keep in mind that this is a pretty horrific thing to do to your database: it can't use indexes, it will scan every single document in the collection, ...
You might be better off using a string field so that you can do normal regex matching. I'm pretty sure MongoDB will be able to use an index if you anchor your regex at the beginning too. If you really need it to be a number inside the database then you could always store it as both an Integer and a String field:
field :number, :type => Integer
field :number_s, :type => String
and then have some hooks to keep :number_s up to date as :number changes. If you did this, your pattern matching scope would look at :number_s. Precomputing and duplicating data like this is pretty common with MongoDB so you shouldn't feel bad about it.
The way to do a $where in mongoid is using Criteria#for_js
Something like this
Model.for_js("new RegExp(number).test(this.int_field)", number: 763)

Resources